Amazon Elastic Container Service
Developer Guide (API Version 2014-11-13)

Tutorial: Creating a Cluster with an EC2 Task Using the AWS CLI

The following steps help you set up a cluster, register a task definition, run a task, and perform other common scenarios in Amazon ECS with the AWS CLI. Ensure that you are using the latest version of the AWS CLI. For more information on how to upgrade to the latest version, see Installing the AWS Command Line Interface.


This tutorial assumes that the following prerequisites have been completed:

Step 1: (Optional) Create a Cluster

By default, your account receives a default cluster when you launch your first container instance.


The benefit of using the default cluster that is provided for you is that you don't have to specify the --cluster cluster_name option in the subsequent commands. If you do create your own, non-default, cluster, you must specify --cluster cluster_name for each command that you intend to use with that cluster.

Create your own cluster with a unique name with the following command:

aws ecs create-cluster --cluster-name MyCluster


{ "cluster": { "clusterName": "MyCluster", "status": "ACTIVE", "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/MyCluster" } }

Step 2: Launch an Instance with the Amazon ECS AMI

You must have an Amazon ECS container instance in your cluster before you can run tasks on it. If you do not have any container instances in your cluster, see Launching an Amazon ECS Container Instance for more information.

The following table lists the current Amazon ECS-optimized Amazon Linux AMI IDs by Region.

Region AMI Name AMI ID EC2 Console Link
us-east-2 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0eba5aab4550a443a Launch instance
us-east-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0d09143c6fc181fe3 Launch instance
us-west-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-00303cd65a37d033b Launch instance
us-west-2 amzn-ami-2018.03.u-amazon-ecs-optimized ami-084799b9fb64c149e Launch instance
ap-east-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-053d6ef319599211f Launch instance
ap-northeast-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0e1aa8c2e9d719f58 Launch instance
ap-northeast-2 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0765a9b4036f26f32 Launch instance
ap-south-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-09155a5dc3c532de1 Launch instance
ap-southeast-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0309369aa9694281c Launch instance
ap-southeast-2 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0c2963a1e04bd8a00 Launch instance
ca-central-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-00d5c6d1f8349edd1 Launch instance
eu-central-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0c0c01a7a42f41c0c Launch instance
eu-north-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-01d37f0c18610fdc6 Launch instance
eu-west-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-03d739ab8755ab020 Launch instance
eu-west-2 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0959f069afa43696a Launch instance
eu-west-3 amzn-ami-2018.03.u-amazon-ecs-optimized ami-025da470c90f48af8 Launch instance
sa-east-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-01c4f4ee99cf38e79 Launch instance
us-gov-east-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-0f85cbfbcffb10b08 Launch instance
us-gov-west-1 amzn-ami-2018.03.u-amazon-ecs-optimized ami-91b1caf0 Launch instance

Step 3: List Container Instances

Within a few minutes of launching your container instance, the Amazon ECS agent registers the instance with your default cluster. You can list the container instances in a cluster by running the following command:

aws ecs list-container-instances --cluster default


{ "containerInstanceArns": [ "arn:aws:ecs:us-east-1:aws_account_id:container-instance/container_instance_ID" ] }

Step 4: Describe your Container Instance

After you have the ARN or ID of a container instance, you can use the describe-container-instances command to get valuable information on the instance, such as remaining and registered CPU and memory resources.

aws ecs describe-container-instances --cluster default --container-instances container_instance_ID


{ "failures": [], "containerInstances": [ { "status": "ACTIVE", "registeredResources": [ { "integerValue": 1024, "longValue": 0, "type": "INTEGER", "name": "CPU", "doubleValue": 0.0 }, { "integerValue": 995, "longValue": 0, "type": "INTEGER", "name": "MEMORY", "doubleValue": 0.0 }, { "name": "PORTS", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [ "22", "2376", "2375", "51678" ], "type": "STRINGSET", "integerValue": 0 }, { "name": "PORTS_UDP", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [], "type": "STRINGSET", "integerValue": 0 } ], "ec2InstanceId": "instance_id", "agentConnected": true, "containerInstanceArn": "arn:aws:ecs:us-west-2:aws_account_id:container-instance/container_instance_ID", "pendingTasksCount": 0, "remainingResources": [ { "integerValue": 1024, "longValue": 0, "type": "INTEGER", "name": "CPU", "doubleValue": 0.0 }, { "integerValue": 995, "longValue": 0, "type": "INTEGER", "name": "MEMORY", "doubleValue": 0.0 }, { "name": "PORTS", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [ "22", "2376", "2375", "51678" ], "type": "STRINGSET", "integerValue": 0 }, { "name": "PORTS_UDP", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [], "type": "STRINGSET", "integerValue": 0 } ], "runningTasksCount": 0, "attributes": [ { "name": "com.amazonaws.ecs.capability.privileged-container" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" }, { "name": "com.amazonaws.ecs.capability.logging-driver.json-file" }, { "name": "com.amazonaws.ecs.capability.logging-driver.syslog" } ], "versionInfo": { "agentVersion": "1.5.0", "agentHash": "b197edd", "dockerVersion": "DockerVersion: 1.7.1" } } ] }

You can also find the Amazon EC2 instance ID that you can use to monitor the instance in the Amazon EC2 console or with the aws ec2 describe-instances --instance-id instance_id command.

Step 5: Register a Task Definition

Before you can run a task on your ECS cluster, you must register a task definition. Task definitions are lists of containers grouped together. The following example is a simple task definition that uses a busybox image from Docker Hub and simply sleeps for 360 seconds. For more information about the available task definition parameters, see Amazon ECS Task Definitions.

{ "containerDefinitions": [ { "name": "sleep", "image": "busybox", "cpu": 10, "command": [ "sleep", "360" ], "memory": 10, "essential": true } ], "family": "sleep360" }

The above example JSON can be passed to the AWS CLI in two ways: You can save the task definition JSON as a file and pass it with the --cli-input-json file://path_to_file.json option. Or, you can escape the quotation marks in the JSON and pass the JSON container definitions on the command line as in the below example. If you choose to pass the container definitions on the command line, your command additionally requires a --family parameter that is used to keep multiple versions of your task definition associated with each other.

To use a JSON file for container definitions:

aws ecs register-task-definition --cli-input-json file://$HOME/tasks/sleep360.json

To use a JSON string for container definitions:

aws ecs register-task-definition --family sleep360 --container-definitions "[{\"name\":\"sleep\",\"image\":\"busybox\",\"cpu\":10,\"command\":[\"sleep\",\"360\"],\"memory\":10,\"essential\":true}]"

The register-task-definition returns a description of the task definition after it completes its registration.

{ "taskDefinition": { "volumes": [], "taskDefinitionArn": "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep360:1", "containerDefinitions": [ { "environment": [], "name": "sleep", "mountPoints": [], "image": "busybox", "cpu": 10, "portMappings": [], "command": [ "sleep", "360" ], "memory": 10, "essential": true, "volumesFrom": [] } ], "family": "sleep360", "revision": 1 } }

Step 6: List Task Definitions

You can list the task definitions for your account at any time with the list-task-definitions command. The output of this command shows the family and revision values that you can use together when calling run-task or start-task.

aws ecs list-task-definitions


{ "taskDefinitionArns": [ "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep300:1", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep300:2", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep360:1", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:3", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:4", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:5", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:6" ] }

Step 7: Run a Task

After you have registered a task for your account and have launched a container instance that is registered to your cluster, you can run the registered task in your cluster. For this example, you place a single instance of the sleep360:1 task definition in your default cluster.

aws ecs run-task --cluster default --task-definition sleep360:1 --count 1


{ "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "overrides": { "containerOverrides": [ { "name": "sleep" } ] }, "lastStatus": "PENDING", "containerInstanceArn": "arn:aws:ecs:us-east-1:aws_account_id:container-instance/container_instance_ID", "clusterArn": "arn:aws:ecs:us-east-1:aws_account_id:cluster/default", "desiredStatus": "RUNNING", "taskDefinitionArn": "arn:aws:ecs:us-east-1:aws_account_id:task-definition/sleep360:1", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:aws_account_id:container/container_ID", "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "lastStatus": "PENDING", "name": "sleep" } ] } ] }

Step 8: List Tasks

List the tasks for your cluster. You should see the task that you ran in the previous section. You can take the task ID or the full ARN that is returned from this command and use it to describe the task later.

aws ecs list-tasks --cluster default


{ "taskArns": [ "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID" ] }

Step 9: Describe the Running Task

Describe the task using the task ID retrieved earlier to get more information about the task.

aws ecs describe-tasks --cluster default --task task_ID


{ "failures": [], "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "overrides": { "containerOverrides": [ { "name": "sleep" } ] }, "lastStatus": "RUNNING", "containerInstanceArn": "arn:aws:ecs:us-east-1:aws_account_id:container-instance/container_instance_ID", "clusterArn": "arn:aws:ecs:us-east-1:aws_account_id:cluster/default", "desiredStatus": "RUNNING", "taskDefinitionArn": "arn:aws:ecs:us-east-1:aws_account_id:task-definition/sleep360:1", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:aws_account_id:container/container_ID", "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "lastStatus": "RUNNING", "name": "sleep", "networkBindings": [] } ] } ] }