|
| 1 | +# Amazon ECS CLI |
| 2 | + |
| 3 | +The ECS Command Line Interface (ECS CLI) is a command line interface for Amazon |
| 4 | +EC2 Container Service (ECS) that provides high level commands to simplify |
| 5 | +creating, updating, and monitoring clusters and tasks from a local development |
| 6 | +environment. The Amazon ECS CLI supports |
| 7 | +[Docker Compose](https://docs.docker.com/compose/), a popular open-source tool |
| 8 | +for defining and running multi-container applications. Use the ECS CLI as part |
| 9 | +of your everyday development and testing cycle as an alternative to the AWS |
| 10 | +Management Console. |
| 11 | + |
| 12 | +For more information about Amazon ECS, see the |
| 13 | +[Amazon ECS Developer Guide](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html). |
| 14 | +For information about installing and using the ECS CLI, see the |
| 15 | +[ECS Command Line Interface](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_CLI.html). |
| 16 | + |
| 17 | +The AWS Command Line Interface (AWS CLI) is a unified client for AWS services |
| 18 | +that provides commands for all public API operations. These commands are lower |
| 19 | +level than those provided by the ECS CLI. For more information about supported |
| 20 | +services and to download the AWS Command Line Interface, see the |
| 21 | +[AWS Command Line Interface](http://aws.amazon.com/cli/) product detail page. |
| 22 | + |
| 23 | +## Installing |
| 24 | + |
| 25 | +Download the binary archive for your platform, decompress the archive, and |
| 26 | +install the binary on your `$PATH`. You can use the provided `md5` hash to |
| 27 | +verify the integrity of your download. |
| 28 | + |
| 29 | +* Linux: |
| 30 | + * [https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest](https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest) |
| 31 | + * [https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest.md5](https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest.md5) |
| 32 | +* Macintosh: |
| 33 | + * [https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest](https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest) |
| 34 | + * [https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest.md5](https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest.md5) |
| 35 | +* Windows: |
| 36 | + * (Not yet implemented) |
| 37 | + |
| 38 | +## Configuring the CLI |
| 39 | + |
| 40 | +Before using the CLI, you need to configure your AWS credentials, the AWS |
| 41 | +region in which to create your cluster, and the name of the ECS cluster to use |
| 42 | +with the `ecs-cli configure` command. These settings are stored in |
| 43 | +`~/.ecs/config`. You can use any existing AWS named profiles in |
| 44 | +`~/.aws/credentials` for your credentials with the `--profile` option. |
| 45 | + |
| 46 | +``` |
| 47 | +$ ecs-cli help configure |
| 48 | +NAME: |
| 49 | + configure - Configures your AWS credentials, the AWS region to use, and the EC2 Container Service cluster name to use with the ECS CLI. |
| 50 | +
|
| 51 | +USAGE: |
| 52 | + command configure [command options] [arguments...] |
| 53 | +
|
| 54 | +OPTIONS: |
| 55 | + --region, -r Specify the AWS Region to use. [$AWS_REGION] |
| 56 | + --access-key Specify the AWS access key to use. [$AWS_ACCESS_KEY_ID] |
| 57 | + --secret-key Specify the AWS secret key to use. [$AWS_SECRET_ACCESS_KEY] |
| 58 | + --profile, -p Specify your AWS credentials with an existing named profile from ~/.aws/credentials. [$AWS_PROFILE] |
| 59 | + --cluster, -c Specify the ECS cluster name to use. If the cluster does not exist, it will be created. |
| 60 | +``` |
| 61 | + |
| 62 | +## Using the CLI |
| 63 | +After installing the ECS CLI and configuring your credentials, you are ready to |
| 64 | +create an ECS cluster using the ECS CLI. |
| 65 | + |
| 66 | +``` |
| 67 | +$ ecs-cli help up |
| 68 | +NAME: |
| 69 | + up - Create the ECS Cluster (if it does not already exist) and the AWS resources required to set up the cluster. |
| 70 | +
|
| 71 | +USAGE: |
| 72 | + command up [command options] [arguments...] |
| 73 | +
|
| 74 | +OPTIONS: |
| 75 | + --keypair Specify the name of an existing Amazon EC2 key pair to enable SSH access to the EC2 instances in your cluster. |
| 76 | + --capability-iam Acknowledge that this command may create IAM resources. |
| 77 | + --size [Optional] Specify the number of instances to register to the cluster. The default is 1. |
| 78 | + --azs [Optional] Specify a comma-separated list of 2 VPC availability zones in which to create subnets (these AZs must be in the 'available' status). This option is recommended if you do not specify a VPC ID with the --vpc option. WARNING: Leaving this option blank can result in failure to launch container instances if an unavailable AZ is chosen at random. |
| 79 | + --security-group [Optional] Specify an existing security group to associate it with container instances. Defaults to creating a new one. |
| 80 | + --cidr [Optional] Specify a CIDR/IP range for the security group to use for container instances in your cluster. Defaults to 0.0.0.0/0 if --security-group is not specified |
| 81 | + --port [Optional] Specify a port to open on a new security group that is created for your container instances if an existing security group is not specified with the --security-group option. Defaults to port 80. |
| 82 | + --subnets [Optional] Specify a comma-separated list of existing VPC Subnet IDs in which to launch your container instances. This option is required if you specify a VPC with the --vpc option. |
| 83 | + --vpc [Optional] Specify the ID of an existing VPC in which to launch your container instances. If you specify a VPC ID, you must specify a list of existing subnets in that VPC with the --subnets option. If you do not specify a VPC ID, a new VPC is created with two subnets. |
| 84 | + --instance-type [Optional] Specify the EC2 instance type for your container instances. |
| 85 | +``` |
| 86 | + |
| 87 | +For example, to create an ECS cluster with two Amazon EC2 instances: |
| 88 | + |
| 89 | +``` |
| 90 | +$ ecs-cli up --keypair my-key --capability-iam --size 2 |
| 91 | +``` |
| 92 | + |
| 93 | +It will take a few minutes to create the resources requested by `ecs-cli up`. |
| 94 | +To see when the cluster is ready to run tasks you can use the AWS CLI to |
| 95 | +confirm the ECS instances are registered: |
| 96 | + |
| 97 | + |
| 98 | +``` |
| 99 | +$ aws ecs list-container-instances --cluster your-cluster-name |
| 100 | +{ |
| 101 | + "containerInstanceArns": [ |
| 102 | + "arn:aws:ecs:us-east-1:980116778723:container-instance/6a302e06-0aa6-4bbc-9428-59b17089b887", |
| 103 | + "arn:aws:ecs:us-east-1:980116778723:container-instance/7db3c588-0ef4-49fa-be32-b1e1464f6eb5", |
| 104 | + ] |
| 105 | +} |
| 106 | +
|
| 107 | +``` |
| 108 | + |
| 109 | +**Note:** The default security group created by `ecs-cli up` allows inbound |
| 110 | +traffic on port 80 by default. To allow inbound traffic from a different port, |
| 111 | +specify the port you wish to open with the `--port` option. To add more ports |
| 112 | +to the default security group, go to EC2 Security Groups in the AWS Management |
| 113 | +Console and search for the security group with “ecs-cli”. Add a rule as |
| 114 | +described in |
| 115 | +[the documentation]( http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html#adding-security-group-rule). |
| 116 | +Alternatively, you may specify an existing security group ID with the |
| 117 | +`--security-group` option. |
| 118 | + |
| 119 | +Once the cluster is created you can run tasks – groups of containers – on the |
| 120 | +ECS cluster. First, author a |
| 121 | +[Docker Compose configuration file]( https://docs.docker.com/compose). |
| 122 | +You can run the configuration file locally using Docker Compose. Here is an |
| 123 | +example Docker Compose configuration file that creates a web page: |
| 124 | + |
| 125 | +``` |
| 126 | +web: |
| 127 | + image: amazon/amazon-ecs-sample |
| 128 | + ports: |
| 129 | + - "80:80" |
| 130 | +``` |
| 131 | + |
| 132 | +To run the configuration file on Amazon ECS use `ecs-cli compose up`. This |
| 133 | +creates an ECS task definition and starts an ECS task. You can see the task |
| 134 | +that is running with `ecs-cli compose ps`, for example: |
| 135 | + |
| 136 | +``` |
| 137 | +$ ecs-cli compose ps |
| 138 | +Name State Ports |
| 139 | +fd8d5a69-87c5-46a4-80b6-51918092e600/web RUNNING 54.209.244.64:80->80/tcp |
| 140 | +``` |
| 141 | + |
| 142 | +Navigate your web browser to the task’s IP address to see the sample app |
| 143 | +running in the ECS cluster. |
| 144 | + |
| 145 | +You can also run tasks as services. The ECS service scheduler ensures that the |
| 146 | +specified number of tasks are constantly running and reschedules tasks when a |
| 147 | +task fails (for example, if the underlying container instance fails for some |
| 148 | +reason). |
| 149 | + |
| 150 | +``` |
| 151 | +$ ecs-cli compose --project-name wordpress-test service create |
| 152 | +
|
| 153 | +INFO[0000] Using Task definition TaskDefinition=ecscompose-wordpress-test:1 |
| 154 | +INFO[0000] Created an ECS Service serviceName=ecscompose-service-wordpress-test taskDefinition=ecscompose-wordpress-test:1 |
| 155 | +
|
| 156 | +``` |
| 157 | + |
| 158 | +You can then start the tasks in your service with the following command: |
| 159 | +`$ ecs-cli compose --project-name wordpress-test service start` |
| 160 | + |
| 161 | +It may take a minute for the tasks to start. You can monitor the progress using |
| 162 | +this command: |
| 163 | +``` |
| 164 | +$ ecs-cli compose --project-name wordpress-test service ps |
| 165 | +Name State Ports |
| 166 | +34333aa6-e976-4096-991a-0ec4cd5af5bd/mysql RUNNING |
| 167 | +34333aa6-e976-4096-991a-0ec4cd5af5bd/wordpress RUNNING 54.186.138.217:80->80/tcp |
| 168 | +``` |
| 169 | + |
| 170 | + |
| 171 | +## ECS CLI Commands |
| 172 | + |
| 173 | +For a complete list of commands, see the |
| 174 | +[ECS CLI documentation](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_CLI.html). |
| 175 | + |
| 176 | + |
| 177 | +## Building the CLI |
| 178 | +### Developing |
| 179 | + |
| 180 | +Running ``scripts/vendor.sh`` creates/updates the vedor/ directory with |
| 181 | +dependencies. |
| 182 | + |
| 183 | +For developing code, the correct GOPATH can be printed by running |
| 184 | +`./scripts/shared_env` script. |
| 185 | + |
| 186 | +This can be set as GOPATH on the dev box. |
| 187 | + |
| 188 | +### Building |
| 189 | + |
| 190 | +Running `make build` creates a standalone executable in the `bin/local` |
| 191 | +directory |
| 192 | + |
| 193 | +```bash |
| 194 | +$ pwd |
| 195 | +/home/ubuntu/github/src/github.com/aws/amazon-ecs-cli |
| 196 | +$ make build |
| 197 | +$ ls bin/local |
| 198 | +ecs-cli |
| 199 | +``` |
| 200 | + |
| 201 | +### Cross-compiling |
| 202 | + |
| 203 | +The `make docker-build` target will build standalone amd64 executables for |
| 204 | +darwin and linux. The output will be in `bin/darwin-amd64` and `bin/linux-amd64` |
| 205 | +respectively. |
| 206 | + |
| 207 | +If you have set up the appropriate bootstrap environments, you may also directly |
| 208 | +run the `make supported-platforms` target to create standalone amd64 executables |
| 209 | +for darwin and linux platforms. |
| 210 | + |
| 211 | +## Testing |
| 212 | + |
| 213 | +### Running tests |
| 214 | + |
| 215 | +Running ``make test`` runs unit tests in the package. |
| 216 | + |
| 217 | +## License |
| 218 | + |
| 219 | +The ECS CLI is distributed under the |
| 220 | +[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0), |
| 221 | +see LICENSE and NOTICE for more information. |
0 commit comments