Task 2.1 · 18 min read
Serverless and Containers: Lambda, Fargate, ECS and EKS
Services in this lesson
- AWS Lambdain scope
- AWS Fargatein scope
- Amazon ECSin scope
- Amazon EKSin scope
- Amazon ECRin scope
- Amazon ECS Anywherein scope
- Amazon EKS Anywherein scope
What the exam tests
- When serverless fits. AWS Lambda suits short, event-driven, spiky work, within hard limits. A standard function gets a 15-minute timeout and 10,240 MB of memory. A synchronous call, where the caller waits for the answer, carries at most 6 MB.
- When containers fit, and which orchestrator runs them. Pick Amazon ECS for AWS-native simplicity and Amazon EKS when the team already runs Kubernetes, an open-source system for running containers. Add AWS Fargate when nobody should manage servers.
- Moving an existing application into containers. Package the image, store it in Amazon ECR (Elastic Container Registry), run it as an ECS service or EKS deployment, and move state out of the container.
- Designing stateless microservices. Any copy can then serve any request, and the platform can add or remove copies freely.
- Using Lambda concurrency controls correctly. Concurrency is how many copies of a function run at once.
- Reserved concurrency sets aside part of the account's pool for one function, which caps or guarantees its share.
- Provisioned concurrency keeps copies initialized ahead of time. It avoids cold starts, the setup delay of a fresh copy, up to the provisioned number.
The core concept
Every compute choice trades control for less operations work: the more AWS runs, the less you can change. The exam's default answer is the most managed option the requirements allow, stopping at the first hard limit.
An analogy: where you stay in a new city
Picture moving to a new city for work. You have three options:
- Buy a house. You fix the roof, pay the bills and change every light bulb. That's plain Amazon EC2 (virtual servers you run yourself).
- Rent a serviced apartment. The building is maintained for you, and you only choose the furniture and layout. You can't rewire it or install heavy machinery, though. That's AWS Fargate: you describe the container, and AWS runs the machine under it.
- Book a hotel room. You arrive with a suitcase, and everything else is handled. But checkout comes fast, and you can't stay past it. That's AWS Lambda: you bring only code, and each run ends at 15 minutes.
Where the analogy breaks: a hotel keeps your room between nights. Lambda may reuse, freeze or recycle an environment between requests, and a Fargate task can be replaced at any time. Either way, anything left on local disk or in memory can vanish.
Walk four workloads through the diagram
Mateo runs the platform team at Quillfeather Prints, an online photo-print shop. He has four workloads to place. He starts each one at the top of the diagram and moves down only when a requirement stops him.
- Thumbnails. A customer uploads a photo to Amazon S3 (object storage), and a small thumbnail must appear. Each job is event-driven and takes seconds. It goes to Lambda, which scales per event and runs nothing between events.
- The nightly print-batch job. It runs for 45 minutes, so it fails the 15-minute test.
- It already ships as a container, an app packed with everything it needs to run.
- Nobody on the team knows Kubernetes, so it goes to Amazon ECS (Elastic Container Service), AWS's own orchestrator. An orchestrator places containers on machines and restarts them when they fail.
- Nothing needs special hardware, so it runs on Fargate.
- Image upscaling. This model needs GPUs (graphics processing units). Fargate can't provide them, so the diagram sends it to ECS on EC2 capacity: GPU instances that Mateo's team runs.
- The partner portal. A partner team already deploys pods (Kubernetes' unit of one or more containers) with manifests and Helm charts (packaged Kubernetes configs). Forcing them onto ECS would mean rewriting those files.
- They pick Amazon EKS (Elastic Kubernetes Service).
- They run it on Fargate. That works because their charts need no DaemonSets (a pod on every node) and no privileged pods.
A workload that must keep its own operating system (OS) never enters the container branch. It stays on plain EC2.
The rules the exam tests
- Plain EC2: you patch the OS, size the fleet and keep the processes alive.
- ECS or EKS on EC2: the orchestrator places and restarts containers, but you still own the nodes (the instances containers run on).
- Fargate: you hand over the nodes too. On ECS you manage only the task definition. On EKS you still own the cluster version and add-ons, even on Fargate.
- Lambda: you hand over everything except your code and its configuration.
- Stop at the first hard limit. A 40-minute job can't be one standard Lambda invocation. A GPU workload can't use Fargate. A Kubernetes team shouldn't be forced onto ECS.
- If no limit applies, pick the more managed option.
Deep diveLambda's hard limits and concurrency
- Timeout: a standard Lambda function invocation stops at 900 seconds (15 minutes). Lambda Managed Instances allow up to 90 minutes for asynchronous and most event source mapping invocations, but exam questions assume the 15-minute limit.
- Memory: up to 10,240 MB.
- Payload: 6 MB for a synchronous call.
- Package size: a container image can be up to 10 GB. A .zip package is capped at 250 MB unzipped.
- Concurrency is how many copies of a function run at once. The default quota is 1,000 per account per Region. New accounts can start with a lower quota.
- Reserved concurrency reserves part of that pool for one function and also caps it there.
- Provisioned concurrency keeps execution environments (the sandboxes that run your code) initialized ahead of time. Requests up to the provisioned number then skip the cold start.
- Provisioned concurrency adds no capacity. It counts against the same account quota and doesn't cap the function. You configure it on a published version or alias, not
$LATEST.
Deep diveWhy Fargate can't run GPU work
Fargate task definitions don't accept the gpu parameter. ECS runs GPU workloads on EC2 GPU container instances instead, and EKS runs them on EC2-backed nodes.
Deep diveWhat EKS on Fargate can't run
EKS on Fargate doesn't support DaemonSets, privileged containers, GPUs or Amazon EBS (Elastic Block Store) volumes. A workload that needs any of them runs on EC2-backed nodes.
The walkthrough placed each workload, but two more ideas make those placements work: stateless design and a repeatable migration path.
Stateless is what makes scaling out work
A stateless service keeps nothing between requests on its own disk or memory. Every piece of state lives somewhere all copies can reach:
- Session data goes to a shared store such as Amazon DynamoDB or Amazon ElastiCache, as 2.1c explains.
- Files go to Amazon S3 or Amazon EFS (Elastic File System, a shared network file system).
- Configuration goes to environment variables or AWS Secrets Manager, a managed store for passwords and keys.
Lambda execution environments and Fargate tasks can be replaced at any time. Anything left on them is temporary by design.
Stateful parts such as databases and queues are fine. They just belong in managed services, not inside the compute copies.
Moving an application into containers
The steps are the same whatever the orchestrator:
- Write a Dockerfile, the build recipe for the application's image.
- Build the image and push it to Amazon ECR, AWS's private image store.
- Describe how to run it: an ECS task definition or a Kubernetes manifest.
- Run it as a long-lived service behind a load balancer (2.1c).
Move state and configuration out of the container on the way. If some servers must stay on premises, Amazon ECS Anywhere and Amazon EKS Anywhere run the same workloads on your own hardware. You keep the same tooling.
Decision guide
| If the question says… | Reach for… | Because… |
|---|---|---|
| "runs when a file lands in S3", "processes each queue message", short tasks, idle most of the day | AWS Lambda triggered by the event | Scales per event, runs nothing between events. Each run must finish within 15 minutes |
| "job runs 45 minutes", "long-running process", already containerized, no servers to manage | Amazon ECS task on AWS Fargate | No 15-minute limit, and no nodes to patch |
| "team already uses Kubernetes", "Helm charts", "portable across clouds and on premises" | Amazon EKS (with Fargate or managed node options) | Keeps the Kubernetes API and tooling. ECS would mean rewriting deployment files |
| "containers", "LEAST operational overhead", no Kubernetes requirement | Amazon ECS on AWS Fargate | AWS-native orchestrator with no control plane or nodes for you to run |
| "GPU", "specific instance type", "privileged containers", "daemon on every host" | ECS or EKS on EC2 capacity | GPUs need EC2 GPU container instances. EC2 also gives instance and host control |
| "run the same containers in the company's data center" | Amazon ECS Anywhere or Amazon EKS Anywhere | Registers on-premises servers, so one toolset manages both places |
| "monolith on premises", "move to AWS quickly", "minimal code changes", wants containers | Containerize, push to Amazon ECR, run as an ECS service on Fargate behind an Application Load Balancer (ALB) | The code stays. Only packaging and state handling change |
| "unpredictable cold-start latency is unacceptable" for a Lambda API | Provisioned concurrency | Up to the provisioned number, warm environments answer at once. Reserved concurrency only caps |
| "one function must not starve the others" or "don't overwhelm the database" | Reserved concurrency on that function | Reserves and caps its share of the account pool (1,000 per Region by default) |
| "dependencies are 2 GB", "already build container images" | Lambda function packaged as a container image | Images can be up to 10 GB. A .zip is capped at 250 MB unzipped |
| "users lose their session when a task is replaced" | Store sessions in DynamoDB or ElastiCache | Tasks and execution environments are disposable, so state on them disappears |
Correct pattern
REFERENCE PATTERN: a migrated web application as an ECS task definition for Fargate. Each part has a home outside the container:
- The image lives in Amazon ECR.
- Each task gets its own network interface (
awsvpcmode). - Logs go to Amazon CloudWatch, and the database password comes from AWS Secrets Manager.
- Shared uploads live on Amazon EFS instead of the container's disk.
Run it as an ECS service with a desired count of at least two, spread across two Availability Zones (AZs), behind an Application Load Balancer (ALB).
{
"family": "orders-web",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "1024",
"memory": "2048",
"executionRoleArn": "arn:aws:iam::<account-id>:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::<account-id>:role/orders-web-task",
"containerDefinitions": [
{
"name": "web",
"image": "<account-id>.dkr.ecr.<region>.amazonaws.com/orders-web:1.4.2",
"portMappings": [{ "containerPort": 8080, "protocol": "tcp" }],
"secrets": [
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:<region>:<account-id>:secret:orders-db"
}
],
"mountPoints": [
{ "sourceVolume": "uploads", "containerPath": "/srv/uploads" }
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/orders-web",
"awslogs-region": "<region>",
"awslogs-stream-prefix": "web"
}
}
}
],
"volumes": [
{
"name": "uploads",
"efsVolumeConfiguration": {
"fileSystemId": "<efs-id>",
"transitEncryption": "ENABLED"
}
}
]
}
What makes it the exam-correct shape:
FARGATEandawsvpc. There are no nodes to manage. Each task gets its own elastic network interface and security group.- Two roles. The execution role lets ECS pull the image and read the secret. The task role is what the application code uses to call AWS. Least privilege for each is taught in 1.1.
- No state in the container. Secrets come from Secrets Manager and files from EFS. Any task can be replaced or added without losing data.
- A pinned image tag. Every task in the service runs the same version. A rollback is a task definition change.
Traps
| The trap | Why it fails | Correct pattern |
|---|---|---|
| Lambda for a 40-minute video transcode because "serverless is least overhead" | Lambda is the reflex serverless answer. But a standard function stops at 900 seconds (15 minutes) | ECS task on Fargate (or AWS Batch, a managed batch-job service, on Fargate for queued jobs), or split the work into steps under 15 minutes |
| EKS "for flexibility" when no one on the team runs Kubernetes | Kubernetes sounds standard. It adds cluster upgrades, add-ons and manifests nobody asked for | ECS on Fargate unless the stem mentions Kubernetes, existing manifests or Helm, or portability |
| Fargate for a machine-learning inference service that needs GPUs | Fargate is usually the least-overhead answer. But ECS runs GPU work on GPU container instances | ECS or EKS on GPU-backed EC2 capacity |
Keeping user sessions or uploaded files on the container or in Lambda's /tmp | Works on one copy in testing. Copies get replaced, so the next request may miss the data | Sessions in DynamoDB or ElastiCache, files in S3 or EFS. Keep compute stateless |
| Provisioned concurrency to fix throttling at the account's concurrency limit | Sounds like "more capacity". It only pre-warms environments and counts against the same limit | Request a concurrency quota increase, or put an Amazon SQS (Simple Queue Service) queue in front so bursts wait instead of failing |
| Running Docker on a hand-built EC2 instance to "move to containers" | Feels like the smallest change. You still patch the host, restart containers and handle scaling | ECS (or EKS) as the orchestrator, ideally on Fargate |
Compare
| Aspect | AWS Lambda | Amazon ECS on Fargate | Amazon ECS or EKS on EC2 | Amazon EKS |
|---|---|---|---|---|
| Unit you deploy | Function (code .zip or container image) | Task definition (one or more containers) | Task or pod on instances you run | Kubernetes pods, deployments, Helm charts |
| Longest single run | 15 minutes per invocation | Runs until the container exits | Runs until the container exits | Runs until the container exits |
| You manage | Code, memory, concurrency settings | Task size, image, networking | Instances, AMIs (machine images), patching, capacity | Cluster version and add-ons, plus nodes unless on Fargate or Auto Mode |
| Scaling trigger | Each event or request, automatic | Service auto scaling on metrics or queue depth | Service scaling plus instance capacity | Kubernetes autoscalers |
| GPU workloads | Use EC2-backed containers instead | Not supported. Use EC2 GPU instances. | Yes | Yes, on EC2-backed nodes |
| Stem keywords | "event-driven", "spiky", "per request" | "containers", "no servers", "least overhead" | "GPU", "instance type", "host control" | "Kubernetes", "Helm", "portability" |
EC2 instance families, Auto Scaling groups and right-sizing are taught in 3.2 Compute performance. Compute pricing models are in 4.2 Compute cost.
Worked exam scenario
Exam scenario
A logistics company runs a Java route-planning application in Docker containers on two on-premises servers. Each night a batch step inside the same application recalculates routes for about 90 minutes. The company wants to move to AWS, stop managing servers and operating systems, and keep using its existing container images. The operations team has no Kubernetes experience. Which solution meets these requirements with the LEAST operational overhead?
- A.Push the images to Amazon ECR and run the application as an Amazon ECS service on AWS Fargate, with the nightly batch as a scheduled ECS task on Fargate.
- B.Rewrite the application as AWS Lambda functions and invoke the nightly batch with an EventBridge schedule.
- C.Push the images to Amazon ECR and run them on an Amazon EKS cluster with a managed node group of EC2 instances.
- D.Launch EC2 instances from an Amazon Linux AMI, install Docker, and run the containers with a cron job for the nightly batch.
Show the walkthrough
- Underline the constraints. Existing container images, a 90-minute batch, no servers or operating systems to manage, no Kubernetes skills, least overhead.
- Eliminate B. A standard Lambda invocation stops at 15 minutes. The 90-minute batch doesn't fit, even when an Amazon EventBridge schedule starts it. "Rewrite the application" also ignores the requirement to keep the existing images.
- Eliminate C. EKS would run the containers, but the team has no Kubernetes experience. Managed node groups are also still EC2 instances the team is responsible for.
- Eliminate D. Self-managed EC2 with Docker and cron means patching the OS and restarting failed containers yourself. That's the opposite of the stated goal.
- Choose A. ECS needs no Kubernetes knowledge, and Fargate removes servers and operating systems. ECR stores the existing images unchanged, and a scheduled task runs the batch until it exits.
Answer: A
Check your understanding
Build it
The lab for task 2.1 is the Loose coupling lab. It builds the messaging layer from 2.1a: SNS fan-out to SQS queues with a dead-letter queue.
Those queues are exactly what a Lambda function or an ECS service from this lesson would consume. Messages can arrive more than once, so use the lab to see why a stateless, idempotent consumer matters. An idempotent consumer gives the same result however many times it handles a message.
Key takeaways
- Every compute choice trades control for operations work. The exam's default is the most managed option the stated requirements allow.
- Lambda fits short, event-driven work: 15 minutes per standard invocation, up to 10,240 MB of memory, and 1,000 concurrent executions per account per Region by default.
- Use reserved concurrency to cap or guarantee a function's share, and provisioned concurrency to avoid cold starts up to the provisioned number.
- Containers go to ECS unless the stem says Kubernetes. Run them on Fargate unless the stem needs GPUs or host-level control.
- Keep compute stateless: sessions, files and secrets live in managed services, so any copy can serve any request.
- Pick the most managed compute whose limits the workload fits: Lambda for short event-driven code, Fargate for containers without servers, EC2-backed ECS or EKS for control or hardware serverless lacks.
Hands-on lab: Fan Out Orders with SNS, SQS and a Dead-Letter Queue
Sources
- https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-best-practices.html
- https://docs.aws.amazon.com/lambda/latest/dg/configuration-timeout.html
- https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html
- https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html
- https://docs.aws.amazon.com/lambda/latest/dg/services-sqs-configure.html
- https://docs.aws.amazon.com/decision-guides/latest/decision-guides/choosing-aws-container-service.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/fargate-tasks-services.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-gpu.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/efs-volumes.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-anywhere.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/capacity-launch-type-comparison.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/scheduling_tasks.html
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-using-sqs-queue.html
- https://docs.aws.amazon.com/eks/latest/userguide/fargate.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_services.html
- https://docs.aws.amazon.com/lambda/latest/dg/provisioned-concurrency.html
- https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html
- https://docs.aws.amazon.com/eks/latest/userguide/helm.html
Last reviewed