Connect with me
AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume – there is no charge when your code is not running.
With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.
In my last post, i wrote about Uses and event triggers
We are working on a Cloud data protection and DR related produce, usually, the developer in my team does unit testing with create_snapshot API and they forgot to delete the snapshot or they test the scheduler with 5-minute interval and leaves it. End of the day we will be having tons of useless snapshots from different Amazon EBS volumes in different regions which add more to the monthly bill.
I created a simple AWS Lambda function which deletes the EBS snapshots older than one day
Create a Lambda Service
- Log in to the AWS console
- Click on Lambda
- Skip the blueprint and create a new function
- Set some name, description
- Runtime to Python 3.6
- Paste the code, save and Test.
In delete_snapshots days value is hardcoded to 1 that can be replaced using events. Events are the key-value pair that can be accessed in function using event[‘key’].
In the above code I set the filter description “Created by Nimesa” , you can set filter by tags or any other parameter
Setup Execution role
- You can choose the existing role or create new
- When accessing EBS snapshots Lambda should have EC2 access to list and delete snapshots
In basic settings setup the timeout value and memory, these setting allows you to control the code execution performance and costs for your Lambda function. Changing your resource settings (by selecting memory) or changing the timeout may impact your function cost.
- Create a cloud watch event
- Rule: Fixed rate of 1 Days
- The function will be triggered by the schedule of 1 day
- You can add API Gateway also to call like web API