Sometimes, developers create EC2 instances with volumes attached to them by default. For backup purposes, these developers also create snapshots. However, when they no longer need the EC2 instance and decide to terminate it, they sometimes forget to delete the snapshots created for backup. As a result, they continue to incur costs for these unused snapshots, even though they are not actively using them.
We're using AWS to save money on storage costs. We made a Smart Lambda function that looks at our snapshots and our EC2 instances. If Lambda finds a snapshot that isn't connected to any active EC2 instances, it deletes it to save us money. This helps us keep our AWS costs down.
There are many similar problems like this. For instance, we might attach an Elastic IP to our EC2 instance but forget to delete the Elastic IP after terminating the EC2 instance. In such a case, the Elastic IP continues to incur costs for us.
- Log into your AWS Console.
- Navigate to the EC2 Console.
- In the Instances section, select 'Instances,' and then click on 'Launch Instance'.
- Next, navigate to the 'Elastic Block Store' section and select 'Volumes'.
- You will notice that a default volume has already been created for us.
- Next, click on 'Snapshots,' and then click the 'Create Snapshot' button. It will prompt you with a page that looks like this.
- In Volume ID section choose your default Volume ID created when we create instance.
- Next, click 'Next,' provide a name for your Snapshot, and then scroll down and click 'Create Snapshot'.
- After creating a Snapshot, navigate to the Lambda Console..
- You will see some options in the user interface, such as 'Create Function'.
- Click on 'Functions'.
- Select 'Author from Scratch,' then enter the Function name, and choose the latest Python version.
- Scroll down and click 'Create Function'.
6.After creating the function, scroll down, and you will see something like the image below..
- Click on the 'Code' section.
- Next, clear the existing code and replace it with the 'identify_stale_snapshots.py' code.
- Click 'Deploy' to save your changes, and then click 'Test.' It will prompt a page that looks like the one given below.
- Please configure the settings as displayed above and then scroll down. Next, click on 'Create Event'.
- Once you've created the event, proceed to the IAM Console(Identity and Access Management) and then navigate policies section to create a new policy.
-
In the 'Actions' section, grant permissions for the following actions: DescribeInstances, DescribeVolumes, DescribeSnapshots, DeleteSnapshots.
-
After Creating the Policies , Navigate to the Lambda Sections.
-
Next, go to the page of the Lambda function you've created. In the "Permissions" section, click on the role name.
- Click on 'Add Permissions' and then select 'Attach Policy.'
- Choose the correct policy you created.
- Then scroll down and click 'Add Permissions'.
- After that, you can go to the Lambda function page and run the code; it will display some outputs as shown below.
- You can terminate the EC2 instance to test our Lambda function.
- Navigate to the EC2 console and then terminate the EC2 instance.
- Return to the Lambda console to test the code; go to the Lambda Function page.
- Under the Code section, click 'Test code', it will display an output like this.
- As expected, our Lambda function deleted the snapshot because it was associated with a volume that couldn't be found.
We can use CloudWatch to automatically trigger the Lambda function every hour, day, minute, or second. However, this may result in higher costs because our Lambda execution time increases when triggered automatically. Nevertheless, manually triggering this function is a better choice because it allows us to trigger it when needed.
- Navigate to CloudWatch Console.
- Next, on the following page, configure the schedule pattern as follows:
- Scroll Down and then Click Next.
- Scroll Down and then Click Next.
- On the next page, choose 'None' for the 'Action after Schedule' option.
- You have successfully created the scheduler, which will trigger the Lambda function every hour.
- However, please note that this setup will incur some costs since the function is triggered continuously every hour. Alternatively, we can configure it to run on specific days and times as needed.