# 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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720184201409/ccb6e4db-6256-476e-a364-56c691b44428.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720184494590/17f5a52a-1f49-4eae-a550-5bc0ccafd0d3.png align="center")

3. 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](https://github.com/edrichlewis/AWS-Projects/blob/main/Cost-Optimization-Using-Lambda/CloudOptimizationPolicy.json)
    
4. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720184555962/f704ba64-fd55-4c62-a4af-d67c4d13c331.png align="center")
    
    Paste it under specify permission and click on next
    
5. Provide the policy name and create policy
    
6. Now browse to General configuration and click on edit
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720184816684/39440891-deb2-4dca-86a2-89db96a388ce.png align="center")

7. 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.
    
8. Save the changes
    
9. Browse to code
    
10. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720184982760/47e1471f-c931-4053-ab72-6253635a371e.png align="center")
    
    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](https://github.com/edrichlewis/AWS-Projects/blob/main/Cost-Optimization-Using-Lambda/Lambda-function-to-delete-idel-instance.py)
    
11. Run the test and deploy the code.
    
12. 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 &lt; 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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720185235966/73109e40-4661-41ac-8603-e00bb7859ce6.png align="center")

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](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](https://github.com/edrichlewis/AWS-Projects/blob/main/Cost-Optimization-Using-Lambda/IAM-additional-permission-downsize-underutilized-Instances.json)

2. Tag resources for Cost Allocation
    
3. Move Infrequently Accessed data to Lower-cost storage classes  
      
    (need to update the code)
