Serverless and Containers
AWS Lambda, Amazon ECS, Amazon EKS, AWS Fargate, and AWS Elastic Beanstalk
1. Containers on AWS
What is a Container?
Containers are a method of operating system virtualization. They package an application's code, configurations, and dependencies into a single object. Unlike virtual machines, containers do not contain a full guest operating system. They share the host OS kernel and run as resource-isolated processes.
Docker is the software platform that packages software into containers. You create a container from an image, which is a template that holds everything the software needs to run: libraries, system tools, code, and runtime.
Amazon Elastic Container Service (Amazon ECS)
Amazon ECS is a highly scalable container management service that supports Docker containers. It orchestrates the running of containers on a cluster and maintains the fleet of nodes that run them.
To run an application on ECS, you create a task definition. This text file describes one or more containers (up to ten) that form your application. It specifies which container image to use, which ports to open, and what data volumes to mount. A task is the instantiation of a task definition within a cluster.
- EC2 launch type — You manage the EC2 instances in the cluster. Provides granular control over instance type, networking, and scaling.
- Fargate launch type — AWS manages the cluster infrastructure. You only package your application, specify CPU and memory, and launch. No servers to provision or scale.
Amazon Elastic Kubernetes Service (Amazon EKS)
Amazon EKS is a managed Kubernetes service. Kubernetes is open source software for container orchestration that deploys and manages containerized applications at scale.
EKS makes it easy to run Kubernetes on AWS without installing or maintaining your own Kubernetes control plane. It is certified Kubernetes conformant, so applications that run on upstream Kubernetes are compatible with EKS. EKS automatically manages the availability and scalability of cluster nodes, detects unhealthy control plane nodes, and replaces them.
Amazon Elastic Container Registry (Amazon ECR)
Amazon ECR is a fully managed Docker container registry. It makes it easy to store, manage, and deploy Docker container images. ECR is integrated with both ECS and EKS. Images are encrypted at rest.
2. Serverless with AWS Lambda
AWS Lambda is an event-driven, serverless compute service. You upload your code as a Lambda function and set it to run on a schedule or in response to an event. Your code runs only when triggered. You pay only for the compute time you consume.
- No server provisioning or management
- Built-in fault tolerance across multiple Availability Zones
- Automatic scaling from a few requests per day to thousands per second
- Pay per request and per 100 milliseconds of compute time
- Supports multiple languages: Python, Node.js, Java, C#, Go, Ruby, PowerShell
Lambda Event Sources
Lambda functions are triggered by event sources. Common event sources include:
- Amazon S3 (object created, object deleted)
- Amazon DynamoDB (table updates)
- Amazon SNS (notifications)
- Amazon SQS (messages in a queue)
- Amazon API Gateway (HTTP requests)
- Application Load Balancer (HTTP/HTTPS requests)
- Amazon CloudWatch Events (scheduled or event-based triggers)
Lambda automatically monitors functions through Amazon CloudWatch and stores logs in CloudWatch Logs.
Lambda Limits
| Limit | Value |
|---|---|
| Maximum execution time (timeout) | 15 minutes |
| Maximum memory allocation | 10,240 MB |
| Concurrent executions per Region (soft limit) | 1,000 |
| Deployment package size (unzipped) | 250 MB |
Lambda Use Case Examples
Two classic Lambda patterns appear frequently on the exam:
Schedule-Based: Start and Stop EC2 Instances
A CloudWatch Event is scheduled to trigger a Lambda function at a specific time. The function uses an IAM role with permissions to stop (or start) EC2 instances. Common pattern: stop dev/test instances at night, restart them in the morning — reducing runtime costs by up to 65%.
- CloudWatch Event schedule triggers at 10 PM.
- Lambda function runs with an IAM role that allows
ec2:StopInstances. - EC2 instances enter the stopped state (no compute charges).
- A second CloudWatch Event at 7 AM triggers a Lambda function with
ec2:StartInstances.
Event-Based: Create Thumbnail Images
Amazon S3 detects an object upload and invokes a Lambda function. The function reads the uploaded image, creates a thumbnail using graphics libraries, and saves it to a target S3 bucket. No servers to manage; you pay only when an upload triggers the function.
- User uploads an image to the source S3 bucket.
- S3 detects the object-created event and invokes Lambda, passing the bucket name and object key.
- Lambda assumes its execution role, reads the object, generates a thumbnail, and writes it to the target bucket.
3. Platform as a Service: AWS Elastic Beanstalk
AWS Elastic Beanstalk is a platform as a service (PaaS) that facilitates the quick deployment, scaling, and management of web applications. You upload your code, and Elastic Beanstalk automatically handles deployment, capacity provisioning, load balancing, automatic scaling, and health monitoring.
- Supports Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker
- No additional charge for Elastic Beanstalk itself; you pay only for the underlying AWS resources
- You retain full control over the AWS resources that power your application
- Ideal when you want to focus on code, not infrastructure
4. Decision Guide
| Use Case | Service | Why |
|---|---|---|
| Run code in response to events, pay per execution | AWS Lambda | No servers to manage; millisecond billing |
| Run Docker containers with AWS managing the cluster | Amazon ECS with Fargate | No EC2 instances to provision or scale |
| Run Docker containers with full control over the hosts | Amazon ECS with EC2 | Granular control over instance type and networking |
| Use Kubernetes on AWS without managing the control plane | Amazon EKS | Managed Kubernetes, certified conformant |
| Deploy a web app quickly without managing infrastructure | AWS Elastic Beanstalk | PaaS with auto-deployment, scaling, and monitoring |
| Store and manage Docker images | Amazon ECR | Fully managed registry integrated with ECS and EKS |
Quiz: Serverless and Containers
Select one answer per question. You will receive immediate feedback.