Compute and Serverless
ECS and EKS — Containers on AWS
Amazon ECS and EKS are managed orchestration platforms designed to deploy, scale, and manage containerized applications reliably across distributed infrastructure. They matter because they abstract the operational complexity of scheduling and networking, allowing developers to focus on application logic rather than cluster maintenance. You reach for these services when you need to run high-density workloads, maintain consistent environments across development and production, or leverage microservices architectures at scale.
The Foundation of Containerization
To understand orchestration, one must first recognize that a container is a standardized unit of software that packages code and all its dependencies, ensuring the application runs consistently regardless of the environment. Unlike traditional virtual machines, containers share the host operating system kernel, making them lightweight and fast to start. However, managing hundreds of containers manually is impossible. Orchestration engines address this by maintaining the desired state: if a container fails, the orchestrator detects the discrepancy against your defined configuration and automatically launches a replacement. The scheduler is the heart of this process, determining which specific host has enough memory and CPU capacity to support the container, effectively turning a collection of independent servers into a unified pool of compute resources available for your applications to consume dynamically.
# Define a simple task definition for a containerized web server
{
"family": "web-server",
"containerDefinitions": [{
"name": "nginx",
"image": "public.ecr.aws/nginx/nginx:latest",
"portMappings": [{"containerPort": 80, "hostPort": 80}]
}]
}Amazon ECS: The AWS-Native Choice
Amazon ECS (Elastic Container Service) is AWS's proprietary, highly integrated orchestration platform. Its primary architectural advantage is its deep integration with the rest of the AWS ecosystem, such as IAM for fine-grained security permissions, VPC for networking isolation, and Load Balancers for traffic distribution. ECS treats 'Tasks' as its core primitive; a task definition is a blueprint for your application. Because ECS was built specifically for AWS, the operational overhead is significantly lower compared to generic orchestration tools. You do not manage the control plane, as AWS manages the underlying cluster state for you. This allows teams to iterate faster by avoiding the 'configuration hell' that often plagues complex infrastructure setups, making it the default choice for teams that want to remain firmly within the AWS managed services philosophy.
# Registering a task definition using the AWS CLI
aws ecs register-task-definition --family my-app --container-definitions '[{"name": "app", "image": "nginx"}]'Amazon EKS: The Industry Standard
Amazon EKS (Elastic Kubernetes Service) provides a managed environment for running the industry-standard Kubernetes platform. Unlike ECS, EKS is designed for maximum portability and compatibility with the wider ecosystem of open-source tooling. Kubernetes is inherently more complex than ECS, utilizing concepts like Pods, Deployments, and Services to manage application lifecycles. EKS is the correct choice when your organization requires a multi-cloud strategy or needs specific advanced scheduling patterns not supported by simpler tools. The management overhead is higher because you must manage Kubernetes-specific manifests, configuration files, and potential plugin compatibility. However, the trade-off is the ability to leverage a massive community of extensions, allowing for highly sophisticated deployment strategies, complex networking topologies, and custom automation that would otherwise require significant development effort to replicate in a proprietary system.
# A Kubernetes deployment manifest to maintain 3 replicas
apiVersion: apps/v1
kind: Deployment
metadata: {name: web-app}
spec:
replicas: 3
selector: {matchLabels: {app: web}}
template:
metadata: {labels: {app: web}}
spec:
containers: [{name: nginx, image: 'nginx'}]Compute Options: Fargate vs. EC2
Once you have chosen your orchestrator, you must decide where the containers actually run. Fargate is the serverless compute engine for both ECS and EKS. By using Fargate, you relinquish control over the underlying instances; you simply define how much CPU and memory your task needs, and AWS provisions the underlying infrastructure. This removes the need for patching or scaling virtual machines manually. Conversely, the EC2 launch type gives you full control over the underlying cluster instances. You can select specific instance types, manage persistent storage, and optimize for cost by using reserved instances or spot capacity. You choose EC2 mode when your application requires specialized hardware, massive scale optimizations, or deep integration with host-level networking and security configurations that the serverless Fargate abstraction currently cannot provide.
# Fargate launch type configuration snippet for ECS
{ "requiresCompatibilities": ["FARGATE"], "cpu": "256", "memory": "512" }Service Discovery and Networking
In a distributed system, containers frequently die and are recreated on different physical hosts with different IP addresses. Service discovery solves the problem of how one component finds another. AWS provides integrated tools like Cloud Map and VPC Lattice to keep track of dynamic service locations. When a container starts, it registers its current IP and port; when other services need to communicate, they query these registries rather than hardcoded IPs. This is crucial for microservices, as it decouples internal addressing from service definitions. Furthermore, Load Balancers play a vital role by acting as a stable entry point for external traffic, automatically detecting unhealthy containers via health checks and shifting traffic to healthy ones. Mastering these networking abstractions ensures that your distributed application remains resilient to individual container failures and infrastructure churn.
# ECS service definition connecting to a target group
aws ecs create-service --service-name app-svc --task-definition web-app --load-balancers 'targetGroupArn=arn:aws:tg...,containerName=nginx,containerPort=80'Key points
- Containers offer a consistent packaging format that simplifies deployment across diverse environments.
- ECS provides deep AWS integration and is generally easier to operate for AWS-exclusive workloads.
- EKS is the industry-standard choice for teams needing portability and complex, feature-rich orchestration.
- Fargate is a serverless compute option that eliminates the need to manage individual virtual machine instances.
- The EC2 launch type provides granular control over underlying hardware for performance and cost optimization.
- Desired state management allows orchestrators to automatically recover failed tasks to maintain application availability.
- Service discovery and load balancing are essential for maintaining connectivity between dynamic, short-lived containers.
- Selecting between ECS and EKS should depend on your team's operational maturity and portability requirements.
Common mistakes
- Mistake: Choosing ECS EC2 launch type when Fargate is appropriate. Why it's wrong: It increases operational overhead by requiring manual management of underlying EC2 instances. Fix: Use Fargate for serverless container orchestration unless specific hardware customization or legacy requirements exist.
- Mistake: Misconfiguring EKS IAM roles for Service Accounts (IRSA). Why it's wrong: It grants excessive permissions to the entire EKS node rather than the specific pod. Fix: Implement IRSA to associate IAM roles directly with Kubernetes service accounts for principle of least privilege.
- Mistake: Incorrectly configuring the Load Balancer for ECS services. Why it's wrong: Choosing an Application Load Balancer (ALB) for non-HTTP traffic. Fix: Use a Network Load Balancer (NLB) for TCP/UDP traffic and an ALB for HTTP/HTTPS container traffic.
- Mistake: Failing to define appropriate health checks in Task Definitions. Why it's wrong: ECS may continue routing traffic to unhealthy containers, leading to application downtime. Fix: Define explicit health checks and utilize the 'essential' parameter to ensure task restarts upon failure.
- Mistake: Using a single EKS Node Group for all workloads. Why it's wrong: It prevents proper resource isolation and auto-scaling performance. Fix: Use multiple Node Groups with different instance types to match specific compute needs and utilize Cluster Autoscaler effectively.
Interview questions
What is the fundamental difference between Amazon ECS and Amazon EKS?
Amazon ECS is an AWS-native container orchestration service designed for simplicity, deep integration with other AWS services, and high performance. It uses a proprietary API and scheduler, making it very easy to set up and manage within the AWS ecosystem. Conversely, Amazon EKS is a managed service that runs upstream Kubernetes. While EKS provides greater portability and access to the massive Kubernetes ecosystem of tools and plugins, it is significantly more complex to configure and maintain compared to the streamlined, opinionated approach of ECS.
Can you explain the role of AWS Fargate in container orchestration?
AWS Fargate is a serverless compute engine for both Amazon ECS and EKS that eliminates the need to provision, configure, or scale clusters of virtual machines. Instead of managing underlying EC2 instances, you simply define your container resource requirements—such as CPU and memory—and Fargate handles the infrastructure provisioning. This allows developers to focus entirely on application development rather than patching or scaling servers. It is ideal for event-driven workloads or environments where minimizing operational overhead is the primary business requirement.
When should you choose the EC2 launch type over Fargate?
You should choose the EC2 launch type when you require granular control over the underlying infrastructure, such as specific CPU architectures, GPU support for machine learning, or custom networking configurations. EC2 launch types are also more cost-effective for high-utilization, predictable workloads where you can leverage Savings Plans or Reserved Instances. Furthermore, if you need to perform low-level debugging on the host or have strict compliance requirements that dictate specific host management procedures, the EC2 launch type provides the visibility and configuration flexibility that the abstracted Fargate environment cannot offer.
How does service discovery function within an Amazon ECS environment?
Amazon ECS uses AWS Cloud Map for service discovery, which allows containers to discover each other automatically by name. When a service is deployed, ECS creates a service record in Route 53. Other services in the VPC can then resolve these names to internal IP addresses. This eliminates the need for hardcoded load balancer endpoints or complex static configuration files. By utilizing private DNS namespaces, you create a dynamic, scalable architecture where services can scale up or down independently while maintaining reliable, low-latency connectivity via standard DNS queries.
Describe the architecture and benefit of using an EKS Add-on.
An EKS Add-on is a software component that provides operational capabilities for your Kubernetes cluster, such as networking, storage, or observability. For example, the VPC CNI add-on is critical because it allows Kubernetes pods to receive an IP address directly from the VPC subnet. By using managed add-ons, AWS ensures that these critical components are verified, up-to-date, and compatible with the specific EKS version you are running. This reduces the 'undifferentiated heavy lifting' of maintaining complex cluster tooling, allowing teams to focus on deploying applications rather than debugging infrastructure-level integration software.
Compare the IAM roles used for ECS tasks versus EKS pods.
In Amazon ECS, you use 'Task IAM Roles' where an IAM role is assigned directly to the task definition, granting the container permissions to access AWS services like S3 or DynamoDB. In EKS, you use 'IAM Roles for Service Accounts' (IRSA). IRSA works by associating an IAM role with a Kubernetes service account using an OIDC provider. When a pod is scheduled, the EKS cluster injects the necessary security token into the pod's environment. While both serve the same goal of least-privilege access, IRSA is more complex to set up because it requires OIDC configuration but is ultimately more flexible for granular, pod-level permissions in multi-tenant clusters.
Check yourself
1. An application requires direct access to the underlying EC2 host's network namespace for custom packet processing. Which ECS launch type should be selected?
- A.Fargate
- B.EC2
- C.EKS Fargate
- D.App Runner
Show answer
B. EC2
EC2 launch type allows control over the underlying instance, supporting host networking. Fargate isolates containers in a managed environment that prevents host network access. EKS Fargate and App Runner also hide the host networking layer.
2. A developer needs to ensure that pods in an EKS cluster have granular access to S3 buckets without assigning an IAM role to the underlying worker nodes. What is the recommended approach?
- A.Hardcode credentials in the container environment variables
- B.Attach a profile to the EC2 worker nodes
- C.Use IAM Roles for Service Accounts (IRSA)
- D.Use the AWS Secret Manager Kubernetes CSI driver
Show answer
C. Use IAM Roles for Service Accounts (IRSA)
IRSA allows specific Kubernetes service accounts to assume IAM roles, providing fine-grained access. Hardcoding credentials is a security risk; attaching profiles to nodes violates least privilege; CSI drivers are for secrets, not IAM permission mapping.
3. In ECS, what occurs when a container marked as 'essential' fails its health check?
- A.The entire task is stopped and restarted
- B.Only the container is restarted while the task remains running
- C.The ECS service stops routing traffic to the cluster
- D.The task is migrated to a different Availability Zone
Show answer
A. The entire task is stopped and restarted
If any container marked as 'essential' in an ECS task fails, the entire task is stopped. Containers aren't restarted in isolation within the same task. The task is not automatically migrated across AZs, but the service scheduler will attempt to launch a new task.
4. Why would an administrator choose EKS over ECS for a containerized application?
- A.EKS is faster and cheaper than ECS
- B.EKS provides better integration with native Kubernetes tools and ecosystem
- C.ECS does not support Docker images
- D.EKS requires no management of nodes
Show answer
B. EKS provides better integration with native Kubernetes tools and ecosystem
EKS is chosen for its compatibility with the standard Kubernetes API and ecosystem. It is not necessarily cheaper or faster. ECS supports Docker images, and EKS (excluding Fargate) still requires managing node groups.
5. Which component in EKS is responsible for automatically adjusting the number of nodes in a managed node group based on pod resource requirements?
- A.Horizontal Pod Autoscaler
- B.AWS Load Balancer Controller
- C.Cluster Autoscaler
- D.CoreDNS
Show answer
C. Cluster Autoscaler
The Cluster Autoscaler monitors for unschedulable pods and adds nodes as needed. The Horizontal Pod Autoscaler adjusts the number of pods, not nodes. The Load Balancer Controller manages ingress, and CoreDNS is for service discovery.