Skip to content

Instantly share code, notes, and snippets.

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 jonathan-kosgei/793a6998c4aca677af98e8cb6bc7c6ca to your computer and use it in GitHub Desktop.
Save jonathan-kosgei/793a6998c4aca677af98e8cb6bc7c6ca to your computer and use it in GitHub Desktop.
""" Make sure to create the "backup" tag on the volumes you want to backup.
For authentication, setup the aws policy and user as specified in the snapshot-trust.json and snapshot-policy.json
Inspired by: https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups/
"""
from time import gmtime, strftime
import boto3
region = "us-west-2"
backup_tag = "backup"
client = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
volumes = client.describe_volumes(Filters=[{"Name":"tag-key", "Values":[backup_tag]}])
for volume in volumes["Volumes"]:
time = strftime("%a, %d %b %Y %X +0000", gmtime())
snapshot = ec2.create_snapshot(VolumeId=volume["VolumeId"], Description='EBS Backup {0}'.format(time))
print "Created : {0} ID: {1}".format(snapshot.description, snapshot.snapshot_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment