How do memory and computing power affect AWS Lambda cost?

3 minutos de lectura
0

I want to understand how memory and computing power affect AWS Lambda cost.

Short description

Memory is available to Lambda developers to control the performance of a function. The amount of memory allocated to a Lambda function is between 128 MB and 10,240 MB. The Lambda console defaults new functions to 128 MB, and many developers choose 128 MB for their functions.

However, it's a best practice to choose 128 MB only for simple Lambda functions. For example, functions that transform and route events to other AWS services. If the function performs any of the following actions, then it has a higher memory allocation:

  • Imports libraries.
  • Imports Lambda layers.
  • Interacts with data loaded from Amazon Simple Storage Service (Amazon S3).
  • Interacts with data loaded from Amazon Elastic File System (Amazon EFS).

Resolution

Lambda function pricing

Lambda charges are based on the number of requests for your functions and the duration that it takes for your code to run. Lambda counts a request each time it invokes in response to an event notification. For example, from Amazon Simple Notification Service (SNS) or Amazon EventBridge. Additionally, Lambda counts a request each time it starts in response to an invoke call. For example, from Amazon API Gateway, or using the AWS SDK, including test invokes from the Lambda console.

Duration calculates from the time that your code begins to run until it returns or stops, rounded up to the nearest 1 millisecond. For more information, see AWS Lambda Pricing. The price depends on the amount of memory that you allocate to your function. The amount of memory also determines the amount of virtual CPU available to a function. Adding more memory proportionally increases the amount of CPU, which then increases the available computational power. If a function is CPU-bound, network-bound, or memory-bound, then changing the memory setting can improve performance. An increase in memory size initiates an equivalent increase in CPU available to your function.

Effect of memory power on Lambda cost

The Lambda service charges for the total amount of gigabyte-seconds consumed by a function. An increase of memory affects the overall cost if the total duration stays constant. Gigabyte-seconds are the product of total memory (in gigabytes) and duration (in seconds). However, if you increase the memory available, the duration decreases. As a result, the overall cost increase is negligible or even decreases.

For example, 1000 invocations of a function that computes prime numbers has the following average durations at different memory levels:

MemoryDurationCost
128 MB11.722 s$0.024628
512 MB6.678 s$0.028035
1024 MB3.194 s$0.026830
1536 MB1.465 s$0.02463

In this example, at 128 MB, the function takes 11.722 seconds on average to complete, at a cost of $0.024628 for 1000 invocations. When the memory is increased to 1536 MB, the duration average drops to 1.465 seconds, so the cost is $0.024638. For a one-thousandth of a cent cost difference, the function has a 10-fold improvement in performance.

If memory consumption approaches the configured maximum, then monitor functions with Amazon CloudWatch and set alarms. This helps to identify memory-bound functions. For CPU-bound and IO-bound functions, monitor the duration to provide more insight. In these cases, an increase of memory helps resolve the compute or network bottlenecks. For more information, see Monitoring and observability.


OFICIAL DE AWS
OFICIAL DE AWSActualizada hace un año