Worth-It: Creating AMI/Snapshot Backup With Tags From Its EC2.

Manoj Kumar S
MiQ Tech and Analytics
3 min readAug 29, 2019

Introduction

This blog assumes you are familiar with AWS EC2 Concepts and know how AWS Lambda functions work. For those who are not, please refer to the documentation here.

Here we discuss some of the key benefits of the script that we use in our Lambda to create a backup of critical EC2 instances.

Problem Statement

AWS by itself has EC2 instance backup options through AMI/Snapshot creation and let’s say you have more than 50 instances which need to be backed up along with proper tags. These AMI’s needs to be deleted after a Period of time and doing that manually through the console every day is not a feasible option. even if 2 to 5 instances need to be backed up, doing it manually is never a good idea as human intervention can seldom be error-prone.

There are several Lambda scripts out there on the internet which allow us to automate these tasks of backup through Lambda, but those scripts do not perform tagging of AMI’s and snapshots which are essential for meaningful archival and cost tracking. So how do we achieve this?

Solution

AWS Lambda is a Server Less component from AWS, which can be employed to automate a lot of operational tasks. But here we focus only on the script we used for automating AMI/Snapshots backup along with the requisite tagging

As an organization , we have to keep track of costs incurred by AWS resources and tagging the resources enables us to identify cost utilization for each Team or even Product/Module based on the priority of the task. So tags are important to get proper data and this has to be done for backed up AMI’s/Snapshots as well. The plethora of pre-existing scripts online do not necessarily provide the ability to tag resources during the back-up runs. Hence we created a custom script that would allow us to propagate the tags from the EC2 instance to its AMI and the pertinent snapshots while creating the images during the Lambda run.

How can you make use of the script?

  • Please download python script below which can be used in your AWS Lambda Service to take backups of EC2 along with the tags associated to it.

Github link for complete details

  • The script is generalized and can be run through the Cron scheduler. You can reconfigure or modify the script to make it compatible with the Lambda function, please refer to the ReadMe which has pointers on the same.
  • Please make sure that you have all the prerequisites mentioned in the ReadMe in order to run the script in your environment, it also helps you to know how script identifies instances which are supposed to be backed up.
  • This script can also post slack notification in case of any exceptions while creating backups and if you don’t have slack? No worries! You can still make it work just by changing arguments when you run, examples given in git repository will help you understand.

Summary

This particular script is used to create backups of our critical EC2 instances while effectively segregating the AMI/ Snapshots costs for each Product/Team.

--

--