Edit : You dont need to follow this guide. AWS has made life simple. Just follow
https://aws.amazon.com/blogs/aws/new-lifecycle-management-for-amazon-ebs-snapshots/
The new life-cycle management policies.
Just keeping this as reference
Taking automatic snapshots of ec2 volumes is currently not as ‘automatic’ as the case with an RDS instance in which we can fully automate it.
However with AWS Ops Automator and CloudFormation, we can come up with a solution to automate the creation / deletion of snapshots.
1.1) First, launch CloudFormation and create a new stack :
1.2) After clicking on create new stack, select the option
Give the URL as : https://s3.amazonaws.com/solutions-reference/ops-automator/latest/ops-automator.template
1.3) Give the stack a name in the next page and specify the paramters below it. Dont forget to give the
1.4) Select the next page, leave the options page as default and click next.
Review the options and tick on
and go ahead with create.
Give 5-10 mins for the stack creation to be completed.
Once complete, you will find
Step 2) Next step would be to launch the task template ( for creating snaps)
To do that,
2.1) Open the s3 console in the account in which the above ops automator stack was created
2.2) There would be 2 buckets created, open the one with the keyword configuration, it would be something like opsworks-configuration-xxx. Open it and click on the configuration folder. Now, select the applicable template. First, we would take a template to create the snapshot every ‘x’ hours.
2.3) So, look for the template Ec2CreateSnapshot.template and copy the Link associated with the file ( template )
2.4) Now, go back to CloudFormation and create another stack.
2.5) In the option,
2.6) Give the stack name, something like createsnap which would help to distinguish from other stacks.
2.7) Fill in the paramters like task description etc, The time interval would be a cron time. To run everyday at 3 am, it would be : 0 3 */1 * *
2.8) Give the paramters like, Regions, Timezone.
2.9) Take a note about ‘
What you set here, would go as the tag which we will add to our ec2-instances.
So, set something like
snapshot-time=3
would mean ‘snapshot-time’ is the tag and its value is 3.
Also, note the Snapshot volume options.
2.10) Dont forget to add “
I added something like : volume-source=auto
So my config looks like :
Go ahead and press Next.
Press Next in the options page without modifying anything,
Review and press create and wait for the stack to be created.
Step 3 ) Next, we would be creating the stack to remove the older snapshots.
3.1) Go to the s3 console as we did with the previous stack, select the configuration folder, this time copy the link of the template –
Ec2DeleteSnapshot.template
3.2) Go to ClouldFormation and select Create new stack, in the select template,
3.3) Give the stack a name and go through the parameters.
3.4) As with the previous stack, give the task interval in cron format.
3.5) The tag filter value here would be the tags that will look in the snapshots to be deleted. In the previous stack, we set the tags for the snapshots to be volume-source=auto ( step 2.10)
So give that value here as the tag filter.
3.6) You can either select
Remember to put 0 in retention days option if you are using retension count and vice-versa.
So, with that the Cloudformation and ops automator are set up. Next is to tag the ec2 which needs the snapshot to be taken
What we had set during the snapshot creation stack was snapshot-time=3 ( step 2.9 )
So, go to the EC2 console – Instances – Select the instance, Actions -> Instance settings – Add/Edit tags.
Add the key – snapshot-time and value as 3.
This should get the snapshot creation at the time we specify and delete it in the way we specify either looking at the retention count / days.