How to find the lambda function that costs the most money?

Viewed 909

I have about 60 lambda functions in my AWS account.

Is it possible to list AWS lambda functions by their costs (invocations * duration during a period of time)?

Because I'm going to change their architecture from x86_64 to arm64 to save money. And I want to start doing this from the most expensive lambda funciton.

aws lambda list-functions doesn't seem to provide this feature.

2 Answers

I think the solution you are seeking is the Cloudwatch Logs Insights. If you run this query for a Lambda function you will get the total cost for all your invocations as well as the average Cost of one invocation. Change the cost of the GB per seconds depending on your architecture (0.0000166667 is the x86 Price)

filter @type = "REPORT"
| stats 
count(@type) as countInvocations , 
sum(@billedDuration) / 1000 as allDurationInSeconds,
max(@memorySize)  / 1024 / 1024 / 1024 as memoryAllocated,
allDurationInSeconds * memoryAllocated * 0.0000166667 as totalCost,
totalCost / countInvocations as avgCostPerInvocation

Another way is using Cost and usage report with Athena, it’s a serverless way to analyze your cost usage.

Then create a query to find out the most usage/cost lambda by below query,

SELECT line_item_resource_id, sum(line_item_usage_amount) as usage_amount, sum(line_item_blended_cost) as paid_amount FROM "athena_cur_catalog”."your_account”
    where line_item_product_code='AWSLambda' and product_group='AWS-Lambda-Duration'
    and year='2021' and month='10'
    group by line_item_resource_id
    order by usage_amount desc
    limit 10;
Related