Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New serverless pattern - Lambda Recycle Bin #2463

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Divya-Vijendra-Girase
Copy link

@Divya-Vijendra-Girase Divya-Vijendra-Girase commented Sep 2, 2024

Issue #2464:

Description of changes:
In this pattern an Amazon EventBridge trigger AWS Lambda function to deregister AMI, delete the associated snapshot and move them to Recycle Bin for retention. The infrastructure is in terraform

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@bfreiberg
Copy link
Contributor

Thank you for submitting this request. Your PR does not adhere to our publishing guidelines. It is missing the example-pattern.json file for example. Additionally, please check in any source code in an unzipped format.

@Divya-Vijendra-Girase
Copy link
Author

Thank you for submitting this request. Your PR does not adhere to our publishing guidelines. It is missing the example-pattern.json file for example. Additionally, please check in any source code in an unzipped format.

Thank you for the review.
Added example-pattern.json

ami-recycle-bin/README.md Show resolved Hide resolved
@@ -0,0 +1,55 @@
{
"title": "Deregister expired AMIs and its snapshot with AWS Lambda and retain them in Amazon EC2 Recycle Bin",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title can have at most 75 characters, please reword

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title can have at most 75 characters, please reword

Updated title to 75 characters AMI de-registration with AWS Lambda and retention in Amazon EC2 Recycle Bin

{
"title": "Deregister expired AMIs and its snapshot with AWS Lambda and retain them in Amazon EC2 Recycle Bin",
"description": "This project demonstrates a pattern to deregister and retain expired AMI and its snapshot with AWS Lambda and Amazon EC2 Recycle Bin",
"language": "YAML",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave this property empty for Terraform

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave this property empty for Terraform

Updated the language property value to be an empty string

@@ -0,0 +1,85 @@
## Description

In this pattern an Amazon EventBridge trigger AWS Lambda function to deregister AMI, delete the associated snapshot and move them to Recycle Bin for retention.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this pattern an Amazon EventBridge trigger AWS Lambda function to deregister AMI, delete the associated snapshot and move them to Recycle Bin for retention.
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as per the suggested changes


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.

AWS Lambda function automates the expiration of Amazon Machine Images (AMIs) moving the AMIs and its associated snapshot to Recycle Bin af. The Recycle Bin is a feature in Amazon Elastic Compute Cloud (EC2) that allows you to retain AMIs that you have deregistered 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 Recyle Bin for recovery.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
AWS Lambda function automates the expiration of Amazon Machine Images (AMIs) moving the AMIs and its associated snapshot to Recycle Bin af. The Recycle Bin is a feature in Amazon Elastic Compute Cloud (EC2) that allows you to retain AMIs that you have deregistered 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 Recyle Bin for recovery.
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as per the suggested changes


resource "aws_cloudwatch_event_rule" "event_rule" {
name = "invoke-lambda-daily"
description = "Invoke a Lambda function every day"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Invoke a Lambda function every day"
description = "Invoke a Lambda function once per day"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated description to Invoke a Lambda function once per day

function_name = "ami-recycle-lambda"
role = aws_iam_role.lambda_role.arn
handler = "ami-recycle-lambda.lambda_handler"
runtime = "python3.9"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the most recent Python version

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the most recent Python version

Used the most recent supported version python3.12

"RBIN_RETENTION_PERIOD_UNIT" = var.rbin_retention_period_unit
}
}
timeout = 300
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is such a long timeout really necessary?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is such a long timeout really necessary?

If the list of expired AMIs is long, the maximum timeout will be helpful to ensure function does not timeout before the expired AMIs and its snapshot is moved to Recycle Bin. However since this is a test pattern, updated the timeout parameter as a configurable variable and set the default timeout to 15 seconds

"logs:PutLogEvents",
"logs:CreateLogGroup"
]
Resource = "arn:aws:logs:*:*:*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please scope the policy down to the current account and region

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please scope the policy down to the current account and region

Added data.tf file to add data source and scoped down the policy to

  • region and account for CreateLogGroup
  • region, account and function name for CreateLogStream and PutLogEvents

"ec2:DeleteSnapshot",
"rbin:ListRules"
]
Resource = "*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please scope the policy down to the current account and region

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please scope the policy down to the current account and region

  • Scoped down policy for the actions that can be restricted to resources like DeregisterImage and DeleteSnapshot
  • Updated policy statement to separate actions that only support wildcard - DescribeImages and ListRules

@Divya-Vijendra-Girase
Copy link
Author

Hi team - Updated the content as per the feedback. Can you please review this. Thank you!

@bfreiberg
Copy link
Contributor

Hi, thanks for the updates. Could please check in the Lambda function code in an unzipped format to allow a review

@Divya-Vijendra-Girase
Copy link
Author

Hi, thanks for the updates. Could please check in the Lambda function code in an unzipped format to allow a review

Thank you - Added the source code.

@bfreiberg
Copy link
Contributor

Looks good, thanks for your contribution. Your pattern will be merged to Serverlessland.com soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants