Web Hosting

Create EC2 AMI Backup Using Lambda Function

Written by Alex

Introduction

We are here to learn to Create EC2 instance AMI backup using lambda function. AMI backup means Amazon Machine Image. To automate AMI backup using lambda function which is part of backup and recovery procedure you have to use some AWS services like IAM, Lambda, EC2.

With the help of this post, you will be able to automate AMI backup using lambda function.

EC2 instance AMI backup using lambda function.

You need to follow the below process to create AMI backup. AWS Lambda service required to describe instance, create/de-register images, and delete snapshot access. However you can do it by login AWS management console and go IAM service console

Create role and policy

1. Click on the policy on the left panel.

Create EC2 instance AMI backup using lambda function

2. Click on ‘Create policy’, then choose the JSON tab and delete sample code & paste the below JSON data.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ec2:DeleteSnapshot",
                "ec2:CreateSnapshots",
                "ec2:CreateSnapshot"
            ],
            "Resource": [
                "arn:aws:ec2:*:*:instance/*",
                "arn:aws:ec2:*::snapshot/*",
                "arn:aws:ec2:*:*:volume/*"
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeImages",
                "ec2:DeregisterImage",
                "ec2:DescribeInstances",
                "ec2:DescribeSnapshotAttribute",
                "ec2:DescribeInstanceAttribute",
                "ec2:DescribeImageAttribute",
                "logs:*",
                "ec2:CreateImage",
                "ec2:DescribeSnapshots",
                "ec2:DescribeInstanceCreditSpecifications",
                "ec2:DescribeInstanceStatus"
            ],
            "Resource": "*"
        }
    ]
}

3. Click on the review policy and give the name of the policy.

4. Select create policy.

5. Go back to the IAM console click on on the “Role” left side panel.

6. Create a role and select “Lambda” then click on next

7. On the policy, search box search your policy which you created earlier

8. Select next: Tags enter tag name if you need otherwise click on “Next: Review”.

9. Give any name to your role then select on “create role”.

Create Lambda Function for AMI backup

Now you have done above all steps and the next step we will create a lambda function for AMI backup and delete the backup.

1. Go to the lambda service in the AWS management console.

Create EC2 instance AMI backup using lambda function

2. Go the function and click on create function.

Create EC2 instance AMI backup using lambda function

Create EC2 instance AMI backup using lambda function

3. Fill the details in as per the below screenshot and attach the role that you have created earlier.

Create EC2 instance AMI backup using lambda function

4. Scroll down to the ‘Function code’ delete sample code and copy this code into the blank area.

Create EC2 instance AMI backup using lambda function

5. Scroll down to ‘Environment variables’ and create the environment variables like below.

Create EC2 instance AMI backup using lambda function

6. Set the timeout value to 15 mins; this is the threshold value of execution time for our Lambda function.

Create EC2 instance AMI backup using lambda function

7. Save the changes by clicking on ‘Save’ in the top-right corner, and then click on ‘Test’.

8. Configure the test event. Give then event name: Backup and click on “create”.

Create EC2 instance AMI backup using lambda function

9. Go to the Lambda function and run it by clicking ‘Test’. We can check the output in log output and we can also check in our CloudWatch logs.

10. Check the created AMI Backup in the AMI console and you will be able to see your AMI and also you can see AMI snapshot in snapshot option in the left corner.

11. Go back to the Lambda function. And click on the ‘Add trigger’ in the designer console and select the EventBridge(CloudWatch Events).

12. After that create a new rule for the trigger.

Create Cron expression

13. We will add Cron expression to the Lambda function, this will automatically trigger the function as per the Cron. The Cron expression mentioned in the screenshot is for a daily run at 11 pm. For example about Cron expressions, please refer to AWS Schedule Expressions.

Create EC2 instance AMI backup using lambda function

14. Similarly, Repeat the above process and create a ‘Delete function’ and copy this code to the ‘Function’ code and add ‘Environment variables’ again as per the below screenshot.

15. We can add a retention period as 3 days. In other words, the function scans the list for AMIs which are older than 3 days and deletes them accordingly. We can change the retention to 15 or 30 as per requirement.

16. Enter timeout values & triggers as a create AMI function.

Create EC2 instance AMI backup using lambda function

In Conclusion

Now you are able to Create EC2 AMI Backup Using Lambda and automatically manage AMI Backup and Snapshots.

We hope you are like this article and this is useful for you. If you learn more about Linux, AWS Tips, and Tricks Please Bookmark this Website. We are Daily Publish New Article in this Website WWW.BPMTECHGURU.IN and all post on this website is practically tested and 100% Work if you have any Problem Please Drop a Comment we are trying to resolve your problem.

 

About the author

Alex

Leave a Comment