ARQ Português

Architecture // Volume 2 of the series

EC2, Fargate or Lambda? Three questions before you choose (and the record that saves you an argument six months later)

By André Romão Quinhoneiro · All articles

"Let's use Lambda for everything" and "let's just spin up a few EC2 instances" are two sentences I've seen end a meeting too early. Choosing where an application runs on AWS is an architecture decision, and a decision needs criteria and a record.

This post summarizes part of chapter 2 of Cloud Solutions Architect on AWS, from the From Journey to Practice series.

A managed service is not an ownerless service

Before comparing, a warning. Managed services move the responsibility line, but they don't erase your side of it. With PostgreSQL on RDS, AWS handles the operating system and engine patches. Your team still owns access, network, encryption, the data model, cost, and testing that a backup actually restores.

Rule of thumb: when comparing a managed option with a self-built one, list what your team stops doing and what it keeps doing. If the second list is empty, the analysis is incomplete.

The three models, side by side

Criterion EC2 ECS on Fargate Lambda
Control over the environment High Medium Low
Operating system work Team AWS AWS
Best usage pattern Continuous, or OS dependencies Continuous, web and API Event-driven, short, irregular
Cost when idle Pay for running instances Pay for running tasks Close to zero
Cost under high, steady load Predictable Predictable Can get high
Change needed for a legacy app Little Medium (containerize) Large (rewrite as functions)

Three questions that remove most of the doubt

  1. What is the usage pattern? Always on (web, API) points to containers or VMs. Event-driven, short and irregular points to functions.
  2. Does it depend on something in the OS? An agent, a native library or a machine-bound license that won't fit in a container points to EC2.
  3. Does it fit the execution limits? Lambda has a maximum duration per invocation (currently 15 minutes), plus memory and package size limits. These values change: check the official documentation and write down the date you checked next to the decision.

The answers give you a starting point, not a final decision.

It doesn't have to be just one

Different workloads can use different models. In a fictional online store:

  • web app and API, always on: containers (ECS on Fargate);
  • nightly import of a price spreadsheet: a scheduled function, if it fits the limits;
  • order confirmation email: a function triggered by a queue message.

Resolve the "if" with a proof of concept

Compare two PoC goals:

  • Weak: "let's try Lambda for the import".
  • Strong: "we need to know whether importing the largest spreadsheet from the last six months finishes in under 10 minutes inside a function, using the library we use today".

The strong goal says what to measure, with which data, and which result keeps or rules out the option. Define the success criterion before you start, keep it short, and record the result, even when the answer is "it doesn't work". If the PoC uses AWS resources, use a lab account, a budget alert and a cleanup script: a forgotten PoC left running is a classic surprise bill.

Record the choice

Six months from now, someone will ask "why didn't we use Lambda for everything?". The answer needs to exist in writing:

ADR-002: execution model for the web application
Context:       web app and API active all day; campaign peak estimated
               at ~5x (assumption, not yet measured); team of 4 with no
               experience running servers.
Options:       EC2 with Auto Scaling; ECS on Fargate; Lambda.
Decision:      ECS on Fargate.
Why:           no OS to maintain; continuous load does not favor Lambda;
               the current app runs in a container with small changes
               (PoC-01).
Rejected:      EC2, due to OS upkeep with a small team;
               Lambda, due to rewriting the app as functions.
Consequences:  the team now owns the image (base, scanning, updates);
               cost grows with the number of running tasks.
Revisit if:    the measured peak differs a lot from the estimate; or an
               OS dependency appears that won't fit in a container.

Notice two lines: Rejected (the reason for "no" matters as much as the reason for "yes") and Revisit if (every decision has an expiry date).


If you want the full path, from a vague request to a complete architecture package, with labs and case studies, it's in Cloud Solutions Architect on AWS, volume 2 of the From Journey to Practice series: https://www.amazon.com/dp/B0HMZGM278

How does your team record architecture decisions today? ADRs, a wiki, nothing at all?

This post summarizes a chapter of Cloud Solutions Architect on AWS, volume 2 of the From Journey to Practice series.