How to Automate AMI Backups & Cleanups, using AWS Lambda (Serverless), with EC2 Tags
Is your Dev-ops or Infrastructure management team looking for options to cut costs and save time by eliminating human errors and the need for dedicated resources (like a standalone server) executing all tasks? Look no further! Automating AMI backups and cleanups using AWS Lambda helps you solve the above predicament to the T. We’ve seen it put to good use, and thought we’ll bring it to you in this step-by-step guide. So, let’s get started!
What is AWS Lambda?As Wikipedia says, AWS Lambda is an event-driven, serverless computing platform provided by Amazon Web Services. Introduced in 2014 by AWS, Lambda simplifies the process of building smaller, on-demand applications that are responsive to events and new information. It runs code in response to events and automatically manages compute resources required by the code. You can start a Lambda instance within milliseconds! To top it all, it supports Node.js, Python and Java, as of 2016.Why do I need AMI Backups and Cleanups?AMI makes it easier and faster to recover an instance in case of a disaster or failure of the instance, and therefore, automating this process is the way to go. In this blog-post, let me take you through the steps involved in automating the AMI backups and cleanups using AWS Lambda. The process, generally comprises of the following steps:
1. Setup IAM Permissions 2. Create Lambda Backup Function 3. Create Lambda Cleanup Function 4. Schedule Functions 5. Tagging EC2 InstanceLet’s now take a closer look at each of them with some demos and screenshots to make it easier.
1. SETUP IAM PERMISSIONSLogin to your AWS Management console, Go to Services, and click on IAM under Security & Identity.In IAM Dashboard, Click on Roles, and Create New Role with the Role Name: lamda-ec2-ami-role. Under AWS Service Roles, select AWS Lambda as the Role Type and then proceed to create a role. Go to Policies tab, click Create Policy and select Create your own policy (you can name the policy as lamda-ec2-ami-policy). Paste the content of the following JSON in Policy Document, and click on Create Policy. Select the created policy, click on Policy Actions and Attach to select the role already created - lamda-ec2-ami-role and click on Attach Policy.We have just created a role for which we have allowed permissions to EC2 instances and view logs in Cloudwatch:
2. CREATE LAMBDA BACKUP FUNCTIONNow that we have created a role and a policy, we’ll have to create the first function that allows us to backup every instance in our account, which has a "Backup" key tag. We don’t have to indicate a value here. Here’s how the AMI backup script works:
- The script will first search for all ec2 instances having a tag with "Backup” on it.
- As soon as it has the instances list, it loops through each instance and then creates an AMI of it.
- Also, it will look for a "Retention" tag key which will be used as a retention policy number in days. If there is no tag with that name, it will use a 7 days default value for each AMI.
- After creating the AMI it creates a "DeleteOn" tag on the AMI indicating when it will be deleted using the Retention value and another Lambda function.
- Click on Functions Menu on the left, and click on Create a Lambda Function
- Select Blank Function and proceed with lambda
- Give a name for it - lambdaAMIBackups
- Select Python 2.7 as a Runtime option * You’ll have to write a code next. Refer this github page for a sample code
- Select the previously created IAM role (lamda-ec2-ami)
- Click Next and Create Function
3. CREATE LAMBDA CLEANUP FUNCTIONHaving successfully created the AMI using the previous function, we need to now remove them when not needed anymore. Here’s how the AMI cleanup script works:
- The script first searches for all ec2 instances having a tag with "Backup” on it.
- As soon as it has the instances list, it loops through each instance and reference the AMIs of that instance.
- It checks that the latest daily backup succeeded then it stores every image that's reached its DeleteOn tag's date for deletion.
- It then loops through the AMIs, de-registers them and removes all the snapshots associated with that AMI.
4. SCHEDULE FUNCTIONSWe need to run at least once a day both. Login to your AWS Management console, Go to Services, and click on Lambda under Compute.
- Click on Functions Menu on the left, and click on the function you want to schedule - lambdaAMIBackups for example.
- Go to Triggers tab and click on Add Trigger
- Select Cloudwatch Events - Schedule
- Type Rule Name ( create-ami for example)
- Go to Schedule Expression: select cron and modify accordingly with the schedule time.
- Check Enable Trigger
- Click Submit
5. TAGGING EC2 INSTANCEHaving created AMI backup and cleanup functions and scheduling them, now it’s time to create a tag for the EC2 instance with a tag-key Backup with no value and Retention with retention days. Login to your AWS Management console, Go to Services, and click on EC2 under Compute.
- Click on Instances menu
- Select the Instance you want to tag (Linux-test, for example).
- Go to Tags >> Add Tags and add a tag with Backup with no value and with Retention value 4, for example.
Is there a limitation?Well, off-course. The process works very well for all standalone instances, unless the instance terminates. In cases where you might have a load balancer serving many instances, the tag attached to one instance may terminate for some reason, therefore, not creating the AMI. However, there’s a work-around using ELBs. It fetches all the load balancers with pre-defined tags (Backup for example), puts them in an array and passes them through a loop, before picking one instance attached to it, to create the AMI. This and a whole lot more in our forthcoming blogpost, but until then, please keep the comments and feedback coming!
Nagarjuna D. N is System Administrator with BluePi Consulting.