AWS Lambda Functions: Finding the right memory size

Somebody who likes to code
Search for a command to run...

Somebody who likes to code
No comments yet. Be the first to comment.
Single Purpose and Monolithic Lambda Functions are two approaches to developing and deploying our applications (you can check the pros and cons here). But what are the differences from the performance perspective? To answer this question, we develop ...
Google processes tens of thousands of code changes every day across a codebase of hundreds of millions of lines. The secret isn't magic tooling — it's a documented, principled approach to code review

Amazon Simple Notification Service (SNS) looks straightforward on the surface — publish a message, deliver it to subscribers — but the gap between a topic that "works in dev" and one that is productio

Amazon Simple Queue Service (SQS) is deceptively simple to get started with, but surprisingly easy to misconfigure in ways that cause silent message loss, runaway costs, duplicate processing storms, o

HTTP API is the right default choice for the majority of .NET serverless workloads. At 70% less cost than REST API, lower latency, and native JWT authorization, it covers most production requirements

AWS Lambda has become a cornerstone of modern serverless architectures, but writing a function that "just works" is very different from writing one that is performant, cost-effective, secure, and read

Once we have our first Lambda Function up and running, a new question arises, How much memory should we use? As we know, the AWS Lambda pricing model is based on request charges(number of requests) and compute charges(GB-seconds). In addition, we need to notice that the amount of memory is related to the CPU power (check this post for more details). These characteristics make finding the right memory size not as simple as it seems. Here is where AWS Lambda Power Tuning comes to the rescue:
AWS Lambda Power Tuning is a state machine powered by AWS Step Functions that helps you optimize your Lambda functions for cost and/or performance in a data-driven way.
The state machine is designed to be easy to deploy and fast to execute. Also, it's language agnostic so you can optimize any Lambda functions in your account.
Today we will test this tool with an AWS Lambda Function that:
The code can be found here. See the post Deploying AWS Lambda Functions with Serverless Framework to get the application up.
Let's start going here to install the AWS Lambda Power Tuning in our AWS Account, with just a few clicks in the AWS Management Console.
Once the tool is installed, go to AWS Step Functions to locate our state machine:

Open the state machine and start a new execution:


Use the following as input to the state machine:
{
"lambdaARN": "<YOUR-LAMBDA-ARN>",
"powerValues": [
128,
256,
512,
1024,
1280,
1536,
1792,
2048
],
"num": 50,
"payload": {
"Resource": null,
"Path": null,
"HttpMethod": null,
"Headers": null,
"MultiValueHeaders": null,
"QueryStringParameters": null,
"MultiValueQueryStringParameters": null,
"PathParameters": null,
"StageVariables": null,
"RequestContext": null,
"Body": "{\"Name\":\"Hi\"}",
"IsBase64Encoded": false
}
}
lambdaARN: Unique identifier of the Lambda function you want to optimize.powerValues: The list of memory values to be tested.num: The number of invocations for each memory configuration.payload: The static payload that will be used for every invocation.Wait until the execution ends, go to the output tab, copy the visualization URL and open it in a browser:


Looking at the graph, 512Mb provides a good balance between invocation time and cost for our Lambda function. But if time is a concern 1024Mb is a good choice as well. Thanks, and happy coding.