logo
March 10, 2023

[TIL] How to Calculate Cost per Lambda Function?

How actual AWS Lambda cost is calculated?

Have you ever thought about the number in your AWS Lambda bill or wanted to find out which Lambda function costs you how many dollars or how the Cost is calculated for Lambda?

How actual AWS Lambda cost is calculated?

Calculating the cost of an AWS Lambda function can be tricky due to the multiple factors that determine it. However, with a bit of analysis, we can easily determine which Lambda function costs us how much. In this blog post, we will cover the four key elements that determine the cost of a Lambda function and the CloudWatch Insights query we can use to calculate it.

The four key elements that determine the cost of a Lambda function are:

  • Number of Requests: The number of times the Lambda function is invoked.
  • Duration of each Request handled by the Function: The time taken by the function to process each request.
  • Allocated Memory for the Function: The amount of memory allocated to the function.
  • Storage allocated to the Function: The amount of storage allocated to the function.
  • Basically, every Lambda Function invocation log would contain a log similar to the following containing the following information

  • Request ID
  • Duration
  • Billed Duration
  • Memory Size
  • Max Memory Used
  • Let's explore them with an example.

    We have a Lambda function and its log group, we run the bellow query on AWS CloudWatch Log Insight

    plain text
    parse @message /Duration:\s*(?<@duration_ms>\d+\.\d+)\s*ms\s*Billed\s*Duration:\s*(?<@billed_duration_ms>\d+)\s*ms\s*Memory\s*Size:\s*(?<@memory_size_mb>\d+)\s*MB/
    | filter @message like /REPORT RequestId/
    A visual depiction of what is being written about

    To calculate the cost of a single Lambda function, we need to analyze each of these elements. Fortunately, AWS provides us with all the information we need in the Lambda function logs. Each log contains information such as the Request ID, Duration, Billed Duration, Memory Size, and Max Memory Used.

    There is all information we need to calculate the cost here.

    Calculation formula

    We are going to do the math with the following formula

    cost_dollars_total = sum(billed_duration_ms * memory_size_mb * 1.6279296875e-11 + 2.0e-7)

    Here,

  • billed_duration_ms is the billed duration of the Lambda function in milliseconds,
  • memory_size_mb is the memory size allocated to the function in megabyte
  • What is the 1.6279296875e-11 + 2.0e-7? It is from the cost model and the numbers set by AWS Lambda s

  • 1.6279296875e-11 is the cost per MB-millisecond
  • 2.0e-7 is the invocation cost, $0.20 per 1M requests
  • You can refer to the following screenshot

    A visual depiction of what is being written about
    A useful query to calculate the Lambda cost

    To calculate the cost, we can use a CloudWatch Insights query that parses the Lambda function logs and calculates the cost in dollars. The query we will use is as follows:

    plain text
    parse @message /Duration:\s*(?<@duration_ms>\d+\.\d+)\s*ms\s*Billed\s*Duration:\s*(?<@billed_duration_ms>\d+)\s*ms\s*Memory\s*Size:\s*(?<@memory_size_mb>\d+)\s*MB/
    | filter @message like /REPORT RequestId/
    | stats sum(@billed_duration_ms * @memory_size_mb * 1.6279296875e-11 + 2.0e-7) as @cost_dollars_total

    The output billed dollars is bellow

    A visual depiction of what is being written about

    For example, suppose we want to calculate the cost of a Lambda function with Request ID abc123 for the last 24 hours. We can modify the CloudWatch Insights query as follows:

    plain text
    parse @message /Duration:\s*(?<@duration_ms>\d+\.\d+)\s*ms\s*Billed\s*Duration:\s*(?<@billed_duration_ms>\d+)\s*ms\s*Memory\s*Size:\s*(?<@memory_size_mb>\d+)\s*MB/
    | filter @message like /REPORT RequestId: abc123/
    | filter @timestamp >= now() - 24h
    | stats sum(@billed_duration_ms * @memory_size_mb * 1.6279296875e-11 + 2.0e-7) as @cost_dollars_total

    Here, we filter for logs with the Request ID abc123 and timestamp within the last 24 hours.

    This query will return the total cost of all invocations of the function with Request ID abc123 in the last 24 hours.

    Summary

    Calculating the cost of AWS Lambda functions is important for managing resources in the cloud. By analyzing key factors such as requests, duration, memory, and storage, you can accurately calculate the cost of running a single Lambda function. CloudWatch Insights provides a helpful tool for extracting cost data from logs. With this information, you can make informed decisions about resource usage and optimize your AWS environment for cost-effectiveness.