Optimizing Cloud Costs with AWS Lambda: A Serverless Approach to Efficiency
AWS Lambda is a serverless compute service provided by Amazon Web Services (AWS) that allows you to run code without provisioning or managing servers. Here's how Lambda works:
Key Concepts:
Event-Driven: Lambda functions are triggered by events. These events could be changes in data (like uploading a file to S3), HTTP requests via API Gateway, or even timed schedules (like cron jobs).
Functions: Lambda functions are pieces of code that you upload and configure to run when an event triggers them. You can write them in multiple programming languages like Python, Java, Node.js, Go, and more.
Serverless: With Lambda, you don’t have to worry about managing servers or scaling your infrastructure. AWS automatically handles scaling your function based on the incoming requests.
Pay-per-Use: AWS Lambda is cost-efficient because you only pay for the compute time you consume—there’s no charge when your code isn’t running. The cost is calculated based on the number of requests and the compute time (measured in milliseconds) that your function uses.
Auto-Scaling: Lambda automatically scales to handle the workload, ensuring that your application can handle high traffic without manual intervention.
UNDERSTANDING CLOUD COST OPTIMIZATION
Create an EC2 instance ,By default a root volume(used to store the data generated or used by an EC2 instance) is attached to the instance while creating.
Now Create a Snapshots(A snapshot in AWS is a point-in-time backup of an Amazon EBS volume.) and attach the volume to it.
Create a Lambda Function.
After the creation of the lambda Function, navigate to the code tab. Now edit the code as the following.
Now Click on the deploy button to update the code.
In order to test the code the user has to create a test event .After creating it click the test button.
After the execution the code ,it will fail because by default the lambda function execution time is 3 seconds and it will show the error of the permissions issue with the IAM role for describing the snapshots.
Navigate to the Configuration tab and click on edit and edit the timeout tab to 10 seconds for the lambda function.
By default an IAM role will be created and a default permission is attached to it. Navigate to the role and Create an inline policy and select the service as EC2 and configure the necessary permissions like Describe Snapshots, Delete Snapshots, Describe Instances, Describe Volumes. Now give the name of the policy and create the policy.
Again execute the code and it will succeeded.
You will notice that the Snapshot is not deleted . Now Navigate to the ec2 instance and delete the instance and the volume will also will get deleted. But the snapshots will remain the same.
Again run the test you will notice that the snapshot gets deleted because it’s associated volume is not found.
By following these steps, you can effectively utilize AWS Lambda for cloud cost optimization by automating the creation and management of snapshots for your EC2 instances. Remember to adjust your Lambda function's timeout settings to accommodate the tasks it needs to perform.
With the power of serverless computing, you can ensure that your resources are efficiently managed without the overhead of traditional server management.
Happy cloud building!
Subscribe to my newsletter
Read articles from Jyothsna directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by