AWS Cloud Cost Optimization: Identifying and Deleting Stale EBS Snapshots

AWS Cloud Cost Optimization: Identifying and Deleting Stale EBS Snapshots

Amazon Elastic Block Store (EBS) provides persistent block storage volumes for use with Amazon EC2 instances. However, EBS snapshots, which are backups of these volumes, can accumulate over time, leading to unnecessary storage costs. To optimize costs, it's crucial to identify and delete stale EBS snapshots—those no longer associated with active EC2 instances. In this blog, we'll create a Lambda function to automate this process, from identifying to deleting unused snapshots.

Step-by-Step Guide to Automating EBS Snapshot Cleanup with AWS Lambda

Prerequisites

  • An AWS account with sufficient permissions to manage Lambda, EC2, and EBS.

  • Basic understanding of AWS services and Python programming.

Step 1: Set Up IAM Role for Lambda

  1. Create an IAM Role:

    • Go to the IAM service in the AWS Management Console.

    • Click on "Roles" and then "Create role."

    • Choose "AWS Service" and select "Lambda."

    • Click "Next: Permissions" and attach the following policies:

      • AmazonEC2ReadOnlyAccess

      • AmazonEC2FullAccess

    • Click "Next: Tags" (optional) and then "Next: Review."

    • Provide a role name, e.g., Lambda_EC2_EBS_Cleanup, and create the role.

Step 2: Create the Lambda Function

  1. Navigate to Lambda Service:

    • Open the AWS Management Console and navigate to the Lambda service.

    • Click on "Create function."

  2. Configure the Lambda Function:

    • Choose "Author from scratch."

    • Function name: CleanupStaleEBSSnapshots

    • Runtime: Python 3.x

    • Role: Choose the Lambda_EC2_EBS_Cleanup role created earlier.

    • Click "Create function."

  3. Write the Lambda Function Code:

    1. Deploy the Code:

      • Click "Deploy" to save and deploy your Lambda function.

Step 3: Set Up a CloudWatch Events Rule

  1. Navigate to CloudWatch:

    • Go to the AWS Management Console and open the CloudWatch service.

    • Click on "Rules" under the "Events" section and then "Create rule."

  2. Configure Event Source:

    • Choose "Event Source" as "Schedule."

    • Configure the schedule (e.g., rate(1 day) to run daily).

  3. Add Target:

    • Add a target and select "Lambda function."

    • Choose the CleanupStaleEBSSnapshots Lambda function.

    • Click "Configure details," provide a name for the rule, and create the rule.

Step 4: Testing the Lambda Function

  1. Manual Test:

    • Navigate back to the Lambda function in the AWS Management Console.

    • Click on "Test" and configure a new test event with any sample JSON (it won't be used).

    • Click "Test" to execute the function manually and check the CloudWatch logs for any output.

Conclusion

Automating the identification and deletion of stale EBS snapshots can significantly reduce AWS storage costs. By setting up a Lambda function, you can ensure that your environment remains cost-efficient without manual intervention. This guide provided a detailed walkthrough from creating the necessary IAM roles to deploying and testing the Lambda function. Regular execution of this function ensures that your storage usage is optimized, keeping your AWS bill under control.