Created
April 19, 2017 03:45
-
-
Save jonathan-kosgei/793a6998c4aca677af98e8cb6bc7c6ca to your computer and use it in GitHub Desktop.
This file contains 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
""" 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