33import datetime
44
55ec = boto3 .client ('ec2' )
6- iam = boto3 .client ('iam' )
76
87"""
98This function looks at *all* snapshots that have a "DeleteOn" tag containing
1211"""
1312
1413def lambda_handler (event , context ):
15- account_ids = list ()
16- try :
17- """
18- You can replace this try/except by filling in `account_ids` yourself.
19- Get your account ID with:
20- > import boto3
21- > iam = boto3.client('iam')
22- > print iam.get_user()['User']['Arn'].split(':')[4]
23- """
24- iam .get_user ()
25- except Exception as e :
26- # use the exception message to get the account ID the function executes under
27- account_ids .append (re .search (r'(arn:aws:sts::)([0-9]+)' , str (e )).groups ()[1 ])
28-
29-
14+ account_ids = (boto3 .client ('sts' ).get_caller_identity ()['Account' ])
15+
3016 delete_on = datetime .date .today ().strftime ('%Y-%m-%d' )
3117 # limit snapshots to process to ones marked for deletion on this day
3218 # AND limit snapshots to process to ones that are automated only
@@ -35,7 +21,7 @@ def lambda_handler(event, context):
3521 { 'Name' : 'tag:DeleteOn' , 'Values' : [delete_on ] },
3622 { 'Name' : 'tag:Type' , 'Values' : ['Automated' ] },
3723 ]
38- snapshot_response = ec .describe_snapshots (OwnerIds = account_ids , Filters = filters )
24+ snapshot_response = ec .describe_snapshots (OwnerIds = [ account_ids ] , Filters = filters )
3925
4026 for snap in snapshot_response ['Snapshots' ]:
4127 for tag in snap ['Tags' ]:
@@ -50,4 +36,4 @@ def lambda_handler(event, context):
5036 # do nothing else
5137 else :
5238 print "Deleting snapshot %s" % snap ['SnapshotId' ]
53- ec .delete_snapshot (SnapshotId = snap ['SnapshotId' ])
39+ ec .delete_snapshot (SnapshotId = snap ['SnapshotId' ])
0 commit comments