Skip to content

Instantly share code, notes, and snippets.

@rhossi
Created August 11, 2016 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhossi/5b3b457b07f0a2c4df26575a9464b1fd to your computer and use it in GitHub Desktop.
Save rhossi/5b3b457b07f0a2c4df26575a9464b1fd to your computer and use it in GitHub Desktop.
Lambda function to create AMIs of EC2 instances
import boto3
def lambda_handler(event, context):
client = boto3.client('ec2')
filters = [{'Name':'tag:Backup-AMI', 'Values':['yes']}]
instances = client.describe_instances(Filters=filters)
if instances and instances['Reservations']:
for i in instances['Reservations'][0]['Instances']:
instance_id = i['InstanceId']
name = "Image for instance {instance_id}".format(instance_id=instance_id)
print "creating image for instance {instance_id}".format(instance_id=instance_id)
client.create_image(InstanceId=instance_id,Name=name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment