Finding unused Amazon EBS volumes using AWS Lambda, EventBridge, and SNS for cost optimization
Tags: lambda, cloud, python, aws
Published: 2023-03-06 04:38:44

**Overview**
Cloud cost control is one of the top goals for customers across all sectors and industries. With respect to the AWS EBS storage service, unused resource expenses may be incurred if the lifecycle of volumes is not fully observable. Hence, Amazon EBS volumes that are unused or are forgotten about, add to AWS charges.
In this article, we'll show you how to utilize AWS Lambda, Amazon EventBridge, and AWS SNS to discover EBS volumes that are idle and disconnected from an EC2 instance by receiving alerts through email notifications. This strategy will aid in cost reduction and cost optimization.
In order to list all the unused EBS volumes and send email notifications using SNS topics, we will build a lambda function for this solution. In the following steps, we will establish an Amazon EventBridge rule that will automatically call the lambda function once a week. As a result, we can compile a list of all unused EBS volumes on a weekly basis in a particular AWS region.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hes00rg6lxhwiat9fh8z.png)
**Prerequisite**
To receive email notifications, we require one subscribed AWS SNS topic. We will utilize the SNS topic ARN in Lambda code.
The Lambda IAM role includes SNS publish, EBS volume describe, list and basic lambda execution permissions.

**Steps walkthrough**

**Create Lambada function**
· Visit the Lambda Service Dashboard using the Amazon Management Console. On the Lambda dashboard, select **Create Function**.
· After that, click **Author from Scratch**, specify the name of the function, and select **Python 3.7** as the runtime. Then pick the lambda service role and select the Create option.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4i3o53g2a6lf67fvd9gq.png)
Note: Please ensure that the SNS and EBS permissions policies are associated with the lambda execution role.
· Then open a code editor, begin writing the code

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdbrj4lqiaylyv1i0gzq.png)
· Enter the following code into the Lambda function with the correct SNS topic ARN and then choose **Deploy**.

```
import boto3
def lambda_handler(event, context):
    ec2_client = boto3.client('ec2')
    sns_client = boto3.client('sns')
    volumes = ec2_client.describe_volumes()
    sns_arn = '<SNS Topic ARN>'
    
    unused_vols = []
    for volume in volumes['Volumes']:
        if len(volume['Attachments']) == 0:
            unused_vols.append(volume['VolumeId'])
            print(volume)
            
    
    email_body = "##### Unused EBS Volumes ##### \n"
    
    for vol in unused_vols:
        email_body = email_body + f"VolumeId = {vol} \n"
       
    
    # Send Email
    
    sns_client.publish(
        TopicArn = sns_arn,
        Subject = 'Unused EBS Volumes List',
        Message = email_body
    )
    print(email_body)
```
· Now the lambda function is ready for execution.

**Create EventBridge Schedule Lambda on Weekly Basis**
· Navigate Amazon EventBridge service and open rules. And click on **create rule**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zw1ixizu1d4iejf2zit.png)
· Mention the **rule name**, select the schedule option, and specify a **Cron expression** by selecting the **cron-based schedule**. Here we are using the cron expression which will trigger once a week.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y9ypfd9yzd5hxcinydr.png)
· In the Targets details, select the **AWS Lambda** option and select our lambda function which we build in the earlier step and then choose Create rule.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bj2vgk9vt2j4woo2qm9e.png)
· The Lambda function will now automatically get triggered every week to identify the unused EBS volumes and send email alerts using the SNS topic as below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrnlcd0ccu1olqaz3yyv.png)
**Conclusion**
In this article, we showed you how to receive email notifications about a list of unused EBS volumes so you may check them out for further action and delete them if they're not required to minimize the cost of your monthly Amazon bill.