Lambda Vs EC2: Which compute service is cheaper?

An objective look at comparing EC2 and Lambda for price under different conditions

Many people tend to think Lambda costs more than EC2 on average.

A lot of that is incorrect.

Is EC2 cheaper than Lambda because can scale and charge you more?

Is Lambda cheaper than EC2 because it is a pay-as-you-go service?

The real answer is: it depends on your usage.

In this post, I’ll explain in simple terms how your usage directly influences which service will be more cost-efficient.

By the end, you’ll know exactly which service to use for your use case and which one will cost you less.

Lambda Pricing

These are the prices for (us-east-1 region) Lambda function executions.

EC2 Pricing

These are the starting prices for on-demand (standard) EC2 instances.

Price Comparision 1

Imagine you’re building an e-commerce app. Your app is getting 3 million requests a month. Let’s assume each request has an execution time of 150 milliseconds.

Let’s see how much this would cost us with Lambda and then with EC2 (assuming the t3.large instance).

Lambda

Total duration = 3 million * 150 ms = 450,000 GB/s
Total execution cost = 450,000 * 0.0000166667 = $ 7.5
Total request charge = 3 millon * 0.2 = $ 0.6

Our total cost = $7.5 + $0.6 = $8.1/month

EC2

Now with the t3.large instance:

Total duration = 24h/day * 30day = 730h

Our total cost = 730 * $0.0832 = $60.7/month

Clearly, in this scenario, of relatively low usage, Lambda offers a much cheaper service compared to EC2. And this is expected from a ephemeral compute nature.

Price Comparision 2

Now imagine your traffic goes up to 12 million requests a month and your average execution times are longer - 3 seconds instead of 150ms (due to more complex server-side logic).

Let’s see how that changes are cost:

Lambda

Total duration = 3 million * 3000 ms = 9,000,000 GB/s
Total execution cost = 36,000,000 * 0.0000166667 = $150
Total request charge = 3 millon * 0.2 = $0.6

Our total cost is now $150 + $0.6 = $150.6/month

EC2

If we upgrade our EC2 to the next instance size t4g.xlarge which charges us $0.1664 an hour.

That totals to $98.1/month.

In this scenario, EC2 comes out to much cheaper.

And if we needed more compute power, we can keep the same instance and be charged the same price of $98/month with EC2. However, our Lamba function would increase beyond $150/month.

The takeaway here is this:

Lambda is designed for ephemeral computing. Short bursts of invocations will usually cost much less than EC2 instances. But as soon as the traffic becomes more consistent and processes run longer, you’ll start paying more than EC2.

This is why Lambda excels at event-driven tasks and ephemeral compute use cases, but falters on more consistent usage.

Further Reading:

“AWS Lambda Pricing in Context - A Comparison to EC2”: https://www.trek10.com/blog/lambda-cost

“Is AWS Lambda Always Most Cost-effective Choice?”: https://towardsaws.com/is-aws-lambda-always-the-best-option-3b7a16549ce0

I hope you liked this week’s newsletter.

You can find us on Medium for more articles and on LinkedIn to connect with me.

Until next week.

The Cloud Economist