Skip to content

Instantly share code, notes, and snippets.

@libert-xyz
Created October 26, 2017 22:30
Show Gist options
  • Save libert-xyz/71c5bede345a93c58fd2afd8ba9e0bca to your computer and use it in GitHub Desktop.
Save libert-xyz/71c5bede345a93c58fd2afd8ba9e0bca to your computer and use it in GitHub Desktop.
Create Snapshots with AWS Lambda
# Backup all in-use volumes in all regions
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
# Get list of regions
reg = 'us-east-1'
# Connect to region
ec2 = boto3.client('ec2', region_name=reg)
# Get Instance
instance = ec2.describe_instances(Filters=[{'Name':'tag:Name','Values':['RT']}])
# Get volume ID
volume_id = instance['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']
print ("Backing up %s" %(volume_id) )
# Create snapshot
result = ec2.create_snapshot(VolumeId=volume_id,Description='Created by Lambda backup function ebs-snapshots')
# Get snapshot resource
ec2resource = boto3.resource('ec2', region_name=reg)
snapshot = ec2resource.Snapshot(result['SnapshotId'])
# Add volume name to snapshot for easier identification
snapshot.create_tags(Tags=[{'Key': 'Name','Value': 'RT'}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment