Finding & Notifying Unused AWS EBS Volumes Using Lambda, Event Bridge, & SNS

Written By: Manish Juneja

Overview

Efficient Cloud Cost Management is a crucial objective for customers spanning various industries and sectors. Specifically, concerning the AWS EBS storage service, unutilized resources can lead to unnecessary expenses if the lifecycle of volumes is not closely monitored. Consequently, neglected or forgotten Amazon EBS volumes contribute to increased AWS charges.

This article presents a solution utilizing AWS Lambda, Amazon EventBridge, and AWS SNS to identify idle and disconnected EBS volumes, ensuring timely alerts through email notifications. By implementing this approach, businesses can effectively reduce costs and optimize their expenditure.

To achieve the objective of listing unused EBS volumes and sending email notifications via SNS topics, we will construct a lambda function. Subsequently, we are going to establish an Amazon EventBridge rule that triggers the lambda function automatically every week. This process enables the compilation of a comprehensive inventory of orphaned EBS volumes in a specific AWS region, providing valuable insights for cost management.

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

Prerequisite

It is essential to have a subscribed AWS SNS topic to enable email notifications. The SNS topic’s Amazon Resource Name (ARN) will be utilized in the Lambda code.

The Lambda execution requires an IAM role with the necessary permissions, including SNS publish, EBS volume describes, list, and basic Lambda execution privileges.

Steps walkthrough

Create Lambda function

· Navigate to the Lambda Service Dashboard within the Amazon Management Console. Once you have accessed the Lambda dashboard, click the Create Function option.

Creating Lambda function

· Next, select the option Author from Scratch and proceed to specify the desired function name. Choose Python 3.7 as the preferred runtime. Select the appropriate lambda service role, and finalize the process by selecting the Create option.

Creating a function

Note: Please ensure the lambda execution role is correctly configured with the necessary SNS and EBS permissions policies.

· Next, open a code editor of your choice and start writing the required code. This will allow you to implement the necessary functionalities and logic for the task.

code editor

· Copy and paste the provided code into the Lambda function. Ensure that you replace the SNS topic ARN with the appropriate value. Once done, click on “Deploy” to finalize the deployment process.

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)

· The lambda function is now prepared and ready to be executed.

Create EventBridge Schedule Lambda on Weekly Basis

· Access the Amazon EventBridge service and navigate to the rules section. From there, select the option create rule.

Creating EventBridge Schedule

· Specify the desired rule name and choose the schedule option. Select the cron-based schedule and provide a Cron expression for the schedule type. In this scenario, we are utilizing a Cron expression that triggers the rule once a week.

Define rule detail

· Within the Targets details section, choose the AWS Lambda option. Select the lambda function created in the previous step and select the “Create rule” option.

Target details

· The Lambda function will be automatically triggered weekly to detect new EBS volumes and send email alerts via the designated SNS topic.

Conclusion

Throughout this article, we have demonstrated the process of receiving email notifications containing a comprehensive list of unused EBS volumes. By reviewing and taking necessary actions, such as deleting unnecessary volumes, you can effectively reduce the costs associated with your monthly Amazon bill.

Share

Search Post

Recent Posts

What Are Regulatory Compliance Issues In Cloud Computing? How To Overcome It?
June 21, 2024
Endpoint Security for Startups: Why Should Small Businesses Care?
June 19, 2024
Accelerate Business Operations with Amazon Aurora
June 14, 2024

Categories

Tags

Subscribe to the
latest insights

Subscribe to the latest insights

Related Posts

What Are Regulatory Compliance Issues In Cloud Computing? How To Overcome It?

Endpoint Security for Startups: Why Should Small Businesses Care?

Accelerate Business Operations with Amazon Aurora

Get in Touch!

Are you prepared to excel in the digital transformation of healthcare with Rapyder? Let’s connect and embark on this journey together.

I accept T&C and Privacy