
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
AWS lambda function script template for amazon web services
Typology: Study notes
1 / 1
This page cannot be seen from the preview
Don't miss anything!

from future import print_function
import json, boto
def lambda_handler(event, context): print("Received event: " + json.dumps(event, indent=2))
message = event['Records'][0]['Sns']['Message'] autoscalingInfo = json.loads(message) ec2InstanceId = autoscalingInfo['EC2InstanceId']
ec2 = boto3.resource('ec2') for v in ec2.volumes.filter(Filters=[{'Name': 'attachment.instance-id', 'Values': [ec2InstanceId]}]): description = 'Autosnap-%s-%s' % ( ec2InstanceId, v.volume_id )
if v.create_snapshot(Description = description): print("\t\tSnapshot created with description [%s]" % description)
ec2 = boto3.client('ec2') response = ec2.create_tags( Resources=[ec2InstanceId], Tags=[{'Key': 'Snapshots', 'Value': 'Created'}] ) print ("***Tag added to EC2 instance with id: " + ec2InstanceId)
return ec2InstanceId