Raul Naupari
raulnq

raulnq

AWS Lambda Functions: Finding the right memory size

AWS Lambda Functions: Finding the right memory size

Raul Naupari's photo
Raul Naupari
·Oct 23, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

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:

  • Receives an HTTP request from API Gateway.
  • Store the request in a DynamoDb table.
  • Send a message to an SQS queue.
  • Return an HTTP response

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.

tool.PNG

installing.PNG

Once the tool is installed, go to AWS Step Functions to locate our state machine:

image.png

Open the state machine and start a new execution:

image (5).png

image.png

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:

image (6).png

image.png

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.

 
Share this