When is EC2 better choice than AWS Lambda

by INVOKE Team

AWS EC2 vs Lambda

image courtesy https://www.simform.com/

Two well known and most used AWS cloud resources are EC2 and Lambda functions. There are fans for both solutions, as well as pros and cons in using these based on your requirements and use cases. Note that, we strongly believe that no one solution fits or solves every problem . So, we are not advocating for one solution or other. There was a question on Quora a few weeks ago, which precisely touched this topic and user u\drch tried to address this question in a very well articulated manner. Most of this blog post content is his/her answer.

When we have alternative solutions available, the choice of a best solution essentially falls back to how effectively we can utilize that solution compared with others. How much value we can get out of the solution, this value in cloud computing will be money paid vs how much busy the resources are kept.

On the pure computing cost, AWS EC2 is much cheaper. The amount of money you pay to the instance compute cost will be fixed (mostly except if they are burstable) based on the type of instance. The difference is how effectively your compute capacity is used. How busy you can keep the instance.

The advantage of Lambda (and most serverless services) is that you don't pay for idle resources. If your Lambda isn't actively handling a request, you don't pay anything, whereas an idle or under-utilized EC2 instance costs you the same as one that is max'd. The purchasing options (RIs and Savings Plans) are more attractive for EC2.

Typical billing for Lambda is per GB of memory per second rate . It's not a perfect basis for comparison, but here's what that looks like compared to compute-optimized and general purpose instances.

Cost / GB / Hour - on-demand

  1. Lambda: $0.06
  2. c5: $0.0425
  3. m5: $0.012
  4. m6g:$0.0096

Cost / GB / Hour - Savings Plans 1 year All Up-front

  1. Lambda: $0.05
  2. c5: $0.0144
  3. m5: $0.0082
  4. m6g:$0.0066

So if you have a consistent load and can fully utilize one or more instances, you can get up to an 87% discount compared to Lambda, and even more with a 3-year commitment. Spot instances can be even cheaper. If you use cloud cost and resource optimization tools, your savings can be even more.

For example, you can introduce dynamic start/stop the instance solutions instead of using schedulers to keep the resource uptime to only when they are needed, like Lambda. This will eliminate idle resource costs and you get the bill for the time your instance is being used. If you start using spot instances with these on-demand start and stop capabilities the cost of running will be a lot lesser than using Lambda.

So when Lambda could be a good choice? Lambda shines on workloads that need to scale quickly (“quickly” here could be context dependent and some users not happy with this latency too, there are a lot of debates over the internet on Lambda and keeping it warm and other approaches, which are not point of discussion to this topic). A typical EC2 setup is going to be taking metrics every 5 minutes and scale up when it's had 2 or 3 of those that are over a threshold, which means you're starting to scale up after 15 minutes of increasing load. For large systems or systems that can scale gradually, this is ok.

Lambda can scale from zero to 1000 or 3000 concurrent requests in seconds (depending on the region), and from there add an additional 500 concurrency every minute, without you having to add any metrics, monitoring, or logic. The costs scale down instantly when the load subsides, whereas your scaling-in on EC2 is usually gradual and conservative, so you end up with a lot of under-utilized instances during that time.

If you are using Lambda as service layer for user interface, few other things to point out are:

  1. Lambda has a per request price, and so do the services you'll probably use in front of it (API Gateway, CloudFront) could also add up to the cost.
  2. Lamba bills for 100ms even if the request is < 100ms. If your functions are returning cached or computed responses that are more in the 10-20ms range, you could be paying a LOT more than you would on EC2.

In summary, Lambda and EC2 have their own place in the computing world. Cost savings perspective EC2 could give you more, if can optimally use the capacity otherwise you need to carefully examine to pick the best one fits your need.