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