Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the cost of a single lambda function?

I'm creating a report of the lambda functions in my AWS accounts and looking to see if I could get the cost of the previous invokes and how many times a lambda function has been invoked. I've looked into the documentation for lambda, pricing, cloudwatch, and costexplorer but haven't found anything. I'm now wondering if I have to go through multiple API calls. All help is appreciated!

like image 507
Parsa Jahanlou Avatar asked Feb 02 '26 05:02

Parsa Jahanlou


2 Answers

The following log insights query will tell you the cost of a lambda function for a given period.

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

You just need to go into log insights and select the correct log stream for your lambda function. This can be modified pretty easily to give you an invocation count as well.

Important Note: The 1.6279296875e-11 + 2.0e-7 factor is based on the per compute-millisecond-megabyte cost for an x86 lambda instance in us-east-1. You may need to adjust it if that doesn't apply to you.

like image 97
magnanimousllamacopter Avatar answered Feb 04 '26 19:02

magnanimousllamacopter


One other option is that you add a unique tag to each lambda function. Cost explorer should allow you to filter results based on tag. This will not give you historical data but it should allow you to more easily track of a single function cost via the Billing console and APIs.

like image 29
JD D Avatar answered Feb 04 '26 20:02

JD D