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.

Prerequisites

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.

Note

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

Output:

{ "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.p-amazon-ecs-optimized ami-0351a163d5f20e068 Launch instance
us-east-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0be9e1908fe51a590 Launch instance
us-west-2 amzn-ami-2018.03.p-amazon-ecs-optimized ami-01a82c3fce2c3ba58 Launch instance
us-west-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0860832102c806acb Launch instance
eu-west-3 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0a6720df6a8239525 Launch instance
eu-west-2 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0bf096cb3ddbdffe0 Launch instance
eu-west-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0f85d8192f90863dc Launch instance
eu-central-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-02c40c6d994943b85 Launch instance
eu-north-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0719a9312994ac2cb Launch instance
ap-northeast-2 amzn-ami-2018.03.p-amazon-ecs-optimized ami-07ef5505ecca20315 Launch instance
ap-northeast-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-047281402e27cf0c5 Launch instance
ap-southeast-2 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0f0ba4af21d1e1766 Launch instance
ap-southeast-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-04e47a1e7ce1d448a Launch instance
ca-central-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0e73429acd3865bd2 Launch instance
ap-south-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0cf953afde70921a2 Launch instance
sa-east-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0656fd0f926c55fc6 Launch instance
us-gov-east-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-0c6b0b15e70eb6f3e Launch instance
us-gov-west-1 amzn-ami-2018.03.p-amazon-ecs-optimized ami-22d3a443 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

Output:

{ "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

Output:

{ "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,

{ "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

Output:

{ "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

Output:

{ "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

Output:

{ "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

Output:

{ "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": [] } ] } ] }