A Step-by-Step Guide to Building and Deploying a Cost-Saving Lambda Function for EBS Snapshot Management
Welcome to the world of DevOps! Today, we’ll dive into an exciting cost optimisation project that tackles a common challenge faced by AWS users—managing unnecessary EBS snapshots. In this project, we’ll create a Python program that automatically cleans up:
This hands-on project is not only a great way to reduce your AWS storage costs but also an opportunity to strengthen your understanding of resource utilisation in AWS. Along the way, we’ll explore boto3, the AWS SDK for Python, to interact with AWS resources and manipulate them to suit our needs.
Let’s get started on this journey to automate cost-saving measures and enhance your DevOps skills!
Before we jump into building our cost optimization project, let’s make sure you have the necessary tools and knowledge in place. Here’s what you’ll need:
aws configure
This step will set up your access keys, region, and output format to enable seamless communication with AWS services.
Once you’ve ticked off these prerequisites, you’ll be ready to proceed with the project!
The source code for this project is available in my GitHub repository:
? GitHub Repo: AWS Cloud Cost Optimization
Follow these steps to create and configure your Lambda function:
Create a Lambda Function:
Add the Code:
aws configure
Deploy the Code:
Increase Timeout:
Test the Function:
This error occurs because the Lambda function lacks the required permissions, which we will address in the next step.
To enable the Lambda function to interact with EC2 resources, we need to add specific permissions to its service role. Instead of granting full access, we’ll follow the principle of least privilege to ensure the function has only the permissions it requires. Follow these steps:
Locate the Service Role:
Open the Service Role in IAM:
Create an Inline Policy:
Review and Assign the Policy:
The ebs-permissions policy will look like this:
Once this inline policy is added, the Lambda function will have all the permissions it needs to describe and delete snapshots, volumes, and instances effectively.
With the permissions in place, let’s test the function again in the next step!
With the permissions in place, it’s time to test our Lambda function using real-world scenarios. Follow these steps to see how the function performs:
1. First Use-Case: Cleaning Snapshots of Deleted Instances
Create an EC2 Instance:
Create a Snapshot:
Delete the Instance:
Once the snapshot is ready, terminate the test-ebs instance. This leaves behind a snapshot of a non-existent instance, which is now redundant.
Run the Lambda Function:
Verify the deletion in the Snapshots section of the EC2 Dashboard.
2. Second Use-Case: Cleaning Snapshots of Deleted Volumes
Create an EBS Volume:
Create a Snapshot:
Delete the Volume:
Once the snapshot is ready, delete the ebs-volume. This leaves behind a snapshot not attached to any volume.
Run the Lambda Function:
By successfully handling these two scenarios, the Lambda function demonstrates its ability to identify and clean up redundant EBS snapshots, saving costs and streamlining resource management.
Congratulations! ? You’ve just built a cost-optimization solution on AWS using Python and Lambda. In this project, we explored how to identify and clean up redundant EBS snapshots that no longer serve a purpose, helping you save on cloud costs while improving resource management.
Here’s a quick recap of what we achieved:
This project highlights the importance of resource optimization in a cloud environment and equips you with hands-on experience in automating AWS tasks using Python.
Feel free to customize and enhance this function further. For example, you could set up a CloudWatch rule to trigger the function periodically or extend the logic to handle additional cleanup scenarios.
Thank you for following along, and stay tuned for more exciting DevOps and cloud projects! ?
? For more informative blog, Follow me on Hashnode, X(Twitter) and LinkedIn.
Till then, Happy learning! ?
The above is the detailed content of Optimise AWS Costs: Automate Unused EBS Snapshot Cleanup with Lambda. For more information, please follow other related articles on the PHP Chinese website!