Logo

dev-resources.site

for different kinds of informations.

Fine-Tune Your Serverless REST APIs with AWS Lambda Power Tuning

Published at
11/28/2024
Categories
lambda
aws
apigateway
performance
Author
techtrailwithsab
Categories
4 categories in total
lambda
open
aws
open
apigateway
open
performance
open
Author
16 person written this
techtrailwithsab
open
Fine-Tune Your Serverless REST APIs with AWS Lambda Power Tuning

Developing serverless REST APIs with API Gateway and AWS Lambda is now a common practice. With Amazon extending the API Gateway timeout beyond 29 seconds), serverless REST APIs can now handle complex workflows like long-running machine learning predictions and Generative AI tasks.

In this article, weโ€™ll explore how to leverage the AWS Lambda Power Tuning tool to optimize serverless REST APIs (API Gateway configured with proxy integration + AWS Lambda) for both performance and cost efficiency.

Sample Architecture - API Gateway + AWS Lambda + RDS

AWS Lambda Power Tuning

The AWS Lambda Power Tuning tool is an AWS Step Functions-based state machine designed to test Lambda performance under various memory configurations. It helps optimize for cost or performance (or a balance of both) and is compatible with any Lambda runtime.

Getting Started

To deploy the AWS Lambda Power Tuning tool, follow the instructions in the deployment guide. Once deployed, the state machine will appear in AWS Step Functions, as shown below:

AWS Lambda Power Tuning Tool State machine in AWS Console

Executing the Tool

When executing the state machine, you can customize several parameters. Below is a summary:

Parameter Description
lambdaARN Required. ARN of the Lambda function to optimize.
num Required. Number of invocations per power configuration (min: 5, recommended: 10โ€“100).
powerValues Optional. Memory values to test (128MBโ€“10,240MB). Defaults to values set at deployment.
payload Optional. Request payload for the API. Can support a static payload for every invocation or a payload from a list with relative weights
payloadS3 S3 object location for payloads >256KB.
parallelInvocation Runs all invocations in parallel if set to true (default: false).
strategy It can be "cost" or "speed" or "balanced"; if you use "cost" the tool will suggest the cheapest option, while if you use "speed" the state machine will suggest the fastest option. When using "balanced" the state machine will choose a compromise between "cost" and "speed" according to the parameter "balancedWeight".
balancedWeight Parameter that represents the trade-off between cost and speed. Value is between 0 and 1, where 0.0 is equivalent to "speed" strategy, 1.0 is equivalent to "cost" strategy. Default :0.5
preProcessorARN ARN of a Lambda function to run before each invocation of the target function.
postProcessorARN ARN of a Lambda function to run after each invocation of the target function.
includeOutputResults Includes average cost and duration for each configuration in the final output (default: false).
onlyColdStarts Forces all invocations to be cold starts

Refer to the official official documentation. for detailed explanations.

Example Input

{
  "lambdaARN": "<arn of the function being executed>",
  "powerValues": [ 128, 256, 512, 1024, 1536, 2048, 2560, 3072],
  "num": 10,
  "strategy": "speed",
  "payload": {...},
  "parallelInvocation": true,
  "includeOutputResults": true,
  "onlyColdStarts": true
}

Enter fullscreen mode Exit fullscreen mode

Input Payloads for Proxy Integration

Inputs to test Lambda functions behind API Gateway can vary by HTTP method. Below are sample payload links for common methods:

HTTP Method GitHub URL
POST Click for sample input
PUT Click for sample input
GET Click for sample input
GET (With Path Parameters) Click for sample input
GET (With QueryString) Click for sample input
DELETE Click for sample input
PATCH Click for sample input

Weighted Payloads

The tool also offers the option to define multiple payloads for HTTP methods, making it suitable for scenarios where payload structures vary significantly and can impact performance or speed. Refer Weighted Payloads in official documentation to understand how weighted payloads work

HTTP Method GitHub URL
POST (With Weighted Payloads) Click for sample input

Pre/post-processing functions

The tool also provides the ability to run custom logic before and after the execution of the lambda function. This logic should be implemented as lambda functions. Refer Pre/Post-processing functions in official documentation to understand how weighted payloads work

HTTP Method GitHub URL
Post (With Pre/Post functions) Click for sample input

Output

A sample execution output is shown below:

{
  "output": {
    "power": 2048,
    "cost": 0.0000018816000000000001,
    "duration": 54.95933333333334,
    "stateMachine": {
      "executionCost": 0.00075,
      "lambdaCost": 0.0013002423000000002,
      "visualization": "https://lambda-power-tuning.show/#encodeddata"
    },
    "stats": [
      {"value": 128, "averagePrice": 9.345e-7, "averageDuration": 443.8995},
      {"value": 2048, "averagePrice": 0.0000018816, "averageDuration": 54.9593}
    ]
  }
}

Enter fullscreen mode Exit fullscreen mode

A brief description of the output is given below

Key Description
output.power The optimal memory configuration (RAM).
output.cost The corresponding average cost (per invocation).
output.duration The corresponding average duration (per invocation).
output.stateMachine.executionCost The AWS Step Functions cost corresponding to this state machine execution (fixed value for "worst" case).
output.stateMachine.lambdaCost The AWS Lambda cost corresponding to this state machine execution (depending on number of executions and average execution time).
output.stateMachine.visualization A URL to visualize and inspect average statistics about cost and performance. Note: Average statistics are NOT shared with the server, as all data is encoded in the URL hash, client-side only.
output.stats The average duration and cost for every tested power value configuration. Only included if includeOutputResults is set to a truthy value.

Visualizing the output

The element - output.stateMachine.visualization provides a visualization URL - https://lambda-power-tuning.show/#encodeddata that can be used to visualize the result.

The source code of the UI is also open source - https://github.com/matteo-ronchetti/aws-lambda-power-tuning-ui

The outputs of POST / GET (With Path Parameters) are shown below

POST

Results for POST

GET (With Path Parameters)

Results for GET (With Path Parameters)

The tool includes a feature to compare the results of function invocations. To see how this functionality is applied in practice, check out the article Secrets Management in .NET Lambda, where we demonstrate its use to compare the performance of reading secrets in a .NET Lambda.

References

apigateway Article's
30 articles in total
Favicon
Invoking Private API Gateway Endpoints From Step Functions
Favicon
The Power of AWS API Gateway and AWS AppSync: Transforming API Development, Functionality, and Use Cases
Favicon
Generate an OpenAPI From Your Database
Favicon
API Gateway Hosting Options
Favicon
Calling IAM authenticated API Gateway with different HTTP clients
Favicon
Kong API Gateway Setup Basic to advance usages
Favicon
Building a Friends-Themed Chatbot: Exploring Amazon Bedrock for Dialogue Refinement
Favicon
How to return meaningful error messages with Zod, Lambda and API Gateway in AWS CDK
Favicon
Gcp api gateway
Favicon
Accelerating Developer Productivity with Federated Gateways
Favicon
Introducing Dedicated Managed and Fully Self-Hosted Zuplo
Favicon
The API Gateway Powering the AI Revolution
Favicon
Java api gateway cord
Favicon
Why a Hosted API Gateway Is Better Than Building Your Own
Favicon
Setting Up Custom Domain for API Gateway & CloudFront
Favicon
Apply SSL Certificate on AWS ACM (also Cloudflare)
Favicon
how to do api getway contex in aws lambdas ?
Favicon
Top 140+ Unique And Free APIs for Developers to Supercharge Development in 2025 [Must Read]
Favicon
Simplify Your Microservices Architecture: Discover OpenFeign
Favicon
Unlocking the Potential of Spring Cloud Gateway for Scalable Microservices
Favicon
Reverse Proxy vs Load Balancer vs API Gateway: Key Differences
Favicon
Recent Web Vulnerabilities and How LLMs and DCS Can Help Combat Them!
Favicon
API Gateway integration with AWS Services.
Favicon
Avoiding API Gatewayโ€™s integrations hard limit: scaling serverless architectures efficiently
Favicon
โœ…ASP.NET Core API Gateway with Ocelot Part 4 (Rate Limiting)
Favicon
Fine-Tune Your Serverless REST APIs with AWS Lambda Power Tuning
Favicon
System Design 04 - API Gateway: Your Systemโ€™s VIP Entrance
Favicon
Step-by-Step Guide to Integrating Third-Party APIs in Laravel Applications
Favicon
๐Ÿš€ ๐๐จ๐จ๐ฌ๐ญ ๐˜๐จ๐ฎ๐ซ ๐€๐๐ˆ ๐’๐ค๐ข๐ฅ๐ฅ๐ฌ ๐ฐ๐ข๐ญ๐ก ๐Œ๐ฒ ๐Ž๐œ๐ž๐ฅ๐จ๐ญ ๐†๐š๐ญ๐ž๐ฐ๐š๐ฒ ๐„๐ฌ๐ฌ๐ž๐ง๐ญ๐ข๐š๐ฅ๐ฌ!
Favicon
Role of API Gateways in Microservices Architecture

Featured ones: