AWS Cost Optimization using AWS Lambda

·

2 min read

Introduction

In this project, we'll create a Lambda function that identifies empty S3 buckets and deletes them to save on storage costs. Additionally, we'll set up a SNS notification to send an email notification to the user for buckets where versioning was disabled.

Lambda function explanation

The Lambda function fetches all S3 buckets owned by the same account, including details about bucket versioning and objects. If bucket versioning is enabled, the function proceeds to gather object details. If the bucket does not contain any current or versioned objects, the function proceeds to delete the bucket. If versioning is not enabled, then the function sends an email notification to the user, requesting them to enable the versioning or delete the bucket if it is no longer needed.

Project Setup

Step 1: Create a Lambda function with runtime as, Python 3.11

Step 2: Copy the source code from https://github.com/Anandhi-23/AWS/blob/master/aws_lambda_cost_optimization/cost_optimization_s3.py and place it under lambda_function workspace. You can also upload the source code as .zip file or from s3 bucket.

Step 3: Create a SNS topic and subscription. Post adding the subscription, confirm it to generate a successful email notification.

Step 4: Attach necessary policies to the role created for the Lamba function to list buckets, objects, versioning details, delete buckets and publish SNS notifications. You can also give 'AmazonS3FullAccess' and 'AmazonSNSFullAccess' (NOT RECOMMENDED)if you are implementing this project with your personal IAM account and delete the setup once you are done with the project.

Step 5: To test the code click the Deploy button and create a test function to test the lambda function.

Below is the generated mail notification by the Lamba function when the bucket versioning was not enabled.

References

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html

https://www.youtube.com/watch?v=5fTtmeCpSRw&list=PLdpzxOOAlwvLNOxX0RfndiYSt1Le9azze&index=19

https://www.youtube.com/watch?v=OKYJCHHSWb4&list=PLdpzxOOAlwvLNOxX0RfndiYSt1Le9azze&index=20