Creating snapshots from EBS drives attached to your EC2 instances is the most basic way of backing up your data. While you have to be cautious when snapshotting running EC2 instances without restart, doing it regularly is a base of many disaster recovery plans. In the latest update to aws-maintenance repo on GitHub you’ll find a complete code and CloudFormation template that will make this as painless a process as possible.
The code itself is fairly simple. A Lambda is triggered daily by a CloudWatch Rule, and will list all EC2 instances with a tag called “Backup” (name can be customized). This tag can hold a number of days that the snapshots made from this instance will be kept for. Lambda will get all EBS volumes attached to such instance, create their snapshots and add “DeleteOn” (again, name can be customized) tag with a date when this snapshot is to be deleted. It will then list all snapshots it created previously and delete those which date within “DeleteOn” has passed.
Apart from the “DeleteOn” tag, all snapshots made by this Lambda will retain all tags that were assigned to the EC2 instance at the time (except “Backup”) - so if you use tags to associate resources to a specific project or cost centre, those will be retained within the snapshots. The snapshots will also have a “CreatedBy” tag added, which will hold the name of the Lambda function - so you can always trace which were created by this code.
The “Backup” tag on the instance should contain the number of days you want to hold the snapshots for. If you use 0 ( zero), the default defined within the code will be used (7 days).
You can modify the names of “Backup” and “DeleteOn” tags by changing the values of variables defined at the top of the code. Same applies to the default retention period. See README in repo for details.
Full details on how to use the CloudFormation template can be found in the README .