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:

  1. Identify and terminate idle resources

  2. Downsize underutilized instances

  3. Tag resources for Cost Allocation

  4. Move Infrequently Accessed data to Lower cost storage classes

Step 1: Create the Lambda Function

1.1 Identify and terminate idle resources:

  1. Browse to AWS Lambda, click on create function

  2. Select Author from scratch

  3. Provide a function name

  4. Select runtime as Python or any option of your choice with respect to the script we use

  5. Click on create function

Step 2: Assign a Role to Lambda function

  1. when the Lambda function is created click on the configuration, where we get to see the Role name

  1. A new tab of IAM role will open up click on Add permissions and select Create inline policy

  1. 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

  2. Paste it under specify permission and click on next

  3. Provide the policy name and create policy

  4. Now browse to General configuration and click on edit

  1. 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.

  2. Save the changes

  3. Browse to code

  4. 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

  5. Run the test and deploy the code.

  6. 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

  1. 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

  1. Tag resources for Cost Allocation

  2. Move Infrequently Accessed data to Lower-cost storage classes

    (need to update the code)