AWS Cost Optimizations
Cost optimization in AWS is vital when shifting from traditional data centers to the cloud. It's about smartly managing resources to avoid overspending. Deleting unused or unnecessary resources helps keep bills in check. Another key is finding ways to streamline usage, ensuring you're only paying for what you truly need. Essentially, it's about maximizing value while minimizing unnecessary expenses in the cloud.
For better understanding, let's all come together and work on a real-life hands-on project.
In the project, we'll create a Lambda function that identifies EBS snapshots that are no longer associated with any active EC2 instance and deletes them to save on storage costs.
So,let's get started with our project.
Firstly, we'll create an EC2 instance, then create a snapshot. While creating the snapshot, we'll attach our instance's volume to the snapshot.
Now, we've successfully created our snapshot.
Now, we'll create a Lambda function to identify unassociated snapshots.
After clicking the "Create Function" button, we'll give our function a suitable name and choose the runtime programming language. Here, we'll select Python as we'll be using Python code for our cost optimizations. Finally, by clicking the "Create Function" button, our Lambda function will be created.
Now, we'll paste our Python Boto3 scripting code here and hit the deploy button.
Once the code is deployed, we'll click the "Test" button, which will bring up an interface. Here, we'll provide the event name and then click the "Save" option.
Again, if we click on the test button, we'll see an error message in the console saying, "when calling the DescribeSnapshots operation: You are not authorized to perform this operation. User: arn:aws:sts::367235933966:assumed-role/Lambda-costOptimizations-role-a9j81z7d/Lambda-costOptimizations is not authorized to perform: ec2:DescribeSnapshots because no identity-based policy allows the ec2:DescribeSnapshots action."
This error indicates that the Lambda function doesn't have permission to describe snapshots.
To fix this, we need to update the role attached to the Lambda function by granting it access to describe instances, snapshots, volumes, and the ability to delete snapshots.
We'll create a new policy for this. By clicking the role name URL, we'll open a new tab.
Then, we'll add permissions for describing snapshots, instances, volumes, and deleting snapshots to the Lambda function's role.
Once the permissions are given, running the Lambda function will enable it to identify snapshots without attached volumes and delete those snapshots.
Above, we see that I had one snapshot created which was attached to a volume and an instance. Despite that, it didn't get deleted. So, let's proceed to delete our instance, ensuring the volumes get deleted along with it. This way, the snapshot won't have a volume attached to it anymore. Consequently, the Lambda function will automatically delete the unassociated snapshot.
In the screenshot above, we can see both the volume and the instance have been successfully deleted, but there's still one remaining snapshot. Now, if we head to our Lambda function and click the test button, we'll observe that the Python code's condition will result in the snapshot's deletion. This is because the snapshot is unused and not associated with anything.
Upon clicking the test button, we'll notice the message "Deleted EBS snapshot snap-00319fefb48d38f06 as its associated volume was not found" in the console output.
Now, let's check our EC2 dashboard to confirm if our snapshot has indeed been deleted.
As seen in the dashboard, the number of snapshots has decreased to 0, indicating that our Lambda function worked as intended.
This is one of the situations where the developer may have created the snapshot and while deleting the instance he may have forgotten to delete the snapshot. So the DevOps engineer can use the lambda functions to reduce the cost by deleting the unwanted instance. We can do many other things for Cost Optimization, this is just an example of how can we reduce the overall Cost.