Last active
September 21, 2016 04:18
-
-
Save kasng/af5fae543a7ceb0a4140fdc2edb246f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import re | |
import datetime | |
ec = boto3.client('ec2') | |
iam = boto3.client('iam') | |
""" | |
This function looks at *all* snapshots that have a "DeleteOn" tag containing | |
the current day formatted as YYYY-MM-DD. This function should be run at least | |
daily. | |
""" | |
def lambda_handler(event, context): | |
account_ids = list() | |
try: | |
""" | |
You can replace this try/except by filling in `account_ids` yourself. | |
Get your account ID with: | |
> import boto3 | |
> iam = boto3.client('iam') | |
> print iam.get_user()['User']['Arn'].split(':')[4] | |
""" | |
iam.get_user() | |
except Exception as e: | |
# use the exception message to get the account ID the function executes under | |
account_ids.append(re.search(r'(arn:aws:sts::)([0-9]+)', str(e)).groups()[1]) | |
delete_on = datetime.date.today().strftime('%Y-%m-%d') | |
filters = [ | |
{'Name': 'tag-key', 'Values': ['DeleteOn']}, | |
{'Name': 'tag-value', 'Values': [delete_on]}, | |
] | |
snapshot_response = ec.describe_snapshots(OwnerIds=account_ids, Filters=filters) | |
for snap in snapshot_response['Snapshots']: | |
print "Deleting snapshot %s" % snap['SnapshotId'] | |
ec.delete_snapshot(SnapshotId=snap['SnapshotId']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment