AWS Lambda for Cost Optimization
The Project involves creating an AWS Lambda function that will automatically monitor and optimize your AWS resources for cost efficiency. The Lambda function will:
Identify and terminate idle resources
Downsize underutilized instances
Tag resources for Cost Allocation
Move Infrequently Accessed data to Lower cost storage classes
Step 1: Create the Lambda Function
1.1 Identify and terminate idle resources:
Browse to AWS Lambda, click on create function
Select Author from scratch
Provide a function name
Select runtime as Python or any option of your choice with respect to the script we use
Click on create function
Step 2: Assign a Role to Lambda function
- when the Lambda function is created click on the configuration, where we get to see the Role name
- A new tab of IAM role will open up click on Add permissions and select Create inline policy
here under specify permission, we can select the JSON tab, or any option which we are comfortable with to provide the permissions
We can get the JSON file with the permission from the GitHub: https://github.com/edrichlewis/AWS-Projects/blob/main/Cost-Optimization-Using-Lambda/CloudOptimizationPolicy.json
-
Paste it under specify permission and click on next
Provide the policy name and create policy
Now browse to General configuration and click on edit
edit the Timeout to 10sec, since the python script will take approx 10 sec to complete the task.
3 sec is the default timeout and the lambda will switch off automatically once it reaches the threshold of 3 sec, hence we increase it to 10 sec.
Save the changes
Browse to code
-
add the Python code from GitHub: https://github.com/edrichlewis/AWS-Projects/blob/main/Cost-Optimization-Using-Lambda/Lambda-function-to-delete-idel-instance.py
Run the test and deploy the code.
For Verification, we can create 2-3 instances (free tier) and run the code in Lambda, the created the instance which were consuming less CPU < 10% ( the one which we created) will be deleted.
Note: Before we run the code we need to get the data if there are any instances in use but consuming less CPU or we can edit the code as per our requirement
code snippet:
We can also set the Cloud watch to schedule the Lambda function automatically
Similarly, we have Python code for
- Downsize underutilized instances: https://github.com/edrichlewis/AWS-Projects/blob/main/Cost-Optimization-Using-Lambda/Lambda-function-downsize-underutilized-Instances.py
additional permission to set: https://github.com/edrichlewis/AWS-Projects/blob/main/Cost-Optimization-Using-Lambda/IAM-additional-permission-downsize-underutilized-Instances.json
Tag resources for Cost Allocation
Move Infrequently Accessed data to Lower-cost storage classes
(need to update the code)