DEV Community

Cover image for AWS Lambda Optimizations 👨‍💻
Kevin Lupera
Kevin Lupera

Posted on • Edited on • Originally published at dev.to

AWS Lambda Optimizations 👨‍💻

This post was written with the support of Alexis Polo @aledpolo

Spanish Version

Use packers such as esbuild or webpack, in several tests performed there was evidence of improvement using esbuild for compilation time and size here is a reference. Both have offline support.

Using Graviton2

Workloads
Multithreading or performing many I/O operations
Machine learning inference based on CPUs
Video encoding
Gaming
Processing
Cost
20% cheaper, including provisioned concurrency
Supported on Compute Savings Plans
Serverless Framework configuration
The following line architecture: 'arm64' must be added

serverless.yml provider: architecture: 'arm64'

Using AWS Lambda Power Tunning

Measuring cost efficient memory size is one of the easiest and most useful optimization practices.
By default using frameworks like serverless framework or SAM you can granularly define the amount of memory to each lambda but it is good how much memory and vCPU (Virtual CPUs) provide better response times and with it better.
We have a lambda that performs a read operation on a DB and brings 50 records for which we have a comparative table with the different memory configurations, response times and cost.

Table cost per memory

A very important fact is that the 128 MB configuration has a similar cost to the 1536 MB option but the time difference is significant 10 to 1.
More memory does not always mean higher costs

Sample cost vs execution time

Guide for use

Choose the option that involves the least effort option #1. Open the following link
Application Search - AWS Serverless Application Repository while logged into your AWS account.
In the following template you can configure the range of RAM allowed for the evaluation

Step 1

Step 2

Step 3

Once the creation process is finished, click on the link powerTuningStateMachine

Step 4

Step 5
Enter the json with the information of the lambda to be tested

{ "lambdaARN": "your-lambda-function-arn", "powerValues": [128, 256, 512, 1024, 2048, 3008], "num": 10, "payload": "{}", "parallelInvocation": true, "strategy": "cost" }
Enter fullscreen mode Exit fullscreen mode

Start of execution

Step 6

Step 7
We have the following result which gives us very useful information to make adjustments in our lambda

Result

Result Interpretation

Once finished, the stack must be deleted, for this we go to CloudFormation > Stacks, select the power Tunings stack and delete it, thus deleting all the resources created, to avoid incurring additional costs.

References:

Top comments (0)