Created
March 22, 2016 19:32
-
-
Save gabrielferreira/6df33e9cafa4d84005bf to your computer and use it in GitHub Desktop.
Script to create EBS Snapshots from Instances identified by Tags
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 collections | |
import datetime | |
ec = boto3.client('ec2',region_name='us-east-1') | |
def lambda_handler(event, context): | |
reservations = ec.describe_instances( | |
Filters=[ | |
{'Name': 'tag-key', 'Values': ['Identification']}, | |
] | |
).get( | |
'Reservations', [] | |
) | |
instances = sum( | |
[ | |
[i for i in r['Instances']] | |
for r in reservations | |
], []) | |
print "Found %d instances that need backing up" % len(instances) | |
to_tag = collections.defaultdict(list) | |
for instance in instances: | |
for dev in instance['BlockDeviceMappings']: | |
if dev.get('Ebs', None) is None: | |
continue | |
vol_id = dev['Ebs']['VolumeId'] | |
print "Found EBS volume %s on instance %s" % ( | |
vol_id, instance['InstanceId']) | |
snap = ec.create_snapshot( | |
VolumeId=vol_id, | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment