Cost optimization - AWS Lambda

Cost optimization

The cost of running Lambda for your workload is determined by three factors: the number of executions, the duration and memory usage (combined as gigabyte-seconds), and data transfer. Beyond the impact of memory allocation discussed in the previous section, there are other design choices that impact these three variables and therefore can reduce cost.

Your choice of runtime can impact the cost. Generally, compiled languages run code more quickly than interpreted languages but can take longer to initialize. For small functions with simple functionality, often an interpreted language is better suited for the fastest total execution time, and therefore the lowest cost. Functions using compiled languages are often faster in workloads with heavier computational complexity or where you are using Provisioned Concurrency, so the initialization overhead occurs before the invocation.

Invocation frequency is a major factor in determining cost. Depending upon which events are triggering Lambda functions, there are various controls you can use to lower the total number of invocations. For Lambda functions triggered by:

  • API Gateway: use CloudFront caching in front of API Gateway for calls that return data that doesn’t frequently change. This increases the CloudFront cost but reduces the API Gateway and Lambda costs.

  • SQS: the BatchSize property determines the number of items in an SQS queue sent to Lambda per invocation. Increasing this number reduces the number of Lambda invocations. Depending upon your use case, it may be possible to aggregate more data per message sent to SQS to process more data in fewer invocations.

perf optimize figure 13

From https://github.com/aws-samples/s3-to-lambda-patterns/blob/master/ddbImporter/v2/template.yaml

Also, consider the overall data transfer costs for your workload. Data transfer costs for Lambda are charged at the EC2 data transfer rates listed at https://aws.amazon.com/ec2/pricing/on-demand/ in the Data transfer section. Data transferred in from the internet is not subject to any transfer costs. Generally, you can minimize these costs by limiting how much data is passed in messages between microservices.