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

AWS Compute Optimizer – Provides memory recommendations for Lambda functions.
- AWS SSM Parameter Store – Stores optimal memory configurations.
- AWS Lambda (for automation) – Fetches recommendations and updates parameters.
- Amazon EventBridge Scheduler – Triggers the optimization workflow periodically.
- Amazon EventBridge Rule – Listens for SSM parameter updates and triggers Lambda updates.
- 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.
In order to use Compute Optimizer in an AWS account, It should be enabled first. Follow below steps to enable the Compute Optimizer.
- Navigate to the AWS Compute Optimizer console.
- Click on Get Started and choose Opt In.
- Ensure Compute Optimizer has sufficient time (at least 24 hours) to analyze Lambda metrics.
Run below command on the terminal to enable Compute Optimizer.
- Open the AWS Systems Manager Parameter Store.
- Click Create parameter and specify a name (e.g.,
/lambda/memory/MyFunction
). - Set the parameter type as String and enter an initial memory value for your Lambda function.
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.
- Navigate to IAM Console
- 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
- 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
Run below commands to create the required IAM Roles and policies.
Now, we will create the lambda function that fetches the recommendations from Compute Optimizer.
On your local directory, create a file named
compute_optimizer_lambda.py
and add below code into it and save the file.You can deploy the lambda function using the Lambda console or run below commands to deploy it through CLI.
- Open Amazon EventBridge Console.
- Choose Rules on the left side.
- Create a new rule with a schedule to trigger the
compute_optimizer_lambda
Lambda function periodically (e.g., daily or weekly).
On the terminal, run below commands to create the Event Bridge rule that runs daily once. Adjust the schedule as per your need.
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.
On your local directory, create a file named
update_lambda_memory.py
and add below code into it and save the file.You can deploy the lambda function using the Lambda console or run below commands to deploy it through CLI.
- Open Amazon EventBridge Console.
- Choose Rules on the left side.
- 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.
On the terminal, run below commands to create the Event Bridge rule that invokes the lambda function when the SSM parameters are updated.
This is an example CloudFormation template that is used to deploy a sample Lambda function with an initial memory configuration.
Create a file named
sample-lambda.yml
on your local directory and add below code to it.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.
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.
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.

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.
- Github Repository: https://github.com/zechariahks/aws-cost-optimizer-solution/tree/main/lambda
- AWS Compute Optimizer documentation: https://docs.aws.amazon.com/compute-optimizer/latest/ug/getting-started.html
- AWS Lambda documentation: https://docs.aws.amazon.com/lambda/latest/dg/welcome.html
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.