As the high-performance and high throughput block storage on AWS for Elastic Compute Cloud (Amazon EC2) instances, Amazon Elastic Block Storage (AWS EBS) is a core part of how companies run their applications on AWS. However, the number of volumes in use, especially in enterprise IT deployments can quickly spiral out of control, leading to increased cloud storage costs.
Tracking down all these unused resources can be a time-consuming task, so an automatic solution for cleaning them up works best. Luckily, there is just a way to do that using AWS Lambda functions.
In this post we’ll give you a step-by-step walkthrough on how to clean up your unused Amazon EBS volumes using Lambda functions.
Before we get into how to remove an unused volume, let’s look at some of the reasons why you would want to do that in the first place. Not a lot of people realize it, but unused AWS EBS volumes do cost you money. These charges are based on the EBS volume size.
Usually, what you see on production systems is that each application or service running on an EC2 instance will have its own volume. This is configured in such a way to facilitate sandboxing, and to avoid system failure. Consider that there’s only one volume and two services are using that volume. If for some reason the volume fails, both the services using that volume will be affected, and that will mean trouble, for any business. To avoid this, each service will usually have its own separate volume, which limits the scale of failure, but increases the amount of Amazon EBS volumes that are in use. So, while it’s good to have a volume for each application on an EC2 instance which could be used whenever the application runs, when that application or service isn’t running, the volume is still provisioned, and still racking up charges on AWS. Even though it’s good to have the volume ready for when it is needed, it could be increasing your monthly bill. These “parked” EBS volumes still cost money. So, for AWS cost optimization, it becomes important to keep track of how many volumes are being used and how many are parked. But this could get very tedious, very quickly. There’s a way to automatically take care of this: using an AWS Lambda function to remove the unused volumes.
You can easily take care of unused AWS EBS volumes by writing a simple Lambda function that runs periodically and removes all the unused volumes in your deployment. For this demonstration, we created a Lambda function in Java.
<dependencies>
<dependency>
<groupID>com.amazonaws</groupID>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupID>com.amazonaws</groupID>
<artifactId>aws-java-sdk-lambda</artifactId>
<version>1.11.665</version>
</dependency>
<dependency>
<groupID>com.amazonaws</groupID>
<artifactId>aws-java-sdk-ec2</artifactId>
<version>1.11.762</version>
</dependency>
</dependencies>
public static final String AWS_ACCESS_KEY = System.getenv( name: "aws_access_key" );
public static final String AWS_ACCESS_KEY = System.getenv( name: "aws_secret_key" );
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(
new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)
);
AmazonEC2 amazonEC2Client = AmazonEC2Client.builder().withCredentials(awsCredentialsProvider).withRegion("us-east-2").build()
Filter filter = new Filter().withName("status").withValues("available"):
DescribeVolumesResult volumes = amazonEC2Client.describeVolumes(
new DescribeVolumesRequest().withFilters(filter)
);
volumes.getVolumes().forEach(volume -> {
System.out.println("Deleting volume with ID:" + volume.getVolumeId());
amazonEC2CLient.deleteVolume(new DeleteVolumeRequest().withVolumeId(volume.getVolumeId()));
});
START RequestId: 3028bfc0-7043-40c2-bf6f-86e98d37af15 Version:
$LATEST Volumes retrieved: 0
END RequestId: 3028bfc0-7043-40c2-bf6f-86e98d37af15
Report RequestId:3028bfc0-7043-40c2-bf6f-86e98d37af15 Duration: 10108.40 ms
Duration: 319.28 ms
You now have a Lambda function which can delete unused volumes and prevent you from wasting money on unused EBS volumes. But how and when are you going to run this Lambda function?
It is impractical to think that you’ll go to the AWS console every morning and run it manually. Running this Lambda function at any frequency you want, automatically, is easy to do with the help of CloudWatch Events. Let’s see how to do this.
If you keep unused volumes around, your monthly AWS bill is going to increase. To avoid this, you can easily go to the AWS console and delete all unused volumes. But this can’t be a manual process that you add to your schedule every day. An automatic solution is required.
You can create a Lambda function that will get the list of all unused EBS volumes in the given region, and then delete all of them, one by one. Then, you can configure a CloudWatch Event to trigger this Lambda function automatically at given intervals to completely automate the process, while keeping the cost low. This is a level of cloud automation that will not only save money but a lot of headaches.
But another way to alleviate the costs of active or unused EBS volumes in your deployment is by using NetApp Cloud Volumes ONTAP. By leveraging storage cost-cutting efficiency features such as thin provisioning, deduplication, compaction, and compression, the cost of storage volumes can be reduced.
Ready to unlock the full potential of your business with our cutting-edge cloud solutions?
Take the first step towards success and enquire now to explore how our tailored solutions can revolutionize your operations and drive growth.