AWS Logo
Menu

Optimize AWS Lambda Costs with Automated Compute Optimizer Insights

This article explores an automated and scalable method for optimizing AWS Lambda memory configurations to enhance cost efficiency and performance. By leveraging AWS Compute Optimizer, memory recommendations are dynamically stored in AWS SSM Parameter Store. An EventBridge rule triggers a Lambda function periodically, fetching Compute Optimizer insights and updating the corresponding SSM parameters. Additionally, another rule listens for SSM Parameter Store updates and triggers a Lambda memory updates.

Published Mar 4, 2025

Introduction

AWS Lambda offers a flexible way to run serverless applications, but selecting the optimal memory configuration can be challenging. Over-provisioning leads to unnecessary costs, while under-provisioning can impact performance. AWS Compute Optimizer provides recommendations for memory adjustments based on real-time usage data. Automating this process ensures continuous cost savings and optimal performance.
Architecture Overview
architecure
Lambda-Cost-Optimizer-Architecture

Components Used:

AWS Compute Optimizer – Provides memory recommendations for Lambda functions.
  1. AWS SSM Parameter Store – Stores optimal memory configurations.
  2. AWS Lambda (for automation) – Fetches recommendations and updates parameters.
  3. Amazon EventBridge Scheduler – Triggers the optimization workflow periodically.
  4. Amazon EventBridge Rule – Listens for SSM parameter updates and triggers Lambda updates.
  5. AWS CloudFormation/Terraform – Dynamically references SSM parameters during deployment.
Source Code in this article is located here https://github.com/zechariahks/aws-cost-optimizer-solution/tree/main/lambda. Feel free to provide your feedback.

Implementation Steps

Step 1: Enable AWS Compute Optimizer for Lambda

In order to use Compute Optimizer in an AWS account, It should be enabled first. Follow below steps to enable the Compute Optimizer.

Using AWS Console:

  1. Navigate to the AWS Compute Optimizer console.
  2. Click on Get Started and choose Opt In.
  3. Ensure Compute Optimizer has sufficient time (at least 24 hours) to analyze Lambda metrics.

Using AWS CLI:

Run below command on the terminal to enable Compute Optimizer.

Step 2: Store Memory Configurations in AWS SSM Parameter Store

Using AWS Console:

  1. Click Create parameter and specify a name (e.g., /lambda/memory/MyFunction).
  2. Set the parameter type as String and enter an initial memory value for your Lambda function.

Using AWS CLI:

Step 2: Create IAM Roles

Next, we will create the IAM roles that are required for the Lambda functions. These roles will have the adequate permissions to pull Compute Optimizer recommendations and update the relevant resources.

Using AWS Console:

  1. Navigate to IAM Console
  2. Create role for Compute Optimizer Lambda:
    • Choose Lambda as the service
    • Add these policies:
      • AWSLambdaBasicExecutionRole
      • Custom policy for Compute Optimizer access (refer below CLI section for specific actions)
      • Custom policy for SSM Parameter Store access
  3. Create role for Update Lambda:
    • Choose Lambda as the service
    • Add these policies:
      • AWSLambdaBasicExecutionRole
      • Custom policy for Lambda actions
      • Custom policy for SSM Parameter Store access

Using AWS CLI:

Run below commands to create the required IAM Roles and policies.

Step 3: Create a Lambda Function to Fetch Compute Optimizer Recommendations

Now, we will create the lambda function that fetches the recommendations from Compute Optimizer.

Create a Python Lambda function (compute_optimizer_lambda.py):

On your local directory, create a file named compute_optimizer_lambda.py and add below code into it and save the file.

Deploy the Lambda function using AWS CLI:

You can deploy the lambda function using the Lambda console or run below commands to deploy it through CLI.

Step 4: Schedule Execution with EventBridge

Using AWS Console:

  1. Open Amazon EventBridge Console.
  2. Choose Rules on the left side.
  3. Create a new rule with a schedule to trigger the compute_optimizer_lambda Lambda function periodically (e.g., daily or weekly).

Using AWS CLI:

On the terminal, run below commands to create the Event Bridge rule that runs daily once. Adjust the schedule as per your need.

Step 5: Create an EventBridge Rule to Detect SSM Parameter Updates and Adjust Lambda Configuration using a second Lambda

Follow below steps to create the Lambda function that updates the target lambdas that need modifications recommended by Compute Optimizer. This lambda pulls the saved configurations from SSM Parameter store.

Create a Lambda function to update memory configuration (update_lambda_memory.py):

On your local directory, create a file named update_lambda_memory.py and add below code into it and save the file.

Deploy the Lambda function update_lambda_memory using AWS CLI:

You can deploy the lambda function using the Lambda console or run below commands to deploy it through CLI.

Configure EventBridge rule to trigger update_lambda_memory function:

Using AWS Console:

  1. Open Amazon EventBridge Console.
  2. Choose Rules on the left side.
  3. Create a new rule with to trigger the update_lambda_memory Lambda function when SSM Parameters are updated. Check below CLI step for the specific event details to use.

Using AWS CLI:

On the terminal, run below commands to create the Event Bridge rule that invokes the lambda function when the SSM parameters are updated.

Step 6: Create/Modify IaC (CloudFormation Template) to Use SSM Parameters

This is an example CloudFormation template that is used to deploy a sample Lambda function with an initial memory configuration.

Example in CloudFormation:

Create a file named sample-lambda.yml on your local directory and add below code to it.

Deploy the CloudFormation Stack using CLI

On your terminal, run below command to create the sample CloudFormation stack. Once the stack is deployed, it will take approximately 24 hours for Compute Optimizer generate recommendations for the lambda that was deployed as part of this template.

Deploy the entire infrastructure stack

If you want to use CloudFormation to deploy all the above resources, you can use the template lambda-optimizer-template.yml located at https://github.com/zechariahks/aws-cost-optimizer-solution/lambda/templates/ location. Run below command to deploy the stack.

Script to invoke lambda function to generate recommendations

As per the documentation, lambda function must be invoked at least 50 times in the lat 14 days to generate recommendations. You can use below script for invoking your lambda function.
After the solution is deployed in the account, wait for 24 hours and you will see lambda recommendations as below. The event bridge rules will get triggered as per the schedule and update the memory configurations for the relevant Lambda functions.
compute_optimizer_recommendations
compute_optimizer_recommendations

Conclusion

This solution ensures continuous Lambda memory optimization using AWS Compute Optimizer, SSM Parameter Store, and EventBridge. This approach eliminates the need for manual updates and automates the entire workflow for updating the Lambda functions dynamically based on Compute Optimizer recommendations.

Resources:

Clean up

If you used CLI commands to create the resources, then run the following commands to clean up the resources. Update the commands appropriately to align with your resource names.
If you used CloudFormation template to create the infrastructure, then run below command to delete the stack.
 

Comments