Hands-On: Run It on Fargate

Register a task definition and run your container, with no servers involved.

1 min read

A task definition describes how to run the image. With Fargate, networkMode is always awsvpc, which gives each task its own network interface and private IP.

Try it

taskdef.json
{
  "family": "hello-app",
  "requiresCompatibilities": ["FARGATE"],
  "networkMode": "awsvpc",
  "cpu": "256",
  "memory": "512",
  "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
  "containerDefinitions": [
    {
      "name": "web",
      "image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-app:v1",
      "portMappings": [{ "containerPort": 8080 }],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/hello-app",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "web",
          "awslogs-create-group": "true"
        }
      }
    }
  ]
}

Register it and run one task (use a public subnet and a security group that allows port 8080):

Terminal
aws ecs create-cluster --cluster-name demo
aws ecs register-task-definition --cli-input-json file://taskdef.json

aws ecs run-task --cluster demo --launch-type FARGATE           --task-definition hello-app           --network-configuration "awsvpcConfiguration={subnets=[subnet-0abc],securityGroups=[sg-0abc],assignPublicIp=ENABLED}"

For an always-on app you'd create a service instead (aws ecs create-service … --desired-count 2), usually attached to an Application Load Balancer.

Note

cpu: "256" means 0.25 vCPU. Fargate supports fixed CPU/memory combinations, from tiny tasks up to many vCPUs and tens of GB of memory, and you pay per second for what you request.

Tip

Writing this JSON by hand gets old fast. Real projects usually define ECS services with CDK or CloudFormation (see the CloudFormation & CDK tutorial).

Key takeaways

  • Fargate task definitions use awsvpc networking and fixed CPU/memory sizes.
  • run-task starts a one-off task, while create-service keeps tasks running.
  • Send container logs to CloudWatch with the awslogs driver.

Check your understanding

Pick an answer to see the explanation.

  1. 1. In a Fargate task definition, cpu: "512" means…