{"id":3248,"date":"2018-04-26T22:03:13","date_gmt":"2018-04-26T22:03:13","guid":{"rendered":"http:\/\/pariswells.com\/blog\/?p=3248"},"modified":"2022-12-02T01:57:06","modified_gmt":"2022-12-02T01:57:06","slug":"aws-lambda-function-to-list-all-volumes-in-aws-user-id-before-backup","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/code\/aws-lambda-function-to-list-all-volumes-in-aws-user-id-before-backup","title":{"rendered":"AWS Lambda Function to List all Volumes in AWS User ID before Backup"},"content":{"rendered":"\n<p><\/p>\n\n\n<div class=\"wp-block-wab-pastacode\">\n\t<div class=\"code-embed-wrapper\"> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">import boto3<br\/><br\/>def lambda_handler(event, context):<br\/># TODO implement<br\/>userID=&#039;%%%%%%%&#039; # redundancy user account ID<br\/>ec2 = boto3.client(&#039;ec2&#039;)<br\/>volumes = ec2.describe_volumes()<br\/>ec2Resource=boto3.resource(&#039;ec2&#039;)<br\/>for volume in volumes[&#039;Volumes&#039;]:<br\/>for attachment in volume[&#039;Attachments&#039;]:<br\/>instance=ec2Resource.Instance(attachment[u&#039;InstanceId&#039;])<br\/>instanceName=instance.tags[0][u&#039;Value&#039;]<br\/>print &quot;Backing up %s in %s&quot; % (volume[&#039;VolumeId&#039;], volume[&#039;AvailabilityZone&#039;])<br\/>break<br\/>snapshots = ec2.describe_snapshots(Filters=[{ &#039;Name&#039;: &#039;owner-id&#039;,&#039;Values&#039;:[userID] }])<br\/>for snapshot in snapshots[&#039;Snapshots&#039;]:<br\/>print &quot;Backing up %s in %s&quot; % (instanceName, snapshot[&#039;SnapshotId&#039;], volume[&#039;AvailabilityZone&#039;])<\/code><\/pre> <div class=\"code-embed-infos\"> <a href=\"https:\/\/github.com\/pariswells\/public-code\/blob\/master\/\/AWSLamdaListVolumes.py\" title=\"See AWSLamdaListVolumes.py\" target=\"_blank\" class=\"code-embed-name\">AWSLamdaListVolumes.py<\/a> <a href=\"https:\/\/raw.github.com\/pariswells\/public-code\/master\/\/AWSLamdaListVolumes.py\" title=\"Back to AWSLamdaListVolumes.py\" class=\"code-embed-raw\" target=\"_blank\">view raw<\/a> <\/div> <\/div><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[2242,2437,2436],"class_list":["post-3248","post","type-post","status-publish","format-standard","hentry","category-code","tag-aws","tag-lamda","tag-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=3248"}],"version-history":[{"count":4,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/3248\/revisions"}],"predecessor-version":[{"id":6445,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/3248\/revisions\/6445"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}