Automating Cost Optimization: Deleting Unattached EBS Volumes Using AWS Lambda and CloudFormation

automating-cost-optimization:-deleting-unattached-ebs-volumes-using-aws-lambda-and-cloudformation

Understood! Here’s a structured blog post format for your content, including headings, subheadings, and references:

Automating Cost Optimization: Deleting Unattached EBS Volumes Using AWS Lambda and CloudFormation

Managing unused resources in your AWS account is crucial for cost optimization. One common source of unnecessary costs is unattached Elastic Block Store (EBS) volumes. In this guide, we’ll walk you through creating a Lambda function using AWS CloudFormation to identify and delete unattached EBS volumes with specific tags.

Why Automate EBS Volume Cleanup?

Unattached EBS volumes in the Available state can accumulate over time, leading to unnecessary costs. Automating their cleanup ensures your AWS environment stays optimized without manual intervention.

Step 1: Create the CloudFormation Template

The first step is to create a CloudFormation template that provisions the necessary resources, including the Lambda function and its associated IAM role.

Instructions:

  1. Open your text editor or IDE (e.g., Visual Studio Code).
  2. Create a new file named ebs-unattached-volume-cleanup.yaml.
  3. Copy and paste the following CloudFormation template into the file:
AWSTemplateFormatVersion: '2010-09-09'
Description: Lambda function to identify and delete unattached EBS volumes in the 'Available' state.

Resources:
  # IAM Role for Lambda Execution
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: LambdaExecutionPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
              - Effect: Allow
                Action:
                  - ec2:DescribeVolumes
                  - ec2:DeleteVolume
                Resource: '*'

  # Lambda Function to Delete Unattached EBS Volumes
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          import boto3

          def lambda_handler(event, context):
              ec2 = boto3.client('ec2')

              # Describe volumes with the state 'Available'
              try:
                  response = ec2.describe_volumes(Filters=[
                      {'Name': 'status', 'Values': ['available']}  # Filter by system-defined state
                  ])
                  print(f"Describe Volumes Response: {response}")  # Log the response for debugging
              except Exception as e:
                  print(f"Error describing volumes: {str(e)}")
                  return

              # Check if any volumes are found
              if not response['Volumes']:
                  print("No unattached EBS volumes in the 'Available' state found.")
                  return  # Exit without doing anything

              for volume in response['Volumes']:
                  volume_id = volume['VolumeId']

                  # Delete the volume
                  try:
                      print(f"Deleting volume in 'Available' state: {volume_id}")
                      ec2.delete_volume(VolumeId=volume_id)
                      print(f"Successfully deleted volume: {volume_id}")
                  except Exception as e:
                      print(f"Failed to delete volume {volume_id}: {str(e)}")
      Handler: index.lambda_handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: python3.11
      Timeout: 60
      MemorySize: 128

  # EventBridge Rule to Trigger Lambda Function
  CleanupScheduleRule:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: cron(0 11 ? * 6 *)  # Runs every Friday at 13:00 CEST (11:00 UTC)
      Targets:
        - Arn: !GetAtt LambdaFunction.Arn
          Id: LambdaTarget
      State: ENABLED

  # Grant EventBridge Permission to Invoke Lambda
  LambdaPermissionForEventBridge:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref LambdaFunction
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt CleanupScheduleRule.Arn
  1. Save the file on your system.

Step 2: Deploy the CloudFormation Template

Once the template is ready, deploy it using the AWS Management Console.

Instructions:

  1. Log in to the AWS CloudFormation Console.
  2. Click Create stack and select With new resources (standard).
  3. Under Specify template, choose Upload a template file and upload the ebs-unattached-volume-cleanup.yaml file.
  4. Enter a Stack name (e.g., UNATTACHEDEBSVOLUME-Cleanup-Lambda).
  5. Review the stack details, acknowledge the creation of IAM resources, and click Create stack.

Step 3: Verify the Deployment

After the stack is created, verify that the Lambda function and associated resources have been deployed successfully.

Instructions:

  1. Wait for the stack status to change to CREATE_COMPLETE.
  2. Navigate to the Resources tab in the CloudFormation stack to find the Lambda function name.
  3. Go to the AWS Lambda Console to confirm the function exists.

Step 4: Test the Lambda Function

Finally, test the Lambda function to ensure it works as expected.

Instructions:

  1. Open the Lambda function in the AWS Lambda Console.
  2. Click Test and create a new test event (you can leave it empty for now).
  3. Run the test and check the logs to confirm that unattached EBS volumes are being identified and deleted.

Conclusion

By following this guide, you’ve successfully automated the cleanup of unattached EBS volumes using AWS Lambda and CloudFormation. This solution helps reduce unnecessary costs and keeps your AWS environment optimized.

References

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-to-thrive-as-an-ai-product-manager:-aman-khan-at-#mtpcon-london-2025-(director-of-product,-arize-ai)

How to thrive as an AI product manager: Aman Khan at #mtpcon London 2025 (Director of Product, Arize AI)

Next Post
missouri-protoplex-partners-with-solvus-global

Missouri Protoplex Partners with Solvus Global

Related Posts