Eliminate unused (available) EBS using AWS Lambda and CloudWatch Events
Tags: aws, ebs, cloudwatch
Published: 2021-11-14 19:34:01

In this occasion we share an AWS Lambda that checks if the EBS are available in an AWS region and with CloudWatch events we call upon to eliminate EBS volume. However, it is also possible to add a tag for EBS volume that is available, and it does not require to be deleted.

####Definitions####

**AWS Lambda** *“Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging. With Lambda, you can run code for virtually any type of application or backend service.”* [1]

**AWS Elastic Block Store (EBS)** *“provides block level storage volumes for use with EC2 instances. EBS volumes behave like raw, unformatted block devices.”* [2]

**CloudWatch Events** *“delivers a near real-time stream of system events that describe changes in Amazon Web Services (AWS) resources. Using simple rules that you can quickly set up, you can match events and route them to one or more target functions or streams.”* [3]

####Warning####
**We recommend that you execute this configuration first in a controlled environment for tests or sandbox.**

**Using the next repository**
https://github.com/vperezpereira/ebs-delete

1.	In an AWS account using CloudShell download the git repository and execute the following commands.
git clone https://github.com/vperezpereira/ebs-delete.git 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edqxb05ztr1f24mk22lb.png)
 
 

sam build 
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/715we3a66yeuy9aly2d3.png)
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt163sv536xxs9pjr9xs.png)
 
 
sam deploy --guided
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vfyg0jj0r3cj60ix171.png)
 
**If an error like this occurs using the last command:**

*“Error: Failed to create changeset for the stack: ebs-ireland, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state: For expression "Status" we matched expected path: "FAILED" Status: FAILED. Reason: Requires capabilities : [CAPABILITY_NAMED_IAM]”*

**Execute the following command:**
sam deploy --capabilities CAPABILITY_NAMED_IAM 
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpctyx06yo95ebb1imuo.png)
 
**In this point, SAM will require confirmation for deployment**
1.	An AWS Lambda with the code that executes revision and deletes EBS volume.
2.	A S3 Bucket where  the deleted EBS volume is stored.
3.	Finally, the permissions necessary for deployment.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sr6qvwxf2ufncv38emf.png)
 



Result of deploy
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e53jo3gs5vs1f4dx0zka.png)
 
The configuration of creating the CloudWatch events rule is not added to the template because we think that it will depend on the requirements of each individual.
2.	Creating CloudWatch events Rule.
For this demo, we add a Rule each 5 minutes that calls upon that AWS Lambda: ebs-ireland-EBSDelete
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1r6sla1f4kmd1on77rr0.png)
 
We activate and finish the rule creation.
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxbc2gzgo5oe5aimmod0.png)
 

We validate that AWS Lambda has the correct environments, you can also modify them, or add a tag on EBS volume that you wish to protect from elimination.  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x04v4qyo9a75qsbn8l3.png)
 
For a demo, we created two EBS volumes available. 
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4cou0gonw1fvd9m3iu9.png)
 

Then the result is that the eliminated EBS volume goes to the S3 Bucket and then we review the file log.
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfgteigqmdwqxdlecjla.png)
 
The file indicates that EBS volumes ID were eliminated.
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4l57vwf8w6drpj3x9g0z.png)
 



[1]: https://docs.aws.amazon.com/lambda/latest/dg/welcome.html
[2]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html
[3]: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html