Salta al contenuto

How do memory and computing power affect my Lambda cost?

3 minuti di lettura
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 for lightweight Lambda functions only. 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 calculates charges based on the number of requests for your functions and how long your code runs. Lambda counts a request each time it invokes in response to an event notification. For example, an event notification from Amazon Simple Notification Service (Amazon SNS) or Amazon EventBridge. Additionally, Lambda counts a request each time it starts in response to an invoke call. For example, invoke calls from Amazon API Gateway, the AWS SDK, and 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 memory size also determines the amount of virtual CPU available to a function. When you add more memory, you proportionally increase the amount of CPU and 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. 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:

In the preceding 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 increased to 1536 MB, the duration average drops to 1.465 seconds, so the cost is $0.02463. 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 use Amazon CloudWatch to monitor functions and set alarms. This helps to find memory-bound functions. For CPU-bound and IO-bound functions, monitor the duration to get more insight. In these cases, an increase of memory helps resolve the compute or network bottlenecks. For more information, see Monitoring, debugging, and troubleshooting Lambda functions.

Related information

How do I reduce Lambda costs through architecture migration, pricing models, and alternative compute options?

aws-lambda-power-tuning on the GitHub website

AWS UFFICIALEAggiornata un mese fa