Skip to content

ECS

Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that helps you easily deploy, manage, and scale containerized applications . As a fully managed service, Amazon ECS comes with AWS configuration and operational best practices built-in, allowing teams to focus on building applications rather than managing infrastructure .

This comprehensive guide covers everything you need to know about Amazon ECS, from core concepts to hands-on implementation and advanced features.


Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that simplifies the deployment, management, and scaling of containerized applications . It’s integrated with both AWS tools, such as Amazon Elastic Container Registry (ECR), and third-party tools, such as Docker, making it easier for teams to focus on building applications, not the environment .

BenefitDescription
Fully ManagedNo control plane, nodes, or add-ons to manage—AWS handles the orchestration layer
Serverless OptionAWS Fargate eliminates server management entirely
Deep AWS IntegrationSeamless integration with IAM, CloudWatch, Load Balancers, and other AWS services
Cost EfficiencyPay only for resources you use; with Fargate, pay per vCPU and memory
Security by DesignGranular IAM permissions per container, integration with security services

Amazon ECS operates on three fundamental layers: Capacity, Controller, and Provisioning .

LayerDescription
CapacityThe infrastructure where your containers run (EC2 instances, Fargate, or on-premises)
ControllerThe ECS scheduler that manages your applications and containers
ProvisioningTools to interface with the scheduler (Console, CLI, SDKs, CDK, Copilot)

The following diagram illustrates how ECS components work together:

ECR (Image Registry) → Task Definition → Service/Task → Cluster → Running Containers

Key Components Explained:

ComponentDescription
ContainerA lightweight, portable unit that includes application code, runtime, libraries, and dependencies
ImageA read-only template used to create containers; stored in a registry like Amazon ECR
Task DefinitionA JSON blueprint describing one or more containers, including CPU, memory, networking, and IAM roles
TaskAn instantiation of a task definition—the running instance of your application
ServiceManages long-running tasks, ensuring a desired number are always running; supports load balancing and auto scaling
ClusterA logical grouping of tasks or services; the infrastructure where your application runs
Container InstanceAn EC2 instance registered to an ECS cluster (EC2 launch type only)
Container AgentRuns on each container instance, communicating with the ECS control plane

ECS provides two launch types, each with distinct trade-offs .

FeatureEC2 Launch TypeFargate Launch Type
ManagementYou manage EC2 instances (patching, scaling, security)AWS manages everything—fully serverless
ControlFull control over instances, custom AMIs, GPU accessNo infrastructure access
BillingPay for EC2 instances (per second/hour)Pay per vCPU and memory per task
Best ForLarge steady workloads, GPU needs, regulatory complianceBursty workloads, microservices, event-driven apps
ScalingCluster Auto Scaling for EC2 instancesAutomatic infrastructure scaling

With the EC2 launch type, you configure and deploy EC2 instances in your ECS cluster to run your containers .

Ideal workloads:

  • Workloads requiring consistently high CPU and memory usage
  • Applications needing persistent storage
  • GPU-accelerated workloads (ML inference, graphics)
  • When direct infrastructure management is required for compliance

Fargate is a serverless, pay-as-you-go compute engine. With Fargate, you don’t need to manage servers, handle capacity planning, or isolate container workloads for security .

Ideal workloads:

  • Small workloads with occasional demand bursts
  • Batch processing jobs
  • Serverless applications
  • Development and testing environments

Many organizations adopt a hybrid model, using EC2 for predictable base workloads and Fargate for bursty or unpredictable jobs .


4. Step-by-Step: Creating Your First ECS Cluster

Section titled “4. Step-by-Step: Creating Your First ECS Cluster”
  • An AWS account
  • AWS Management Console access
  • A container image stored in Amazon ECR (or Docker Hub)

Step 1: Create an Amazon ECR Repository (Optional)

Section titled “Step 1: Create an Amazon ECR Repository (Optional)”

If you don’t have a container image:

  1. Navigate to Amazon ECR in the AWS Console
  2. Click Create repository
  3. Name your repository (e.g., my-first-app)
  4. Click Create repository
  1. Navigate to Amazon ECS in the AWS Console

  2. Click Create cluster

  3. Choose a cluster template:

    • Networking only (Fargate) - Serverless option
    • EC2 Linux + Networking - For EC2 launch type
    • EC2 Windows + Networking - For Windows containers
  4. Configure cluster settings:

    • Cluster name: Enter a descriptive name (e.g., my-ecs-cluster)
    • For EC2 launch type: Select VPC, subnet, instance type, and key pair
    • For Fargate: Minimal configuration needed
  5. Click Create

  1. In the ECS console, go to Task Definitions

  2. Click Create new task definition

  3. Select launch type compatibility:

    • Fargate - For serverless
    • EC2 - For EC2 instances
  4. Configure task definition:

    • Task definition name: e.g., my-app-task
    • Task role (IAM role for the task)
    • Task execution role (for pulling images)
  5. Container definition:

    • Click Add container
    • Container name: e.g., web-app
    • Image URI: Your ECR image or Docker Hub image
    • Memory limits (Fargate: minimum 512 MiB)
    • CPU units (Fargate: minimum 256)
  6. Port mappings:

    • Container port: e.g., 80 (for web applications)
    • Protocol: TCP
  7. Click Create

  1. In ECS console, go to Clusters and select your cluster

  2. Click on the Services tab

  3. Click Create

  4. Configure service:

    • Launch type: Fargate or EC2
    • Task definition: Select the version created above
    • Service name: e.g., my-app-service
    • Desired tasks: 1 (number of copies to run)
  5. Configure networking:

    • VPC and subnets: Select your VPC
    • Security groups: Create or select a security group allowing traffic on your container port
  6. Load balancing (optional):

    • Select Application Load Balancer if you need traffic distribution
    • Create or select a load balancer and target group
  7. Click Create

Your service will start and the desired number of tasks will launch .


5. Task Definitions: The Blueprint for Your Application

Section titled “5. Task Definitions: The Blueprint for Your Application”

A task definition is a JSON file that describes the parameters and one or more containers that form your application .

ComponentDescription
FamilyThe name of the task definition (e.g., my-app:1)
Task RoleIAM role that the task uses to make API calls
Execution RoleIAM role for the ECS agent to pull images and write logs
Container DefinitionsOne or more containers (max 10 per task definition)
CPU and MemoryResource allocation for the task
Networking Modeawsvpc, bridge, host, or none
VolumesData volumes for persistent storage
Placement ConstraintsRules for task placement
{
"family": "web-app",
"taskRoleArn": "arn:aws:iam::account-id:role/ecsTaskRole",
"executionRoleArn": "arn:aws:iam::account-id:role/ecsExecutionRole",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "web-container",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:latest",
"cpu": 256,
"memory": 512,
"essential": true,
"portMappings": [
{
"containerPort": 80,
"protocol": "tcp"
}
],
"environment": [
{
"name": "ENVIRONMENT",
"value": "production"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/web-app",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
}
}
],
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512"
}

Task definitions support versioning. Each time you update a task definition, a new revision is created (e.g., my-app:1, my-app:2, my-app:3). Services can be updated to use newer revisions for rolling deployments .


TypeDescriptionUse Case
TaskA standalone instantiation of a task definitionBatch jobs, one-time processes
ServiceManages tasks to maintain desired countLong-running web applications, microservices
StrategyDescription
REPLICAPlaces and maintains a desired number of tasks across instances
DAEMONRuns one task on each container instance (EC2 only)

When running on EC2, you can control where tasks are placed using:

  • Placement strategies: binpack, random, spread
  • Placement constraints: distinctInstance, memberOf, custom attributes

Example placement strategy for binpacking (optimizing resource utilization):

  • Tasks are packed tightly on instances to minimize the number of EC2 instances needed

ModeDescription
awsvpcEach task gets its own ENI (Elastic Network Interface) with private IP; recommended for Fargate
bridgeTasks use Docker’s virtual bridge network (EC2 only)
hostTasks use the host’s network (EC2 only)
noneNo networking for the task

ECS integrates with Elastic Load Balancing to distribute traffic across your tasks .

Supported Load Balancers:

Load Balancer TypeProtocol SupportUse Case
Application Load Balancer (ALB)HTTP/HTTPSWeb applications, microservices, path-based routing
Network Load Balancer (NLB)TCP/UDP/TLSHigh-performance, low-latency workloads

Key features:

  • Dynamic port mapping: ECS registers tasks with load balancers using dynamic ports
  • Path-based routing: Multiple services can share one ALB using different paths
  • Health checks: Load balancer health checks ensure traffic only goes to healthy tasks

ECS integrates with AWS Cloud Map (formerly Service Discovery) so your containerized services can discover and connect with each other .

How it works:

  1. Each service registers with a namespace (e.g., production.myapp.local)
  2. Other services can discover them via DNS queries
  3. Automatically tracks changing IP addresses as tasks scale

Service Auto Scaling increases or decreases the desired number of tasks in your service automatically .

Scaling policies:

Policy TypeDescription
Target trackingScale based on a metric target (e.g., 70% CPU utilization)
Step scalingScale based on CloudWatch alarm thresholds
Scheduled scalingScale based on predictable patterns (e.g., scale up at 9 AM)

Cluster Auto Scaling (CAS) allows Amazon ECS to manage the scaling of Amazon EC2 Auto Scaling Groups (ASG) automatically .

How it works:

  1. You configure ECS to scale your ASG based on capacity needs
  2. When tasks require more resources, EC2 instances are added automatically
  3. When instances are underutilized, they are drained and removed
PracticeDescription
Multiple Availability ZonesSpread tasks across multiple AZs for fault tolerance
Load balancer health checksAutomatically replace unhealthy tasks
Deployment circuit breakerRollback automatically if deployment fails
Service auto scalingMaintain desired task count across AZs

LayerControls
Task-level IAM rolesEach task can have a unique IAM role
Security groupsControl traffic at the ENI level (awsvpc mode)
VPC isolationTasks run in your private VPC
Image scanningECR image scanning for vulnerabilities
RolePurpose
Task RolePermissions for the application running in the container (e.g., access S3, DynamoDB)
Execution RolePermissions for ECS agent (pull images from ECR, write CloudWatch logs)
Instance RolePermissions for EC2 instances (EC2 launch type only)
  • Least privilege: Grant only necessary permissions to each role
  • Use Secrets Manager: Store sensitive data (database passwords, API keys) instead of environment variables
  • Enable encryption: Use encrypted EFS volumes and ECR images
  • Regular patching: Keep base images updated (EC2 launch type)

ECS integrates with Amazon CloudWatch to provide monitoring capabilities for your containers and clusters .

Available metrics:

MetricDescription
CPUUtilizationCPU usage percentage
MemoryUtilizationMemory usage percentage
NetworkRxBytesReceived network bytes
NetworkTxBytesTransmitted network bytes

Viewing metrics:

  • Cluster-level: aggregate metrics across all tasks
  • Service-level: metrics per service
  • Task-level: metrics per individual task

The awslogs log driver sends container logs to CloudWatch Logs .

Configuration in task definition:

"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/my-app",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
}

CloudTrail records all ECS API calls, providing a history of API calls for security analysis, resource change tracking, and compliance auditing .

ToolPurpose
AWS X-RayDistributed tracing for microservices
Amazon Managed Service for PrometheusContainer monitoring at scale
Container InsightsDetailed performance metrics for ECS

ECS Anywhere provides support for registering external instances (on-premises servers or VMs) to your ECS cluster . This enables consistent container management across cloud and on-premises environments.

Blue/green deployments with AWS CodeDeploy minimize downtime during application updates :

  1. Launch new version alongside old version
  2. Test the new version
  3. Reroute traffic gradually
  4. Monitor deployment and rollback if needed

ECS supports management of Windows containers with an ECS-optimized Windows AMI .

Running containers on EC2 with GPUs: deploy EC2 instances with GPUs and use the GPU-optimized AMI to give containers access to attached GPUs for hardware acceleration .

For ML workloads, you can use AWS Inferentia hardware with Deep Learning Containers for purpose-built acceleration of model training and inference .

Run the same task on each selected instance in your cluster—ideal for logging, monitoring, or backup agents .

AWS Copilot is a command-line tool for developers to build, release, and operate production-ready containerized applications on ECS and Fargate . It codifies best practices and well-architected patterns.


Use CaseDescription
MicroservicesDeploy and scale independent services using ECS Services
Web ApplicationsLoad-balanced container deployments with ALB
Batch ProcessingRun scheduled or on-demand data-processing tasks
Machine Learning InferenceServe ML models as containerized endpoints
Cloud MigrationMigrate on-premises container workloads with minimal changes
Hybrid DeploymentsRun low-latency workloads on-premises with AWS Outposts
CategoryPractice
Task DesignKeep tasks single-purpose; use smallest necessary image
Resource AllocationSet appropriate CPU/memory limits; test to find optimal values
High AvailabilityDeploy across multiple AZs; use load balancers
SecurityUse task roles; store secrets in Secrets Manager; scan images
MonitoringEnable Container Insights; set CloudWatch alarms; centralize logs
Cost OptimizationUse Fargate Spot for fault-tolerant workloads; right-size resources
DeploymentUse rolling updates or blue/green; implement deployment circuit breakers

AspectAmazon ECSAmazon EKS (Kubernetes)
ComplexitySimpler setup, easier managementComplex setup, steeper learning curve
PortabilityAWS-native; less portableHighly portable (Kubernetes standard)
ControlLess control (fully managed orchestration)More control over configurations
EcosystemAWS integratedVast Kubernetes ecosystem (Helm, Istio, Prometheus)
Best ForTeams new to containers, AWS-centric workloadsTeams with Kubernetes expertise, multi-cloud strategies
  • You want the simplest, most integrated container experience on AWS
  • Your team doesn’t have Kubernetes expertise
  • You’re heavily invested in AWS services
  • You need quick time-to-value
  • You need Kubernetes portability across clouds
  • Your team already uses Kubernetes tools and patterns
  • You require specific Kubernetes features (custom resources, operators)
  • You’re building complex microservices with service mesh (Istio, Linkerd)

This glossary includes key terms directly related to Amazon ECS.


Amazon ECR (Elastic Container Registry) A fully managed Docker container registry that makes it easy to store, manage, and deploy container images. ECS tasks pull images from ECR to run .

awsvpc networking mode A networking mode where each ECS task receives its own Elastic Network Interface (ENI) with a private IP address. Recommended for Fargate and services requiring security group controls .


Capacity Provider Defines rules for how containerized workloads run on different types of compute capacity and manages scaling. Works with both EC2 and Fargate .

Cloud Map AWS service discovery solution that lets containerized services discover and connect with each other. ECS tasks register themselves, enabling DNS-based service discovery .

Cluster A logical grouping of tasks or services. The cluster represents the infrastructure (EC2 instances or Fargate) where your containerized applications run .

Cluster Auto Scaling (CAS) Feature that allows ECS to automatically manage the scaling of EC2 Auto Scaling Groups registered to your cluster .

Container A lightweight, portable, self-sufficient unit that includes application code, runtime, system tools, and libraries. Containers are created from images and run in isolation .

Container Agent A service that runs on each container instance (EC2 launch type) and communicates with the ECS control plane. Responsible for reporting resource utilization and task state .

Container Instance An EC2 instance that is registered to an ECS cluster and has the ECS container agent running. Used only with the EC2 launch type .

Copilot AWS command-line tool for building, releasing, and operating production-ready containerized applications on ECS and Fargate .


Daemon scheduling A service scheduling strategy that runs one task on each active container instance in a cluster. Ideal for logging, monitoring, or backup agents .


EC2 Launch Type Launch type where you configure and manage EC2 instances in your ECS cluster. Provides full control over infrastructure, custom AMIs, and GPU access .

ECS (Elastic Container Service) Fully managed container orchestration service that simplifies deployment, management, and scaling of containerized applications on AWS .

ECS Anywhere Feature enabling registration of external instances (on-premises servers or VMs) to your ECS cluster, providing consistent container management across cloud and on-premises .

Execution Role IAM role used by the ECS agent to pull container images from ECR and write logs to CloudWatch Logs .


Fargate Serverless compute engine for containers. With Fargate, you don’t manage servers, handle capacity planning, or isolate container workloads for security—AWS handles everything .

Fargate Launch Type Launch type where containers run without any infrastructure management. You pay per vCPU and memory per task. Best for bursty workloads and microservices .

Family The name prefix for a task definition (e.g., my-app). Multiple revisions share the same family name .


Image A read-only template containing the software configuration required to run a container. Images are stored in registries like Amazon ECR or Docker Hub .


Placement constraints Rules that control which container instances a task can be placed on (EC2 launch type). Example: distinctInstance ensures tasks run on different instances .

Placement strategies Algorithms for distributing tasks across container instances (EC2 launch type). Options: binpack (pack tightly), random, spread (distribute evenly) .


REPLICA scheduling A service scheduling strategy that places and maintains a desired number of tasks across instances. Used for long-running applications .


Service Manages long-running tasks, ensuring a specified number of tasks are constantly running. Supports load balancing, auto scaling, and rolling updates .

Service Auto Scaling Feature that automatically increases or decreases the desired number of tasks in your service based on CloudWatch metrics or schedules .

Service Discovery Enables containers to discover and connect with each other using DNS names. ECS integrates with AWS Cloud Map for this purpose .


Task An instantiation of a task definition running within a cluster. A task can be standalone or managed by a service .

Task Definition A JSON-formatted blueprint that describes one or more containers forming your application. Specifies image, CPU, memory, networking, volumes, and IAM roles .

Task Role IAM role that the application running inside the container uses to make AWS API calls (e.g., access S3, write to DynamoDB) .

Target tracking A scaling policy that maintains a target metric value (e.g., keep CPU utilization at 70%) .


Amazon ECS provides the easiest way to build, deploy, and manage containerized applications at any scale on AWS . With its flexible launch types (EC2 and Fargate), deep AWS integration, and simplified management, ECS enables teams to focus on building applications rather than managing infrastructure.

Key takeaways:

  • Start with Fargate for serverless simplicity; use EC2 launch type when you need control or specialized hardware
  • Task definitions are the blueprints—version them and treat them as code
  • Services maintain desired task counts and enable rolling updates
  • Use load balancers and service discovery for resilient microservices
  • Implement auto scaling at both service and cluster levels
  • Follow security best practices: task roles, Secrets Manager, image scanning
  • ECS vs. EKS: Choose ECS for simplicity and AWS integration; choose EKS for Kubernetes portability and ecosystem