AWS Lambda Cold Start Cost Calculator: Pinging Lambda vs. AWS Fargate

In my previous post I indicated that I would perform a detailed cost calculation comparing AWS Lambda with pings (the “ping method”) to an AWS Fargate instance, so lets do that here. AWS also has a new service called provisional concurrency, which solves the cold start problem as well, and it costs $10/month per lambda function. If any of the costs I calculate in the exercise below are above that threshold, provisional concurrency should be looked at.

Cost for AWS lambda pinged at 1 minute intervals 24/7 for 1 month in us-west-2 as of March 3rd, 2022:

Memory: 512MB,
$0.0000000083/ms

Architecture: x86_64

$0.0000166667 for every GB-second

$0.20 per 1M requests

The average amount of time a lambda runs during these “pings” is 17.3070, round up to 18 ms, at least in my testing, for my lambda. Some pings are 2ms, some are 110ms, so just took the average here over an hour for the calculations.

The number of requests per month at 1 every minute would be 60 minutes * 24 hours in a day * 31 days in a month = 44,640.

To get cost per lambda: 44,640 * 18ms * $0.0000000083/ms = 0.006669216. Lets round that up to 1 cent.

However, what about cloudwatch? There is a cost there, as each invocation logs the following to cloudwatch:

END RequestId: 68641b2c-cc94-40a6-9159-3d79df156dd3

REPORT RequestId: 68641b2c-cc94-40a6-9159-3d79df156dd3       Duration: 14.47 ms          Billed Duration: 15 ms                Memory Size: 512 MB    Max Memory Used: 340 MB       

START RequestId: 68641b2d-0894-40a6-9159-3d79df156dd3 Version: $LATEST

What are the Cloudwatch costs? The logs can get more expensive I would think, and I don’t think there is a way to turn them off. If you use basic metrics, the default, there is no charge for these metrics:

“When your AWS Lambda function finishes processing an event, Lambda sends metrics about the invocation to Amazon CloudWatch. There is no charge for these metrics.”

https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html

Now, let’s calculate the cost of running a docker image in amazon Fargate:

per vCPU per hour$0.04048
per GB per hour$0.004445

This is for a Linux/X86 non-spot instance. Therefore, if you need 1 cpu for the month, and their default 20GB of ephemeral storage by default.

So if you start the fargate task, it needs to be running for the whole month, so here we go:

24 hrs/day * 30 days/month * $.04048 vCpu per hour + $.004445gb/hr * 20 GB * 24 hrs/day  * 30 days/month  = $93.2256

You can’t use less than that 20gb of “ephemeral” storage in fargate. So $93.2556 is a lot more than 1 cent in the lambda scenario, and more than using the lambda provisional concurrency method of $10/month.

The point that a lot of people, including  Lambda vs. Fargate: The Cost of Running 24/7 — Six Feet Up, miss is that AWS automatically stores the lambda instance and keeps it warm for up to 5 minutes without charging you. When factoring that in, using lambda and the “ping” method to keep it warm is the approach that is the least costly.