- Published on
[TIL] How to Calculate Cost per Lambda Function?
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?
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:
Basically, every Lambda Function invocation log would contain a log similar to the following containing the following information
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 textparse @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/
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.
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,
What is the 1.6279296875e-11 + 2.0e-7? It is from the cost model and the numbers set by AWS Lambda s
You can refer to the following screenshot
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 textparse @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
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 textparse @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.
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.
- Published on