How to Run a Container on AWS Fargate with the CLI
November 26, 2026 · 2 min de lectura
To run a container on AWS Fargate, register a task definition that names your image, then start it with aws ecs run-task --launch-type FARGATE. There are no servers to create. You only give ECS an image, a size and a network.
This example runs the public python:3.12-slim image for one command, then stops. It assumes a cluster named demo-cluster, a subnet ID, a security group ID, and an execution role named ecsTaskExecutionRole (the role ECS uses to start the task and send logs).
# 1. A log group for the container's output
aws logs create-log-group --log-group-name /demo/hello
# 2. A task definition file: hello.json
cat > hello.json <<'EOF'
{
"family": "hello",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"containerDefinitions": [{
"name": "hello",
"image": "python:3.12-slim",
"command": ["python", "-c", "print('hello from Fargate')"],
"essential": true,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/demo/hello",
"awslogs-region": "us-east-2",
"awslogs-stream-prefix": "hello"
}
}
}]
}
EOF
# 3. Register it
aws ecs register-task-definition --cli-input-json file://hello.json \
--query 'taskDefinition.[family,revision]' --output text
# 4. Run it
aws ecs run-task --cluster demo-cluster --launch-type FARGATE \
--task-definition hello \
--network-configuration "awsvpcConfiguration={subnets=[subnet-0abc123],securityGroups=[sg-0abc123],assignPublicIp=ENABLED}" \
--query 'tasks[0].taskArn' --output text
Output of the last two commands:
hello 1
arn:aws:ecs:us-east-2:123456789012:task/demo-cluster/435bc5842bf345aa8bd9aa21eeaaa82d
What each part does
- Task definition: the recipe.
cpuandmemoryset the size (256 and 512 is the smallest common Fargate size). Eachregister-task-definitioncall creates a new numbered revision. networkMode: awsvpc: required on Fargate. Every task gets its own network interface.assignPublicIp=ENABLED: lets a task in a public subnet pull its image from the internet. Without it (and without a NAT gateway), the pull fails.run-task: starts one task and returns at once. When it stops, it stays stopped.
Check the result and read the log:
aws ecs wait tasks-stopped --cluster demo-cluster --tasks 435bc5842bf345aa8bd9aa21eeaaa82d
aws ecs describe-tasks --cluster demo-cluster --tasks 435bc5842bf345aa8bd9aa21eeaaa82d \
--query 'tasks[0].[lastStatus,containers[0].exitCode]' --output text
aws logs tail /demo/hello --format short
STOPPED 0
2026-11-18T04:12:40 hello from Fargate
Exit code 0 means the program finished cleanly.
Common mistakes
The task stops with CannotPullContainerError. The task could not reach the image. In a public subnet, set assignPublicIp=ENABLED. For a private image in ECR, the execution role also needs permission to pull it.
The task fails with a log-group error. The awslogs driver does not create the group for you by default. Create it first (step 1 above), and make the name in awslogs-group match exactly.
Takeaway
A Fargate task is a task definition plus aws ecs run-task with a subnet and a security group. For something that must stay running, put it behind an ECS service instead.