Using AWS Lambda, Event Bridge, & SNS for finding & notifying unused AWS EBS volumes

Finding & Notifying Unused AWS EBS Volumes Using Lambda
April 24, 2023

Written By: Manish Juneja

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 orphaned EBS volumes on a weekly basis in a particular AWS region.

Compile orphaned EBS volumes on a weekly basis in a particular AWS region

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 describes, 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.

Creating Lambda 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.

Creating a function

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

code editor

· 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.

Creating EventBridge Schedule

· 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.

Define rule detail

· 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.

Target details

· The Lambda function will now automatically get triggered every week to identify the unused EBS volumes and send email alerts using the SNS topic.

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.

Cloud Computing Insights and Resources

Generative AI solution on Enterprise Data

Generative AI solution on Enterprise Data

Written By: Kumar Shanu, Machine Learning Specialist, Rapyder Cloud Solutions Generative AI is a powerful technology that can create new …

Generative AI solution on Enterprise Data Read More »

Extracting email attachment using AWS

Extract Email Attachment Using AWS

Written by – Manish Juneja Email is the most primitive form of person-to-person communication in the post-internet era. It’s extremely …

Extract Email Attachment Using AWS Read More »

Finding & Notifying Unused AWS EBS Volumes Using Lambda

Using AWS Lambda, Event Bridge, & SNS for finding & notifying unused AWS EBS volumes

Written By: Manish Juneja Overview Cloud cost control is one of the top goals for customers across all sectors and …

Using AWS Lambda, Event Bridge, & SNS for finding & notifying unused AWS EBS volumes Read More »