This content originally appeared on Level Up Coding - Medium and was authored by Josh Thorne
Optimise your Lambda Functions in just 5 minutes
Using Lambda Power Tuning and Compute Optimizer to Fine Tune Your Lambda Functions
Introduction
Many AWS resources are often misconfigured, leading to poor user experiences and increased costs (I cover this in more detail in another article here). When leveraging Serverless architectures, ensuring that your AWS Lambda functions are configured correctly is crucial for achieving the best outcomes for your business.
Arguably, one of the most critical configurations for your Lambda functions is their memory allocation. Lambda charges are based on the memory you allocate (per GB-second), so the longer your Lambda function runs and the more memory it uses, the higher your costs.
Lambda pricing is combination of the architecture, memory and invocation time. Changing these factors will influence how much you are charged per month.
However, it is not always the case that a lower memory configuration will result in lower costs. Lambda’s CPU power increases proportionally with the amount of memory you assign. While increasing memory might seem like it would raise your costs, it can actually reduce the duration of your Lambda function’s execution, potentially lowering the overall cost.
So, choosing the right memory configuration isn’t as simple as picking the lowest amount needed. Sometimes, allocating more memory can be more cost-effective and beneficial for performance. And it is not always about costs, a greater CPU bound, lower latency Lambda is always more beneficial.
How Do I Choose the Best Memory Configuration for My Lambda?
Considering the intricate balance between latency and pricing across the many memory options that AWS provides can be challenging. Fortunately, an open-source tool called Lambda Power Tuning can help you make an informed decision: Lambda Power Tuning GitHub Repository.
Lambda Power Tuner
Lambda Power Tuning is a Step Function state machine that runs your Lambda function with various memory configurations. It collects data on the cost and latency for each memory option and presents the results in a graph, making it easy to identify the optimal memory setting for your specific use case.
Deployment Options
There are several ways to deploy the Power Tuner:
- Serverless Application Repository (SAR)
- AWS SAM CLI
- AWS CDK
- Terraform and SAR
- Native Terraform
You can see detailed instructions on how to deploy the tool with the above methods here.
For this example, we’ll deploy the Power Tuner using the Serverless Application Repository (SAR).
Step-by-Step Deployment
- Log in to the AWS Management Console and navigate to the Lambda Power Tuner in SAR.
- Click the Deploy button, which will redirect you to your AWS account.
- A dialog window will open. Click the Acknowledge (1) checkbox and then Deploy (2).
This will generate a CloudFormation stack containing the necessary resources to run the Power Tuner. You can monitor the deployment’s progress in the CloudFormation console.
Once the stack has been successfully deployed, navigate to the Step Functions console. You should see a new state machine corresponding to your Power Tuner setup.
Running the Power Tuner
- Open the state machine and click the Start execution button.
2. This will open a dialog window. Here you specify the lambda function you want to run the power tuner against, as well as the memory values. For my example, I chose the values [128, 256, 512, 1024, 2056] . You can also specify a payload to your lambda if a specific payload is required.
Example Input:
{
"lambdaARN": "arn:aws:lambda:eu-west-2:************:function:ApigwLambdaDynamodbCdkTsSta-lambdaFunction940E68AD-8tzoeQmj1OKX",
"powerValues": [128, 256, 512, 1024, 2048],
"num": 50,
"payload": {}
}
3. Click Start execution to begin the tuning process.
The execution page will show the status of the state machine. Wait for the process to complete, which is indicated when the optimizer step turns green.
Viewing and Interpreting Results
After the execution concludes:
- Click the Execution input and output tab at the top of the page.
- In the Output section, copy the visualization URL and paste it into a new browser tab.
This will open a graph displaying the impact of different memory configurations on latency and cost.
You are also provided with:
- The memory option that provides the lowest cost.
- The memory option that results in the highest cost.
- The memory option that delivers the lowest latency.
- The memory option that produces the highest latency.
As you can see, the lowest memory configuration does not always correlate with the lowest cost. The best option depends on various factors, such as your performance requirements and budget constraints.
Choosing the Best Memory Option
So how do we choose the best memory option?
Well this is going to depend on many things:
- Latency Requirements: Do you have strict latency SLAs, particularly for user-facing functions?
- Budget Constraints: Is there a budget you need to adhere to for your service?
- Prioritization: Which do you value more — cost savings or reduced latency?
For my example, the 2048MB memory option yielded the lowest latency. However, the latency improvement from 512MB to 2048MB was negligible, while the cost significantly increased. Therefore, I would choose 512MB, as it strikes the best balance between cost and performance.
Compute Optimizer
While the Lambda Power Tuner is a great tool, AWS also offers an integrated service called Compute Optimizer. Compute Optimizer automatically analyzes your Lambda functions (along with other AWS services like Fargate, EC2, and RDS) and provides optimizations such as memory recommendations.
To use Compute Optimizer:
- Navigate to the Compute Optimizer section in the AWS Console.
- Activate Compute Optimizer.
Once activated, Compute Optimizer will begin analyzing your workloads and offer recommendations. Keep in mind that it may take some time for recommendations to appear, as findings are generated daily.
Conclusion
Optimising your Lambda functions can significantly impact both cost and performance. By using the Lambda Power Tuner or AWS Compute Optimizer, you can make informed decisions to find the perfect balance for your specific use case.
If you found this article useful please clap, follow or connect with me today! 👏
Optimise your Lambda Functions in Just 5 minutes was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Josh Thorne
Josh Thorne | Sciencx (2024-08-23T11:34:17+00:00) Optimise your Lambda Functions in Just 5 minutes. Retrieved from https://www.scien.cx/2024/08/23/optimise-your-lambda-functions-in-just-5-minutes/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.