You already have your images stored in ECR (subchapter 17.2). Now it's time to run them: get your containers running in production, scale them, and keep them healthy. For this, AWS offers ECS (Elastic Container Service), its service for orchestrating containers. In this subchapter, you'll understand its key concepts and an important decision: Fargate or EC2.

The problem: running containers for real

Running a container on your laptop is easy (docker run). But in production you need much more:

  • Run many containers spread across several servers.
  • Restart them automatically if they crash.
  • Scale them according to demand (more copies at peak times).
  • Distribute traffic among them (with a load balancer, Chapter 13).

Doing all this manually would be unfeasible. A container orchestrator automates it. ECS is AWS's orchestrator.

Analogy: an orchestrator is like the conductor of an orchestra. The musicians are the containers; the conductor makes sure they play in harmony, that the right one comes in, that if one fails another covers for it, and that the whole sounds good. You provide the score (the configuration) and the conductor (ECS) coordinates everything.

The key concepts of ECS

Task Definition

A task definition is the "recipe" for how to run your container: which image to use (from ECR), how much CPU and memory it needs, which ports it opens, which environment variables it has, etc. It's a blueprint that describes how your application should run.

Task Definition "mi-api":
  - Image: mi-api:v2.0 (from ECR)
  - CPU: 0.5 vCPU
  - Memory: 1 GB
  - Port: 8080
  - Variables: ENTORNO=produccion

Task

A task is a task definition in execution: a live instance of your container (or group of containers) running according to that recipe. It is to ECS what a container is to an image.

Service

A service is responsible for keeping the number of tasks you want running and connecting them to a load balancer. You tell it "I always want 3 copies of my API running," and the service makes sure that's the case: if one goes down, it starts another; if you want to scale, it adds more.

Service "mi-api" (desired: 3 tasks)
 ├── Task 1 ✓
 ├── Task 2 ✓
 └── Task 3 ✗ (goes down) ──► the service automatically starts a Task 4

Sound familiar? It's the same concept as the Auto Scaling Group from Chapter 13, but for containers instead of EC2 instances. The service maintains the desired number of tasks and repairs them. And, as with the ASG, it integrates with a load balancer to distribute traffic among the tasks.

The big decision: Fargate vs EC2

ECS needs machines to run the containers. Here you have two modes, and choosing well is important:

EC2 Mode: you provide the servers

In EC2 mode, you manage a group of EC2 instances (Chapter 4) where ECS places the containers. You have full control over those servers (instance type, configuration), but also the responsibility of managing them: patching, scaling, maintaining them.

EC2 Mode:
  You manage the EC2 instances ──► ECS places containers on them
  + Full control    - You maintain the servers

Fargate Mode: no servers to manage

In Fargate mode, AWS provides and manages the servers for you. You just say "run this task with this CPU and memory," and Fargate takes care of everything else. You don't see or manage any EC2 instances: it's serverless for containers.

Fargate Mode:
  You just define the task ──► AWS runs the container (no visible servers)
  + Zero server management    - Less low-level control

Connection with Chapter 14: Fargate is to containers what Lambda is to functions: in both, AWS hides and manages the servers. You forget about the infrastructure and just focus on your application.

Which should I choose?

EC2 Mode Fargate Mode
Who manages the servers You AWS
Control Full (instance type, etc.) Limited (just CPU/memory)
Operational effort High (maintain servers) Minimal
Cost Can be lower at large, constant scale Pay per task; simple
Ideal for Large, constant loads, fine control Simplicity, variable loads, getting started

Recommendation to start: use Fargate. It takes away the burden of managing servers and keeps things simple. EC2 mode makes sense when you grow a lot, have very constant loads (where it can be cheaper), or need special control over the servers. For most teams, Fargate is the default option nowadays.

How it all fits together

Bringing together the three container subchapters so far:

1. Docker: you build the image           (subchap. 17.1)
2. ECR: you store the image              (subchap. 17.2)
3. ECS:
   - Task Definition: execution recipe (uses the ECR image)
   - Service: keeps N tasks running, repairs and scales
   - Fargate: AWS runs everything with no servers for you to manage
   + Load balancer: distributes traffic among the tasks (Chap. 13)

What you should remember

  • ECS (Elastic Container Service) is AWS's container orchestrator: it runs, repairs, scales, and distributes traffic for your containers automatically (the "orchestra conductor").
  • Task Definition: the "recipe" for how to run your container (image, CPU, memory, ports). Task: a recipe in execution. Service: keeps the desired number of tasks running, repairs and scales them (like an Auto Scaling Group, but for containers).
  • Two execution modes: EC2 (you manage the servers, more control and effort) and Fargate (AWS manages the servers, serverless for containers, minimal effort).
  • Fargate is Lambda for containers: forget about servers. It's the recommended option to start; EC2 makes sense at large, constant scale or with special control needs.

In the last subchapter of the chapter (and of Part IV) we'll see the most powerful and complex alternative: EKS (Kubernetes on AWS), and when it's worth it compared to ECS.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved