Optimize code

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.

Use of Graviton2

Workloads

  • Multithreading or performing many I/O operations

  • Machine learning inference based on the CPUs

  • Video encoding

  • Gaming

Cost

  • 20% cheaper, including provisioned concurrency

  • Supported on Compute Savings Plans

Configuration in Serverless Framework

You must add the following line architecture: 'arm64'.

1
2
3
# serverless.yml
provider:
    architecture: 'arm64'

Using AWS Lambda Power Tunning

Measuring cost efficient memory size is one of the very easy and 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.

1_step

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

table_cost

2_step

Usage guide

Choose the option that involves the least effort option #1. Open the following link while logged into the AWS account.

In the following template you can configure the range of RAM memory allowed for the evaluation 3_step Una vez termine el proceso de creación se debe dar clic en el link powerTuningStateMachine

4_step 5_step

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

1
2
3
4
5
6
7
8
{
        "lambdaARN": "your-lambda-function-arn",
        "powerValues": [128, 256, 512, 1024, 2048, 3008],
        "num": 10,
        "payload": "{}",
        "parallelInvocation": true,
        "strategy": "cost"
}

Execution begins:

5_step

6_step

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

result_power_tuning

Serverless optimizations

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

References: