Deploying to ECS Fargate
ECS Fargate runs your containers with no servers to manage β you just describe the task.
ECS (Elastic Container Service) orchestrates containers. With the Fargate launch type there are no EC2 servers to patch or scale β AWS runs your containers on demand.
The three pieces
- Task definition β the blueprint: image, CPU/memory, ports, env.
- Service β keeps N copies of the task running and registered with the load balancer.
- Cluster β a logical grouping for your services.
Example
{
"family": "myapp",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"containerDefinitions": [{
"name": "web",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:abc123",
"portMappings": [{ "containerPort": 3000, "protocol": "tcp" }],
"essential": true
}]
}When to use it
- A team deploys a REST API as an ECS Fargate service so it scales horizontally without managing EC2 instances or OS patches.
- A startup runs nightly batch jobs as Fargate tasks that spin up, process data, and terminate, paying only for actual compute time.
- An ops engineer updates a Fargate service to a new task definition version to deploy a new Docker image with zero downtime.
More examples
Register an ECS task definition
Registers a Fargate task definition specifying 0.25 vCPU, 512 MB memory, the container image, and CloudWatch logging.
aws ecs register-task-definition \
--family myapp \
--network-mode awsvpc \
--requires-compatibilities FARGATE \
--cpu 256 --memory 512 \
--execution-role-arn arn:aws:iam::123456789012:role/ecsTaskExecutionRole \
--container-definitions '[{
"name":"myapp",
"image":"123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:abc1234",
"portMappings":[{"containerPort":3000}],
"logConfiguration":{"logDriver":"awslogs",
"options":{"awslogs-group":"/ecs/myapp","awslogs-region":"us-east-1","awslogs-stream-prefix":"ecs"}}
}]'Create ECS Fargate service
Creates a service that runs two Fargate task replicas in private subnets, behind a security group.
aws ecs create-service \
--cluster prod \
--service-name myapp \
--task-definition myapp:1 \
--desired-count 2 \
--launch-type FARGATE \
--network-configuration \
'awsvpcConfiguration={subnets=[subnet-aaa,subnet-bbb],securityGroups=[sg-ccc],assignPublicIp=DISABLED}'Deploy new image by updating service
Updates the running service to use a new task definition revision, triggering a rolling replacement of tasks.
# Force a new deployment with the latest task definition
aws ecs update-service \
--cluster prod \
--service myapp \
--task-definition myapp:2 \
--force-new-deployment
Discussion