3.2 Lambda, Serverless Compute, and Container Services
Key Takeaways
- Lambda runs code without provisioning servers, scales automatically, and charges per invocation and duration — ideal for event-driven architectures.
- Lambda has a 15-minute maximum execution time, 10 GB memory limit, and 6 MB synchronous / 256 KB async payload limits.
- ECS (Elastic Container Service) runs Docker containers on EC2 or Fargate; EKS (Elastic Kubernetes Service) runs Kubernetes on EC2 or Fargate.
- Fargate is serverless compute for containers — you define CPU/memory, and AWS manages the underlying infrastructure.
- Choose Lambda for short event-driven tasks, Fargate for containerized apps without server management, and ECS/EKS on EC2 for maximum container control.
Lambda, Serverless Compute, and Container Services
Quick Answer: Lambda = event-driven, <15 min, no servers. Fargate = serverless containers, no server management. ECS on EC2 = Docker containers with full control. EKS = Kubernetes on AWS. Choose based on execution duration, control needs, and operational overhead tolerance.
AWS Lambda
Lambda runs code in response to events without provisioning or managing servers.
Lambda Key Specs
| Feature | Limit |
|---|---|
| Max execution time | 15 minutes (900 seconds) |
| Memory | 128 MB to 10,240 MB (10 GB) |
| vCPUs | Proportional to memory (1,769 MB = 1 vCPU) |
| Ephemeral storage | Up to 10 GB (/tmp) |
| Concurrent executions | 1,000 per Region (soft limit, can be increased) |
| Deployment package | 50 MB zipped, 250 MB unzipped, 10 GB with container image |
| Sync payload | 6 MB |
| Async payload | 256 KB |
| Environment variables | 4 KB total |
Lambda Event Sources
| Source | Invocation Type | Example |
|---|---|---|
| API Gateway | Synchronous | REST API endpoint |
| S3 | Asynchronous | Object uploaded/deleted |
| SQS | Polling | Process queue messages |
| DynamoDB Streams | Polling | React to table changes |
| EventBridge | Asynchronous | Scheduled tasks, event rules |
| SNS | Asynchronous | Pub/sub notifications |
| CloudWatch Logs | Asynchronous | Log processing |
| Kinesis | Polling | Stream processing |
| ALB | Synchronous | HTTP request handling |
Lambda Best Practices
- Keep functions small and focused (single responsibility)
- Set appropriate timeout (don't use 15 min for a 3-second function)
- Use environment variables for configuration
- Store dependencies in Lambda Layers for reuse
- Use Provisioned Concurrency to avoid cold starts for latency-sensitive functions
- Use Reserved Concurrency to limit execution and prevent throttling of other functions
Lambda Pricing
- Free tier: 1 million requests + 400,000 GB-seconds per month
- Per request: $0.20 per million requests
- Per duration: $0.0000166667 per GB-second
Amazon ECS (Elastic Container Service)
ECS is AWS's container orchestration service for running Docker containers.
ECS Launch Types
| Feature | EC2 Launch Type | Fargate Launch Type |
|---|---|---|
| Infrastructure | You manage EC2 instances | AWS manages infrastructure |
| Scaling | You manage Auto Scaling of EC2 | Automatic |
| Pricing | EC2 instance pricing | Per vCPU + memory per second |
| Control | Full access to host | No access to host |
| GPU support | Yes | Limited |
| Best for | Full control, GPU, large clusters | Simplicity, less operational overhead |
ECS Key Concepts
| Concept | Description |
|---|---|
| Task Definition | Blueprint for containers (image, CPU, memory, ports, env vars) |
| Task | Running instance of a Task Definition |
| Service | Maintains desired count of tasks, integrates with ELB |
| Cluster | Logical grouping of tasks/services |
Amazon EKS (Elastic Kubernetes Service)
EKS runs Kubernetes on AWS without installing or operating your own Kubernetes control plane.
| Feature | Detail |
|---|---|
| Control plane | AWS managed (multi-AZ, patched, upgraded) |
| Worker nodes | EC2 instances, Fargate, or both |
| Compatibility | Standard Kubernetes — all existing K8s tools work |
| Use case | Organizations already using Kubernetes |
AWS Fargate
Fargate is serverless compute for ECS and EKS — you specify CPU and memory, and AWS manages everything else.
| Feature | Detail |
|---|---|
| No servers | No EC2 instances to manage |
| Pricing | Per vCPU and memory per second |
| Scaling | Automatic based on task count |
| Security | Task-level isolation (each task runs in its own kernel) |
| Best for | Microservices, batch processing, web applications |
Choosing the Right Compute Service
| Scenario | Best Service |
|---|---|
| Event-driven, <15 min execution | Lambda |
| Containerized app, minimal ops | ECS or EKS on Fargate |
| Containerized app, full control needed | ECS or EKS on EC2 |
| Long-running stateful applications | EC2 |
| Batch processing (large scale) | AWS Batch (uses EC2 or Fargate) |
| GPU-intensive ML training | EC2 (P-series) or SageMaker |
On the Exam: "Least operational overhead for running containers" → Fargate. "Run code in response to S3 uploads" → Lambda. "Organization uses Kubernetes and wants to migrate to AWS" → EKS.
A company wants to run Docker containers without managing any servers or infrastructure. Which service should they use?
What is the maximum execution time for an AWS Lambda function?
A company is migrating an existing Kubernetes application to AWS. They want to use their existing Kubernetes tools and configurations. Which service should they use?