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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With