Created
November 22, 2016 20:57
-
-
Save tuksik/10abb9f8175b2b72e8d4beb796035fcc to your computer and use it in GitHub Desktop.
lambda_Snapshots Example
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 datetime | |
ec2 = boto3.resource('ec2') | |
def lambda_handler(event,handler): | |
print("nnAWS Snapshots starting at %s" % datetime.datetime.now()) | |
instances = ec2.instances.filter(Filters=[ | |
{'Name': 'instance-state-name', 'Values': ['running']} | |
]) | |
for instance in instances: | |
instance_name = filter(lambda tag: tag['Key'] == 'Name', instance.tags)[0]['Value'] | |
print("name: %s -id: %s" % (instance_name , instance.id)) | |
for volume in ec2.volumes.filter(Filters=[ | |
{'Name': 'attachment.instance-id', 'Values': [instance.id]} | |
]): | |
description = 'scheduled-%s.%s-%s' % (instance_name, volume.volume_id, datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) | |
print 'description: %s' % (description) | |
if volume.create_snapshot(VolumeId=volume.volume_id, Description=description): | |
print("Snapshot created with description [%s]" % description) | |
print("nnAWS Snapshots completed at %s" % datetime.datetime.now()) | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment