Created
August 11, 2016 22:02
-
-
Save rhossi/5b3b457b07f0a2c4df26575a9464b1fd to your computer and use it in GitHub Desktop.
Lambda function to create AMIs of EC2 instances
This file contains hidden or 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 | |
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