Lambda的性能

内存的分配

  • Lambda内存的配置是开发者调整Lambda性能的最主要手段,大小在128 MB 到 10,240 MB之间。

  • 内存的分配也决定着分配给Lambda的CPU数量,增加更大的内存会正相关的增加CPU数量。当内存为1769MB时,正好对应1个vCPU(至多分配6个vCPU)。所以当分配内存大于1769MB时,应用程序应当使用多核多线程来提高性能。

  • 不管你的应该是内存密集型计算密集型还是带宽密集型,调大内存都能改善程序的运行速度。


由于Lambda按 执行时间 x 分配的内存收费,增加内存会影响Lambda的帐单,有可能增加帐单,也有可能减少帐单例如下面的案例,在128MB或1024MB时,产生的费用较小:

Memory Duration Cost
128 MB 11.722 s $0.024628
256 MB 6.678 s $0.028035
512 MB 3.194 s $0.026830
1024 MB 1.465 s $0.024638

https://docs.aws.amazon.com/lambda/latest/operatorguide/profile-functions.html

Profiling functions with AWS Lambda Power Tuning

Choosing the memory allocated to Lambda functions is an optimization process that balances speed (duration) and cost. While you can manually run tests on functions by selecting different memory allocations and measuring the time taken to complete, the AWS Lambda Power Tuning tool allows you to automate the process.

This tool uses AWS Step Functions to run multiple concurrent versions of a Lambda function at different memory allocations and measure the performance. The input function is run in your AWS account, performing live HTTP calls and SDK interaction, to measure likely performance in a live production scenario. You can also implement a CI/CD process to use this tool to automatically measure the performance of new functions you deploy.

You can graph the results to visualize the performance and cost trade-off. In this example, you can see that a function has the lowest cost at 1024 MB and 1536 MB of memory, but the fastest execution at 3008 MB:

                perf optimize figure 9

Generally, CPU-bound Lambda functions see the most benefit when memory increases, whereas network-bound see the least. This is because more memory provides greater computational capability, but it does not impact the response time of downstream services in network calls. Running the profiler on your functions provides insight into how your code performs at different memory allocations, allowing you to make better decisions about how to configure your functions.

You can also use AWS Cost Optimizer to automate a cost-performance analysis for all the Lambda functions in an AWS account. This service evaluates functions that have run at least 50 times over the previous 14 days, and provides automatic recommendations for memory allocation. You can opt-in from the Cost Optimizer console to use this free recommendation engine.

通过CloudWatch Lambda Insights监控内存使用

As discussed in Monitoring and observability , you can monitor functions with CloudWatch and set alarms if memory consumption is approaching the configured maximum. This can help identify memory-bound functions. For CPU-bound and IO-bound functions, monitoring the duration can often provide more insight. In these cases, increasing the memory can help resolve the compute or network bottlenecks.

image-20220403193522533

image-20220403191310414

Initialize outside the handler

image-20220403191414326

Configuring ephemeral storage (console)

https://aws.amazon.com/blogs/aws/aws-lambda-now-supports-up-to-10-gb-ephemeral-storage/

By default, Lambda allocates 512 MB for a function’s /tmp directory. You can increase or decrease this amount using the Ephemeral storage (MB) setting. To configure the size of a function’s /tmp directory, set a whole number value between 512 MB and 10,240 MB.

Note

Configuring ephemeral storage past the default 512 MB allocated incurs a cost. For more information, see Lambda pricing .

You can configure the size of a function’s /tmp directory in the Lambda console.

To update the size of a function’s /tmp directory

  1. Open the Functions page of the Lambda console.
  2. Choose a function.
  3. On the function configuration page, on the General configuration pane, choose Edit.
  4. For Ephemeral storage (MB), set a value from 512 MB to 10,240 MB.
  5. Choose Save.

image-20220403191539372

Accepting function memory recommendations (console)

If you have administrator permissions in AWS Identity and Access Management (IAM), you can opt in to receive Lambda function memory setting recommendations from AWS Compute Optimizer. For instructions on opting in to memory recommendations for your account or organization, see Opting in your account in the AWS Compute Optimizer User Guide.

Note

Compute Optimizer supports only functions that use x86_64 architecture.

When you’ve opted in and your Lambda function meets Compute Optimizer requirements , you can view and accept function memory recommendations from Compute Optimizer in the Lambda console.

To accept a function memory recommendation

  1. Open the Functions page of the Lambda console.
  2. Choose a function.
  3. On the function configuration page, on the General configuration pane, choose Edit.
  4. Under Memory (MB), in the memory alert, choose Update.
  5. Choose Save.