Skip to content

Commit 0120427

Browse files
authored
Merge pull request #2463 from Divya-Vijendra-Girase/divyavg-feature-ami-recycle-bin
New serverless pattern - Lambda Recycle Bin
2 parents c4fd87a + 3bdfa25 commit 0120427

File tree

9 files changed

+609
-0
lines changed

9 files changed

+609
-0
lines changed

ami-recycle-bin/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# AMI de-registration with AWS Lambda and retention in Amazon EC2 Recycle Bin
2+
3+
## Description
4+
5+
In this pattern an Amazon EventBridge rule triggers an AWS Lambda function which deregisters an Amazon Machine Image (AMI), deletes the associated snapshot and moves them to the Recycle Bin for retention.
6+
7+
The template creates all the necessary resources including an Amazon EventBridge Rule that triggers the AWS Lambda function once every day. Additionally, Recycle Bin rules for AMI and EBS Snapshots are created to retain deleted resources matching the resources for a retention period.
8+
9+
The AWS Lambda function automates the expiration of Amazon Machine Images (AMIs) by moving the AMIs and their associated snapshots to Recycle Bin. Recycle Bin is a feature of Amazon Elastic Compute Cloud (EC2) that allows you to retain AMIs that you have de-registered for a specified retention period, providing an opportunity to recover them if needed. To recover the deleted AMI, its associated snapshot should be recovered first. The Lambda function also adds corresponding tags to both, the AMI and the EBS snapshot, before moving them to Recycle Bin.
10+
11+
12+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/ami-recycle-bin
13+
14+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
15+
16+
## Requirements
17+
18+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
19+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
20+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
21+
* [Terraform Installed](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) Required Terraform version >= 4.61.0
22+
23+
## Pre-requisite for Testing
24+
An AMI to deregister that has the following Tags:
25+
26+
| Key | Value |
27+
| -------- | ------- |
28+
| Expire-After | Date in Zulu format (e.g. 2024-08-30T17:39:00Z) |
29+
| Same value as <i>resource_tag_key</i> in <i>src/variables.tf</i> | Same value as <i>resource_tag_value</i> in <i>src/variables.tf</i> |
30+
31+
32+
## Deployment Instructions
33+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
34+
```
35+
git clone https://github.com/aws-samples/serverless-patterns
36+
```
37+
2. Change directory to the pattern directory:
38+
```
39+
cd ami-recycle-bin
40+
```
41+
3. Bootstrap the input variables in the following file that are used in the Terraform configuration
42+
```
43+
src/variables.tf
44+
```
45+
4. Initialize a new or existing Terraform working directory by downloading required provider plugins and modules
46+
```
47+
terraform init
48+
```
49+
5. Create an execution plan that shows the changes Terraform will make to your infrastructure based on the current configuration files
50+
```
51+
terraform plan
52+
```
53+
6. Apply the changes defined in the Terraform configuration to the infrastructure. Provide the prompts as required
54+
```
55+
terraform apply
56+
```
57+
58+
## How It Works
59+
60+
Following is the architectural diagram to demonstrate how the pattern works:
61+
62+
![alt text](src/ami-recycle-bin.png)
63+
64+
1. An Amazon Eventbridge rule is configured to run daily (on a schedule) with AWS Lambda function as a target
65+
2. The Lambda function performs the following:
66+
- Verifies that Recycle Bin rules matching the `resource_tag_key` and `resource_tag_value` as bootstraped in the `variables.tf` exists
67+
- Filters AMIs matching the `resource_tag_key` and `resource_tag_value` and contains the `Expire-After` tag
68+
- Determines whether any of the filtered AMI are expired using the `Expire-After` tag
69+
- Tags the expired AMI with its Snapshot Id and the associated snapshot with the AMI Id if required for recovery
70+
- Deprecates the expired AMI and deletes its associated snapshot
71+
72+
## Testing
73+
74+
1. Verify the the expired AMI and its snapshot is retained in the Recycle Bin after deletion
75+
2. Verify the AMI and its snapshot can be recovered after deletion before the retention period ends
76+
77+
## Cleanup
78+
79+
Delete the resources
80+
81+
```
82+
terraform destroy
83+
```
84+
----
85+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
86+
87+
SPDX-License-Identifier: MIT-0
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"title": "AMI de-registration with AWS Lambda and retention in Amazon EC2 Recycle Bin",
3+
"description": "This project demonstrates a pattern to deregister and retain expired AMI and its snapshot with AWS Lambda and Amazon EC2 Recycle Bin",
4+
"language": "",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"In this pattern an Amazon EventBridge rule triggers an AWS Lambda function which deregisters an Amazon Machine Image (AMI), deletes the associated snapshot and moves them to the Recycle Bin for retention.",
11+
"The AWS Lambda function automates the expiration of Amazon Machine Images (AMIs) by moving the AMIs and their associated snapshots to Recycle Bin. Recycle Bin is a feature of Amazon Elastic Compute Cloud (EC2) that allows you to retain AMIs that you have de-registered for a specified retention period, providing an opportunity to recover them if needed. To recover the deleted AMI, its associated snapshot should be recovered first. The Lambda function also adds corresponding tags to both, the AMI and the EBS snapshot, before moving them to Recycle Bin."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/ami-recycle-bin",
17+
"templateURL": "serverless-patterns/ami-recycle-bin",
18+
"projectFolder": "ami-recycle-bin",
19+
"templateFile": "main.tf"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Recover deleted Amazon EBS snapshots and EBS-backed AMIs with Recycle Bin",
26+
"link": "https://docs.aws.amazon.com/ebs/latest/userguide/recycle-bin.html"
27+
}
28+
]
29+
},
30+
"deploy": {
31+
"text": [
32+
"terraform init",
33+
"terraform plan",
34+
"terraform apply"
35+
]
36+
},
37+
"testing": {
38+
"text": [
39+
"See the Github repo for detailed testing instructions."
40+
]
41+
},
42+
"cleanup": {
43+
"text": [
44+
"terraform destroy"
45+
]
46+
},
47+
"authors": [
48+
{
49+
"name": "Divya Vijendra Girase",
50+
"image": "https://avatars.githubusercontent.com/u/172667506?v=4",
51+
"bio": "I am a Cloud Infrastructure Architect at AWS and I work with our strategic customers to build, run and maintain their infrastructure on AWS.",
52+
"linkedin": "divya-girase"
53+
}
54+
],
55+
"patternArch": {
56+
"icon1": {
57+
"x": 20,
58+
"y": 50,
59+
"service": "eventbridge",
60+
"label": "Amazon EventBridge rule"
61+
},
62+
"icon2": {
63+
"x": 50,
64+
"y": 50,
65+
"service": "lambda",
66+
"label": "AWS Lambda"
67+
},
68+
"icon3": {
69+
"x": 80,
70+
"y": 50,
71+
"service": "ec2",
72+
"label": "Amazon EC2"
73+
},
74+
"line1": {
75+
"from": "icon1",
76+
"to": "icon2",
77+
"label": ""
78+
},
79+
"line2": {
80+
"from": "icon2",
81+
"to": "icon3",
82+
"label": ""
83+
}
84+
}
85+
}

ami-recycle-bin/data.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# data source to lookup information about the current AWS partition in which Terraform is working
2+
data "aws_partition" "current" {}
3+
4+
# data source to get the access to the effective Account ID, User ID, and ARN in which Terraform is authorized
5+
data "aws_caller_identity" "current" {}
6+
7+
# data source to obtain the name of the AWS region configured on the provider
8+
data "aws_region" "current" {}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"title": "AMI de-registration with AWS Lambda and retention in Amazon EC2 Recycle Bin",
3+
"description": "This project demonstrates a pattern to deregister and retain expired AMI and its snapshot with AWS Lambda and Amazon EC2 Recycle Bin",
4+
"language": "",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"In this pattern an Amazon EventBridge rule triggers an AWS Lambda function which deregisters an Amazon Machine Image (AMI), deletes the associated snapshot and moves them to the Recycle Bin for retention.",
11+
"The AWS Lambda function automates the expiration of Amazon Machine Images (AMIs) by moving the AMIs and their associated snapshots to Recycle Bin. Recycle Bin is a feature of Amazon Elastic Compute Cloud (EC2) that allows you to retain AMIs that you have de-registered for a specified retention period, providing an opportunity to recover them if needed. To recover the deleted AMI, its associated snapshot should be recovered first. The Lambda function also adds corresponding tags to both, the AMI and the EBS snapshot, before moving them to Recycle Bin."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/ami-recycle-bin",
17+
"templateURL": "serverless-patterns/ami-recycle-bin",
18+
"projectFolder": "ami-recycle-bin",
19+
"templateFile": "main.tf"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Recover deleted Amazon EBS snapshots and EBS-backed AMIs with Recycle Bin",
26+
"link": "https://docs.aws.amazon.com/ebs/latest/userguide/recycle-bin.html"
27+
}
28+
]
29+
},
30+
"deploy": {
31+
"text": [
32+
"terraform init",
33+
"terraform plan",
34+
"terraform apply"
35+
]
36+
},
37+
"testing": {
38+
"text": [
39+
"See the Github repo for detailed testing instructions."
40+
]
41+
},
42+
"cleanup": {
43+
"text": [
44+
"terraform destroy"
45+
]
46+
},
47+
"authors": [
48+
{
49+
"name": "Divya Vijendra Girase",
50+
"image": "https://avatars.githubusercontent.com/u/172667506?v=4",
51+
"bio": "I am a Cloud Infrastructure Architect at AWS and I work with our strategic customers to build, run and maintain their infrastructure on AWS.",
52+
"linkedin": "divya-girase"
53+
}
54+
]
55+
}

0 commit comments

Comments
 (0)