# Automating AWS Cost Optimization: A Lambda Function Approach

In this blog, we delve into the pivotal role of Lambda functions and their profound impact on the tasks of DevOps engineers. Lambda functions, a cornerstone of serverless computing, empower DevOps professionals by streamlining and automating complex tasks. With the ability to run code without the hassle of server management, Lambda functions enhance efficiency, optimize resource utilization, and enable seamless integration into various AWS services. This exploration sheds light on how Lambda functions become an invaluable tool in the toolkit of DevOps engineers, contributing to agile, scalable, and cost-effective cloud operations.

What is Lambda Function?

AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). It allows you to run code without provisioning or managing servers.

Features:

**Serverless Computing**

EventDriven Execution

Pay-as-you-go model

### **Real-Time Use Case :**

**Scenario: Automated Snapshot Cleanup**

**Problem Statement:**

* Suppose you want to ensure efficient resource management in AWS and avoid unnecessary costs associated with unattached EBS (Elastic Block Store) snapshots. This means you created unlimited resources that are inactive but are causing unnecessary costs. So we need to create an automated program that runs periodically and removes/deletes these inactive resources
    

**How does AWS Lambda bring the solution?**

1. **Lambda Function Creation:**
    
    * Develop a Lambda function that identifies and deletes unattached EBS snapshots. This function utilizes the AWS SDK to interact with the AWS EC2 service.
        
2. **CloudWatch Events:**
    
    * Schedule the Lambda function to run at specific intervals using CloudWatch Events.
        
    * For example, you can set it to run daily or weekly to regularly clean up unattached snapshots.
        
3. **Automation and Cost Optimization:**
    
    * This Lambda function, triggered by CloudWatch, automates the cleanup process, ensuring that unattached EBS snapshots are regularly removed.
        
    * This automation optimizes costs by preventing the accumulation of unused storage resources.
        

This explained scenario is our main objective in this project. Let's start the project:

**Create an ec2 instance**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704352233545/09b1a1a1-3a9e-4786-897c-c20493111bf6.png align="center")

Here for this instance, we have attached a volume with id ***vol-0b61a32be947716c3*** with 8GB capacity.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704352370771/27019909-b6a1-4571-b90f-286a4dd49628.png align="center")

Create the Snapshot for Volume.

Go to ec2 dashboard &gt; click "snapshot" &gt; click "create snapshot"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704352627291/9c43c4b9-3561-4a57-84cb-e21d9eb789bc.png align="center")

Here, I've selected a volume and chosen the specific volume ID for which I intend to create a snapshot. Feel free to pick the available volume ID that corresponds to the snapshot you wish to generate.

**Create lambda function**

Search for "Lamda function" in the search bar &gt; click on "lambda" &gt;click "create lambda" then:

> Author from scratch
> 
> Function name: cost-optimize-ebs-snapshot
> 
> Runtime: python 3.12
> 
> architecture: x86\_64
> 
> Permissions: default

Save configuration

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704355011206/d7da5978-af68-4e10-aa82-5434612925d5.png align="center")

Now copy the code for the Lambda from [GitHub](https://github.com/imsubash-devops/python-practice/blob/main/day13/ebs_stale_snapshot.py). Copy the code and paste it into the

editor.

This Python script, utilizing the Boto3 library, defines an AWS Lambda function for the automated cleanup of unattached EBS snapshots. The function fetches all owned snapshots and active EC2 instances and then iterates through the snapshots. Unattached snapshots are promptly deleted, and those attached to volumes not linked to running instances are also removed. Boto3 functions such as `describe_snapshots` and `delete_snapshot` simplify AWS interactions, showcasing efficient automation for cost optimization.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704355128625/ec18ccaf-a5e5-4a8d-ba44-4c20dea6cc43.png align="center")

save the code and deploy. Once it is deployed, click on **test,** and a window will open and enter the event name.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704355596167/b50de009-fb45-4d41-93aa-2516f3ff74fa.png align="center")

As we can see on clicking the Test, our code is failed and shows the following error.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704355544789/083023fa-b3ed-4786-9aa0-948490e09886.png align="center")

Since the code execution timed out in just 3 seconds, so I have increased the execution time up to 10 seconds.

> Remember: Minimizing the execution time is advisable, as AWS charges are influenced by the duration of execution. AWS considers the execution time as a factor in determining costs.

Let's test our code once the execution time is increased to 10 seconds.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704356304323/214d0cb6-beb0-4423-a10a-b97c2966d65d.png align="center")

We can see that the error indicates that the user, with the given ARN (`arn:aws:sts::128571802491:assumed-role/cost-optimize-ebs-snapshot-role-g6a8dccq/cost-optimize-ebs-snapshot`), lacks the necessary permissions for the `ec2:DescribeSnapshots` action. So we are required to attach the policy, granting permissions for the `ec2:DescribeSnapshots` action.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704356692319/109c5567-4316-4a70-8493-96f67e41b01d.png align="center")

click on the Execution role, a window will open, and click "Add permissions". Click on Create Inline Policy.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704357023016/f20457e2-1f8f-41b2-9cbf-506d6cf021ed.png align="center")

Now we have chosen the policy rules including Describe Snapshots and DeleteSnapshots.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704358100577/fbf61982-34ea-4ab7-9b80-e7325faa0bfd.png align="center")

Once completing the configuration click on Create Policy and name the Policy name as below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704358181592/359eff54-070f-4b08-9a7b-d9a9a241e7b2.png align="center")

Now let's test the code after attaching the policy.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704358501837/47abbb62-8657-4b6f-bb7f-0dde6975dd56.png align="center")

The error DescribeInstance shows that permission is not allowed.

Let's create a new policy that includes: **DescribeInstance** & **DescribeVolume** in the same way in which we created the Snapshot policy. Once created save the policy as below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704364358636/242c5c54-c6cf-4288-a8fe-78fbd598c89b.png align="center")

We can the below policies attached.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704364424206/73f8f3fe-f444-431f-9736-cfa99489b609.png align="center")

Now let's execute and view the output.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704364456884/c68bcce3-fdbe-472c-87b0-8ce475a85e13.png align="center")

Let's conduct a successful test run by deleting our EC2 instance along with the attached volume. Afterward, we'll execute the test to verify whether the unattached snapshot gets deleted or not.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704364536937/3c40e4cb-773c-4f68-88a6-25d95b701873.png align="center")

Observing the deletion of an EBS snapshot with the snapshot ID, we have successfully initiated the Lambda function manually. Now, let's streamline this process by automating the execution through the CloudWatch service.

**CloudWatch** is a monitoring and management service provided by Amazon Web Services (AWS). It helps you collect and track metrics, collect and monitor log files, and set alarms. With CloudWatch, you can gain system-wide visibility into resource utilization, application performance, and operational health.

To attach to the cloudwatch, search cloudwatch in the search bar and click on it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704364765132/9e27e84c-b8c2-49c6-8d96-5943904854d4.png align="center")

In the Cloudwatch screen, we can see the **events** in the left pan and click on **rules** and then &gt; "Create Rule".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704364936511/04373cac-300f-4cf5-9577-8bbbd9da6c63.png align="center")

Now create the rule as follows:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704365094130/87ead708-341a-422d-abee-9d7ee999a940.png align="center")

Click on "Continue in EventBridge Scheduler"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704365854790/4b33a077-3222-4608-85a7-851bc45493c4.png align="center")

In this step, choose recurring schedule so that at any time if there is any unattached snapshot, it will automatically deleted once the recurring schedule is executed and for this set the cron job at what time you want the execution to be started.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704365972493/38815252-f8b0-4f30-84c1-96aac91ad5f2.png align="center")

Now in this step, choose the AWS Lambda as this contains our cost optimization program.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704366046631/883a6dbf-4e4b-4fb5-b283-ca107a4c365b.png align="center")

Choose **cost-optimize-ebs-snapshot** as the Lambda function.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704366105532/76c08bb7-93ee-47ac-b201-de4ed0f62f22.png align="center")

Absolutely! We've successfully set up cost optimization in AWS using a Lambda function and automated it by scheduling the execution with CloudWatch using a cron job. This ensures that unattached snapshots are automatically deleted based on the specified schedule, reducing unnecessary costs and improving resource management.

After the completion of the project do not forget to terminate the services like lambda function, snapshots, and ec2 instances as they may incur higher charges.

Happy Learning!!
