Serverless and Containers
Lambda, ECS, EKS and Fargate: running code without managing servers, and choosing the right compute.
What is it?
Serverless does not mean no servers - it means you do not provision, patch, or scale them. You provide code; the service runs it when needed and bills for use.
AWS Lambda runs a function in response to events (an HTTP request, a queue message, a file upload, a schedule). Key traits: you pay per invocation and duration, it scales automatically, and each invocation is short-lived. Invocations have limits - for example a maximum run time of 15 minutes and configurable memory - so long, heavy jobs belong elsewhere.
Containers package an app with its dependencies so it runs the same everywhere.
- Amazon ECS: AWS's own container orchestrator, simple and tightly integrated.
- Amazon EKS: managed Kubernetes, for teams that want the Kubernetes API and ecosystem.
- AWS Fargate: a serverless compute engine for containers; you define CPU and memory and it runs the tasks without you managing EC2 hosts (works with ECS and EKS).
- Amazon ECR: a registry to store container images.
Choosing compute: EC2 for maximum control, containers for portable packaged services, Lambda for event-driven short tasks.
More compute options
- AWS Batch: runs batch jobs at any scale. You submit jobs; Batch provisions the right amount of EC2 or Fargate capacity (including Spot) and queues them, so you do not manage a scheduler.
- Amazon Lightsail: simple virtual servers, containers, databases, storage and static IPs with predictable monthly pricing and a friendly console; a good fit for small websites, blogs and learning projects.
- Amazon ECR (Elastic Container Registry): a managed registry for storing, scanning and sharing container images used by ECS, EKS and Fargate.
- AWS Outposts: AWS infrastructure and services running in your own data center for hybrid needs.
Choosing compute: EC2 for full control of virtual servers, containers (ECS/EKS/Fargate) for packaged applications, Lambda for short event-driven code, Elastic Beanstalk for deploying web apps without managing the infrastructure details, and Lightsail for a simple fixed-price start.
Explain like I'm 10
EC2 is renting a whole kitchen: full control, but you clean and maintain it. Containers are meal-prep boxes that can be reheated in any kitchen. Lambda is ordering a single dish from a ghost kitchen - it appears when you ask and you pay just for that dish.
Examples
A Lambda handler
// Pure logic you can run here: the handler shape Lambda expects.
async function handler(event) {
const name = (event && event.name) || "world";
const message = "Hello, " + name + "!";
console.log("handled event", JSON.stringify(event));
return { statusCode: 200, body: JSON.stringify({ message }) };
}
handler({ name: "Zykit" }).then((res) => console.log(res));On AWS you would export this as the handler. The function receives an event and returns a response; the runtime and servers are not your concern.
Deploying a function (CLI)
zip function.zip index.js
aws lambda create-function \
--function-name hello \
--runtime nodejs22.x \
--handler index.handler \
--zip-file fileb://function.zip \
--role arn:aws:iam::111122223333:role/lambda-basic-execution
aws lambda invoke --function-name hello --payload '{"name":"Zykit"}' \
--cli-binary-format raw-in-base64-out out.json && cat out.jsonCheck the currently supported runtime names before deploying; they change as versions retire.
Which compute service?
Situation Pick
----------------------------------------------- ----------------
Legacy app needing OS access EC2
Short event-driven task (resize an upload) Lambda
Microservices, want AWS-native simplicity ECS (+ Fargate)
Team standardised on Kubernetes EKS
Containers but no hosts to patch FargateHow it works
When an event arrives, Lambda finds or creates an execution environment, loads your code, and runs the handler. The first call in a new environment includes a cold start delay. Concurrency scales out by adding environments. You pay for requests and execution time.
ECS and EKS schedule containers onto capacity. With EC2 launch types you manage the hosts; with Fargate AWS supplies the capacity per task.
Event (HTTP, S3, SQS, schedule)
|
v
[ Lambda ] -- scales per event, short-lived
Container image (ECR) --> [ ECS or EKS ] --> capacity:
|-- EC2 hosts you manage
'-- Fargate (managed)Why does it exist?
Managing servers is undifferentiated work. Serverless and managed containers let teams focus on code, scale to zero when idle, and pay for useful work rather than idle hosts.
When to use it
Lambda: APIs, glue code, file processing, scheduled jobs, event handlers. Containers: long-running services, portable workloads, microservices. Fargate: containers without cluster-host management.
When not to use it
Avoid Lambda for jobs longer than its time limit, constant heavy compute where reserved servers would cost less, or workloads needing special OS or hardware. Avoid EKS if your team has no Kubernetes need; ECS is simpler.
Common mistakes
Treating Lambda as stateful - local memory and disk do not persist reliably between invocations.
Giving the function an over-broad IAM role.
Ignoring cold-start impact on latency-sensitive paths.
Choosing Kubernetes for a handful of services without needing it.
Forgetting that unbounded Lambda concurrency can overwhelm a downstream database.
Using Batch for a latency-sensitive web request path; it is for queued, asynchronous jobs.
Practice exercises
- Easy:
Run the handler example here and change the event to include a different name.
- Medium:
Decide between Lambda, Fargate, and EC2 for: nightly 30-minute ETL, image thumbnailing on upload, a Windows legacy app.
- Medium:
Explain the difference between ECS on EC2 and ECS on Fargate in terms of who patches the hosts.
- Hard:
A Lambda connected to a relational database causes connection exhaustion at peak. Propose two mitigations.
Interview questions
What is AWS Lambda?
A serverless compute service that runs code in response to events and bills by invocations and duration, scaling automatically.
ECS vs EKS?
ECS is AWS's proprietary orchestrator; EKS is managed Kubernetes. EKS suits teams that want Kubernetes portability and tooling.
What does Fargate remove?
The need to provision and manage the underlying servers for your containers.
Exam-style: A company wants to run code without managing servers, only when an event occurs. Which service?
AWS Lambda.
Exam-style: Which service stores Docker container images?
Amazon Elastic Container Registry (ECR).
Exam-style: Which service is best for a small business that wants a simple WordPress site with predictable monthly cost?
Amazon Lightsail.
Which service runs queued batch computing jobs without you managing a scheduler?
AWS Batch.