Create AWS Lifecycle Policy Using Terraform for Taking EBS Snapshots

April 1, 2020 | Comments(0) |

In this blog, we are going to discuss about AWS service Data Lifecycle Manager, which helps you to take snapshots of AWS EBS volumes, retain them for number of days and also delete the outdated backups. The advantages of using this service are:

  1. It’s automated.
  2. Protection of valuable data by enforcing regular data backups.
  3. Cost saving by deleting outdated backups automatically.

You will find this service on the EC2 dashboard, under the Elastic Block Store menu.

Create AWS Lifecycle Policy Using Terraform for Taking EBS Snapshots
Figure 1: EC2 dashboard>EBS>Lifecycle Manager

 

Create AWS Lifecycle Policy Using Terraform for Taking EBS Snapshots
Figure 2: Data Lifecycle Manager

 

If you go by the manual method, you will be asked to fill some information and it will create the policy for you. But here, we are going to create this entire policy using Terraform. It’s an amazing open-source ‘infrastructure as code’ (IaC) tool which can be used to deploy your infrastructure efficiently. What it means is, you run a Terraform code from your local computer and the code will deploy instances and other resources for you automatically.

Terraform should have access to your AWS infrastructure for the code to work. For this blog, I am assuming that you know how to configure Terraform and provide AWS credentials to it. I will be using Visual Studio Code as a code editor here, but you can use any other editor as per your convenience.

Let’s have a look at the Terraform script that we are going to use.

First, we will create an IAM role and attach a policy to it. This policy requires permissions to do operations on EC2, which are create snapshots, delete snapshots, describe volumes and describe snapshots.  Additionally, it should have the permission to create tags on the snapshot volumes.

The creation of the DLM (Data Lifecycle Manager) policy starts from line 50 in the GitHub gist script which you can see above. You can also find it on the GitHub repository here.

Create AWS Lifecycle Policy Using Terraform for Taking EBS Snapshots

Even though the Terraform script is self-explanatory, I have added some details for a better understanding for people who are new to Terraform scripting. The latter part of the code with ‘create_rule’ module, specifies the interval between snapshots and the time of running the snapshots. For this one, I am running the policy at 11:45 PM every 24 hrs.

Below is the last part of the Terraform code which shows you how to setup the retention policy of the snapshots taken, what tags to give and which volumes to target while taking snapshots.

Create AWS Lifecycle Policy Using Terraform for Taking EBS Snapshots

Steps to follow in Visual Studio Code for running this Terraform script:

  1. Create a directory named ‘aws_lifecyclepolicy’ in your Terraform workspace.
  2. Create 2 tf files. Name one as main.tf and the other as provider.tf
  3. In the provider.tf file, include details as provider:aws and the region as well.
  4. In the main.tf file, write the above given terraform script.
  5. Run a ‘terraform init’ command to initialize Terraform in the directory.
  6. Run the command ‘terraform plan’ to check the script and see what resources will be created once we run it. Note that, no resources will be created if you run this command. This is just to check if the script is correct and what resources it will create.
  7. If everything looks good in step 6, run the command ‘terraform apply’ and type ‘yes’ when it prompts.
  8. Go to the AWS service Lifecycle Manager after this and you will see your policy there.

I hope this blog has helped you understand how to write a Terraform script for a lifecycle policy for creating snapshots in AWS.

To learn more about AWS Courses visit: cloudthat.in.

If you have any comments or questions, then do write it in the comment.


Leave a Reply