Skip to content

Commit e10201c

Browse files
author
Dave Osment
committed
Updated mechanism through which current Account Id is acquired
1 parent 237a22d commit e10201c

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

ebs-snapshot-manager.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import datetime
44

55
ec = boto3.client('ec2')
6-
iam = boto3.client('iam')
76

87
"""
98
This function looks at *all* snapshots that have a "DeleteOn" tag containing
@@ -12,21 +11,8 @@
1211
"""
1312

1413
def 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

Comments
 (0)