0
0
AWScloud~5 mins

Services and tasks in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to run and manage containers on AWS, you use services and tasks. A service keeps your containers running and replaces them if they stop. A task is a single running container or group of containers based on a task definition.
When you want to run a web app in containers and keep it always available.
When you need to update your app without downtime by replacing old containers with new ones.
When you want to run batch jobs or one-time container tasks on AWS.
When you want to control how many copies of your app run at the same time.
When you want AWS to automatically restart containers if they fail.
Config File - task-definition.json
task-definition.json
{
  "family": "my-app-task",
  "networkMode": "awsvpc",
  "containerDefinitions": [
    {
      "name": "my-app-container",
      "image": "nginx:1.23",
      "portMappings": [
        {
          "containerPort": 80,
          "protocol": "tcp"
        }
      ],
      "essential": true
    }
  ],
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "256",
  "memory": "512"
}

This JSON file defines a task for AWS ECS. The family names the task. networkMode sets networking to AWS VPC mode. containerDefinitions describes the container to run, here an nginx web server on port 80. requiresCompatibilities specifies it runs on Fargate, a serverless container service. cpu and memory set resource limits.

Commands
This command registers the task definition with AWS ECS so you can use it to run containers.
Terminal
aws ecs register-task-definition --cli-input-json file://task-definition.json
Expected OutputExpected
{ "taskDefinition": { "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/my-app-task:1", "family": "my-app-task", "revision": 1, "status": "ACTIVE", "requiresAttributes": [ { "name": "com.amazonaws.ecs.capability.ecr-auth" }, { "name": "ecs.capability.execution-role-awslogs" } ], "containerDefinitions": [ { "name": "my-app-container", "image": "nginx:1.23", "cpu": 0, "memory": null, "portMappings": [ { "containerPort": 80, "protocol": "tcp" } ], "essential": true } ], "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" } }
--cli-input-json - Specifies the JSON file with the task definition details
This command creates a service that runs one copy of the task on Fargate in the specified subnet with a public IP.
Terminal
aws ecs create-service --cluster default --service-name my-app-service --task-definition my-app-task --desired-count 1 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[subnet-0abc1234],assignPublicIp=ENABLED}"
Expected OutputExpected
{ "service": { "serviceName": "my-app-service", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/default", "status": "ACTIVE", "desiredCount": 1, "runningCount": 0, "pendingCount": 1, "launchType": "FARGATE", "taskDefinition": "my-app-task:1" } }
--desired-count - Sets how many task copies to run
--launch-type - Specifies to use Fargate serverless containers
--network-configuration - Defines networking details like subnet and public IP
This command lists the running tasks for the service to check if the container started.
Terminal
aws ecs list-tasks --cluster default --service-name my-app-service
Expected OutputExpected
{ "taskArns": [ "arn:aws:ecs:us-east-1:123456789012:task/default/abcdef1234567890" ] }
This command shows detailed information about the running task, including status and container details.
Terminal
aws ecs describe-tasks --cluster default --tasks arn:aws:ecs:us-east-1:123456789012:task/default/abcdef1234567890
Expected OutputExpected
{ "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/default/abcdef1234567890", "lastStatus": "RUNNING", "desiredStatus": "RUNNING", "containers": [ { "name": "my-app-container", "lastStatus": "RUNNING", "networkBindings": [ { "bindIP": "0.0.0.0", "containerPort": 80, "hostPort": 80 } ] } ] } ] }
Key Concept

A service keeps your containers running and replaces them if they stop, while a task is a single running container or group of containers based on a task definition.

Common Mistakes
Not specifying the correct subnet in the network configuration when creating the service.
The task will fail to start because it has no network to connect to.
Always specify a valid subnet ID in the --network-configuration flag.
Forgetting to register the task definition before creating the service.
The service cannot find the task definition and will not start any tasks.
Run aws ecs register-task-definition first with a valid JSON file.
Setting desired-count to zero when creating the service.
No tasks will run, so your app will not be available.
Set desired-count to at least 1 to run one or more task copies.
Summary
Register a task definition JSON file to tell AWS what containers to run and how.
Create a service to run and maintain the desired number of task copies on Fargate.
Use list-tasks and describe-tasks commands to check the status and details of running containers.