{"id":2270,"date":"2021-03-24T19:21:36","date_gmt":"2021-03-24T16:21:36","guid":{"rendered":"https:\/\/artem.services\/?p=2165"},"modified":"2022-06-08T21:46:02","modified_gmt":"2022-06-08T18:46:02","slug":"2270","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=2270&lang=en","title":{"rendered":"\u00a0Python &#8212; AWS EBS creating snapshots based on a tag and keeping only one latest version"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"wp-image-2273 size-full aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo.png\" alt=\"\" width=\"1024\" height=\"500\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-300x146.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-768x375.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-954x466.png 954w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"2\"><span class=\"Q4iAWc\">This script looks for an <strong>EBS<\/strong> in the region &quot;<strong>eu-west-1<\/strong>&quot; with a tag whose key is &quot;<strong>Application<\/strong>&quot; and the value is passed as an argument, creating a snapshot of this EBS.<\/span><\/span> <span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"1\" data-number-of-phrases=\"2\"><span class=\"Q4iAWc\">In the same way, it searches for a snapshot by tag and deletes everything except the last one.<\/span><\/span><\/span><\/p>\n<p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"1\"><span class=\"Q4iAWc\">An example of running to create a snapshot:<\/span><\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 main.py create artem-test-app\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"1\"><span class=\"Q4iAWc\">An example of running to delete old snapshots:<\/span><\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 main.py cleanup artem-test-app\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>main.py:<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom __future__ import print_function\r\nfrom datetime import datetime\r\nimport sys\r\nimport boto3\r\nimport botocore\r\nimport urllib.request\r\n\r\nec2 = boto3.client(&#039;ec2&#039;, region_name=&#039;eu-west-1&#039;)\r\n\r\ndef create_snapshot(app):\r\n    print(&quot;Creating snapshot for &quot; + str(app))\r\n    # Get Instance ID\r\n    instances_id = []\r\n    response = ec2.describe_instances(\r\n        Filters = [\r\n            {\r\n                &#039;Name&#039;: &#039;tag:Application&#039;,\r\n                &#039;Values&#039;: [\r\n                    app,\r\n                ]\r\n            },\r\n            {\r\n                &#039;Name&#039;: &#039;instance-state-name&#039;,\r\n                &#039;Values&#039;: [&#039;running&#039;]\r\n            }\r\n        ]\r\n    )\r\n\r\n    for reservation in response[&#039;Reservations&#039;]:\r\n        for instance in reservation[&#039;Instances&#039;]:\r\n            instances_id.append(instance[&#039;InstanceId&#039;])\r\n\r\n    # Get Volumes ID\r\n    volumes_id = []\r\n    for instance_id in instances_id:\r\n        response = ec2.describe_instance_attribute(InstanceId = instance_id, Attribute = &#039;blockDeviceMapping&#039;)\r\n        for BlockDeviceMappings in response[&#039;BlockDeviceMappings&#039;]:\r\n            volumes_id.append(BlockDeviceMappings[&#039;Ebs&#039;][&#039;VolumeId&#039;])\r\n\r\n    # Create Volume Snapshot\r\n    for volume_id in volumes_id:\r\n        date = datetime.now()\r\n        response = ec2.create_snapshot(\r\n            Description = app + &quot; snapshot created by Jenkins at &quot; + str(date),\r\n            VolumeId = volume_id,\r\n            TagSpecifications=[\r\n                {\r\n                    &#039;ResourceType&#039;: &#039;snapshot&#039;,\r\n                    &#039;Tags&#039;: [\r\n                        {\r\n                            &#039;Key&#039;: &#039;Name&#039;,\r\n                            &#039;Value&#039;: app + &quot;-snapshot&quot;\r\n                        },\r\n                    ]\r\n                },\r\n            ]\r\n        )\r\n\r\n\r\n        try:\r\n            print(&quot;Waiting until snapshot will be created...&quot;)\r\n            snapshot_id = response[&#039;SnapshotId&#039;]\r\n            snapshot_complete_waiter = ec2.get_waiter(&#039;snapshot_completed&#039;)\r\n            snapshot_complete_waiter.wait(SnapshotIds=[snapshot_id], WaiterConfig = { &#039;Delay&#039;: 30, &#039;MaxAttempts&#039;: 120})\r\n\r\n        except botocore.exceptions.WaiterError as e:\r\n                print(e)\r\n\r\ndef cleanup_snapshot(app):\r\n    print(&quot;Clean up step.&quot;)\r\n    print(&quot;Looking for all snapshots regarding &quot; + str(app) + &quot; application.&quot;)\r\n    # Get snapshots\r\n    response = ec2.describe_snapshots(\r\n        Filters = [\r\n            {\r\n                &#039;Name&#039;: &#039;tag:Name&#039;,\r\n                &#039;Values&#039;: [\r\n                    app + &quot;-snapshot&quot;,\r\n                ]\r\n            },\r\n        ],\r\n        OwnerIds = [\r\n            &#039;self&#039;,\r\n        ]\r\n    )\r\n\r\n    sorted_snapshots = sorted(response[&#039;Snapshots&#039;], key=lambda k: k[&#039;StartTime&#039;])\r\n\r\n    # Clean up snapshots and keep only the latest snapshot\r\n    for snapshot in sorted_snapshots[:-1]:\r\n        print(&quot;Deleting snapshot: &quot; + str(snapshot[&#039;SnapshotId&#039;]))\r\n        response = ec2.delete_snapshot(\r\n            SnapshotId = snapshot[&#039;SnapshotId&#039;]\r\n        )\r\n\r\ndef main():\r\n    if sys.argv[1:] and sys.argv[2:]:\r\n        action = sys.argv[1]\r\n        app = sys.argv[2]\r\n        if action == &#039;create&#039;:\r\n            create_snapshot(app)\r\n        elif action == &#039;cleanup&#039;:\r\n            cleanup_snapshot(app)\r\n        else:\r\n            print(&quot;Wrong action: &quot; + str(action))\r\n    else:\r\n        print(&quot;This script for create and cleanup snapshots&quot;)\r\n        print(&quot;Usage  : python3 &quot; + sys.argv[0] + &quot; {create|cleanup} &quot; + &quot;{application_tag}&quot;)\r\n        print(&quot;Example: python3 &quot; + sys.argv[0] + &quot; create &quot; + &quot;ebs-snapshot-check&quot;)\r\n\r\nif __name__ == &#039;__main__&#039;:\r\n    main()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This script looks for an EBS in the region &quot;eu-west-1&quot; with a tag whose key is &quot;Application&quot; and the value is passed as an argument, creating a snapshot of this EBS. In the same way, it searches for a snapshot by tag and deletes everything except the last one. An example of running to create &hellip; <a href=\"https:\/\/artem.services\/?p=2270&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;\u00a0Python &#8212; AWS EBS creating snapshots based on a tag and keeping only one latest version&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[599],"tags":[543,1441,1851,1527,1529,1853],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2270"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2270"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2270\/revisions"}],"predecessor-version":[{"id":2275,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2270\/revisions\/2275"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}