Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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.

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

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

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

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

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

The post Using AWS Lambda, Event Bridge, & SNS for finding & notifying unused AWS EBS volumes appeared first on Rapyder.



This post first appeared on Managed Cloud Services Provider, please read the originial post: here

Share the post

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

×

Subscribe to Managed Cloud Services Provider

Get updates delivered right to your inbox!

Thank you for your subscription

×