ECS Task Definitions and Services
ECS (Elastic Container Service) is AWS's managed container orchestrator. A Task Definition is a blueprint for running containers—specify container images, CPU/memory, port mappings, environment variables, volumes, and IAM roles.
A Service maintains desired task count and handles deployment. Service types include: REPLICA (maintains fixed count) and DAEMON (runs one task per node—good for log collectors). Configure deployment circuits for safe rollouts: set minimum healthy percent (e.g., 100% for zero-downtime) and maximum percent (e.g., 200%) to control rollout speed.
Task IAM roles give containers permissions to AWS services—attach policies for S3, DynamoDB, Secrets Manager access. Task execution roles let the agent pull images from ECR and send logs to CloudWatch. These are separate roles following least privilege.
Fargate vs EC2 Launch Types
Fargate is serverless—AWS manages the underlying infrastructure. You specify CPU/memory per task (in increments) and Fargate handles provisioning. No SSH access, no OS management, no capacity planning. Ideal for variable workloads, microservices, and teams wanting operational simplicity. Fargate charges per task per second.
EC2 launch type gives you control over the underlying instances. You manage the ECS cluster EC2 instances, handle patching, and configure capacity. Better for workloads requiring GPU, custom AMIs, or instance types not available in Fargate. Use capacity providers to mix both—Fargate for burst, EC2 for baseline.
Capacity Providers let you associate ASGs with ECS clusters, enabling Container Insight monitoring and automatic scaling. Use Fargate Capacity Providers for serverless tasks alongside EC2 Capacity Providers for persistent workloads.
EKS Managed Node Groups and Fargate Profiles
EKS (Elastic Kubernetes Service) runs managed Kubernetes control planes—AWS handles etcd, API servers, and controller managers. You focus on running your applications.
Managed Node Groups provision and manage EC2 instances as Kubernetes nodes. AWS handles node lifecycle, AMI updates, and scaling. Configure instance types, subnets, and labels. Use multiple node groups for different workload types (compute-optimized for batch, memory-optimized for databases).
Fargate Profiles run pods on Fargate—no nodes to manage. Define profiles matching pod labels (e.g., compute-type: fargate). Good for burst workloads and teams wanting to avoid node management. Mix EC2 and Fargate in the same cluster using profile selectors.
Pod Identity simplifies IAM for pods—assign AWS roles directly to service accounts without OIDC providers. This follows the principle of least privilege at the pod level. Each pod gets temporary credentials scoped to its role.
Service Discovery, Load Balancing, and Auto-scaling
Service Discovery lets containers find each other. ECS integrates with Cloud Map for DNS-based discovery—services register and others resolve via service-name.namespace. For EKS, Kubernetes CoreDNS provides service discovery with service-name.namespace.svc.cluster.local.
Load Balancing distributes traffic: use Application Load Balancer for HTTP/HTTPS, Network Load Balancer for TCP/UDP. ECS integrates directly—specify target group ARNs in service definition. For EKS, use AWS Load Balancer Controller to provision ALBs/NLBs via Kubernetes Ingress resources.
Auto-scaling has two layers: Service Auto-scaling adjusts task count based on metrics (CPU, memory, custom CloudWatch). Cluster Auto-scaling adds/removes EC2 nodes when pods can't be scheduled. For EKS, Cluster Autoscaler or Karpenter handles node scaling. Set scale-in/out policies with stabilization periods to prevent thrashing.
Quiz
1. When should you choose Fargate over EC2 for ECS?
2. What is the purpose of a task execution role in ECS?
3. How does EKS Pod Identity differ from OIDC provider for IAM?
Flashcards
Question
What is an ECS Task Definition?
Click to reveal answer
Answer
A blueprint defining container images, CPU/memory, ports, volumes, and IAM roles for running containers in ECS.
Question
What is the difference between Fargate and EC2 launch types?
Click to reveal answer
Answer
Fargate is serverless (AWS manages infra), EC2 gives you control over underlying instances. Fargate charges per task/second.
Question
What is a Daemon service in ECS?
Click to reveal answer
Answer
A service type that runs exactly one task on each active node in the cluster—ideal for log collectors and monitoring agents.
Question
What is Karpenter in EKS?
Click to reveal answer
Answer
A Kubernetes-native node autoscaler that provisions nodes based on pod scheduling requirements, faster and more flexible than Cluster Autoscaler.
Revision Notes
Key Takeaways
- 1. ECS: Task Definitions (blueprint) → Services (maintain count) → Tasks (running containers)
- 2. Fargate = serverless (no infra management), EC2 = full control (your instances)
- 3. EKS manages control plane; use Managed Node Groups or Fargate Profiles for nodes
- 4. Two scaling layers: service-level (task count) and cluster-level (node count)
Interview Tips
- • Know when to recommend ECS vs EKS: ECS for simpler workloads, EKS for multi-cloud/hybrid
- • Explain Fargate trade-offs: simplicity vs. cost at scale vs. customization
- • Describe the two IAM roles in ECS: task execution role vs task role
- • Understand service discovery options: Cloud Map (ECS) vs CoreDNS (EKS)
Cheat Sheet
ECS: container orchestration. Task Definition = blueprint. Service = maintains task count. Fargate (serverless) vs EC2 (managed nodes). EKS: Kubernetes on AWS. Managed Node Groups = AWS-managed nodes. Fargate Profiles = serverless pods. Pod Identity = simplified IAM. Two scaling layers: service (tasks) and cluster (nodes). Karpenter for fast EKS node scaling.