ECS CLIでdocker-composeする

ECS CLI を使ってdocker-composeの設定ファイルでコンテナを稼働させます。

# インストール
ECS CLIをインストールします。

- Mac

```bash
$ sudo curl -o /usr/local/bin/ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest
$ sudo chmod +x /usr/local/bin/ecs-cli
```

- Linux

```bash
$ sudo curl -o /usr/local/bin/ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest
$ sudo chmod +x /usr/local/bin/ecs-cli
```

- 確認

```bash
$ ecs-cli help
NAME:
   ecs-cli - Command line interface for Amazon ECS

USAGE:
   ecs-cli [global options] command [command options] [arguments...]

VERSION:
   0.2.1 (e7b6965)

AUTHOR(S):
   Amazon Web Services
```

http://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/ECS_CLI_installation.html

# セットアップ

ECSの設定ファイルを生成します。

- ecs-cliのみの設定

```bash
$ ecs-cli configure --region ap-northeast-1 --access-key $AWS_ACCESS_KEY_ID --secret-key $AWS_SECRET_ACCESS_KEY --cluster CLUSTERNAME
```
生成される設定ファイル

```bash:~/.ecs/config
[ecs]
cluster = CLUSTERNAME
aws_profile =
region = ap-northeast-1
aws_access_key_id = XXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```

- awscliも設定 or awscli設定を引き継ぐ場合

```bash
$ aws configure                                                                                           
AWS Access Key ID [None]:XXXXXXXXXXXXXXXXXXXXXX
AWS Secret Access Key [None]:XXXXXXXXXXXXXXXXXXXXXXXXXX
Default region name [None]:ap-northeast-1
Default output format [None]:json

$ ecs-cli configure -p default --cluster CLUSTERNAME
```

生成される設定ファイル

```bash:~/.aws/credentials
[default]
aws_access_key_id = XXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```

```bash:~/.aws/config
[default]
output = json
region = ap-northeast-1
```

```bash:~/.ecs/config
[ecs]
cluster = CLUSTERNAME
aws_profile = default
region =
aws_access_key_id =
aws_secret_access_key =
```

http://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/cmd-ecs-cli-configure.html

# クラスタの起動

ECS上にクラスタを稼働させます。

### 最少オプションで起動

```bash
$ ecs-cli up --keypair keypair_name --capability-iam
```

Cloudformationスタックが作成され、下記のオブジェクトが生成されます。

- VPC
    - **VPC**
        - CIDR:  10.0.0.0/16
    - **Subnet**
        - サブネット1
            - AZ: ap-northeast-1a
            - CIDR: 10.0.0.0/24
        - サブネット2
            - AZ: ap-northeast-1c
            - CIDR: 10.0.1.0/24
   - **InternetCateway**
   - **RouteTable**
          - 0.0.0.0/0 -> InternetGateway
   - **Security Group**
        - Group name: amazon-ecs-cli-setup-*CLUSTERNAME*-EcsSecurityGroup-XXXXXXX
        - Policy: Permit 0.0.0.0/0 HTTP

- IAM
    - **Role**
        - Name: amazon-ecs-cli-setup-test-EcsInstancePolicy-XXXXXXXXXX
        - Policy:AmazonEC2ContainerServiceforEC2Role
        
- Auto Scaling
    - **Launch Configurations**
        - Name: amazon-ecs-cli-setup-test-EcsInstanceLc-1P0GAVG1RA5AN
        - AMI ID: ami-18d8de76
        - Instance Type: t2.micro
    - **Auto Scaling Groups**
        - Name: amazon-ecs-cli-setup-test-EcsInstanceAsg-1TKKSVWVJP7CO
        - Load Balancers: なし
        - Desired: 1
        - Min: 1
        - Max: 1
        - Default Cooldown: 300
        - Health Check Type: EC2

### 既存VPC内にセキュリティグループを指定して起動
```bash
$ ecs-cli up --keypair keypair_name --capability-iam --security-group sg-XXXXXXXX --vpc vpc-XXXXXXXX --subnets subnet-XXXXXXXX,subnet-XXXXXXXX
INFO[0001] Created cluster                               cluster=CLUSTERNAME
INFO[0002] Waiting for your cluster resources to be created
INFO[0002] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0062] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0123] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0183] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0243] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS

```

http://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/cmd-ecs-cli-up.html

# コンテナの起動

docker-compose.ymlを作成し、Wordpressのコンテナを起動させます。

```bash:docker-compose.yml
wordpress:
  image: wordpress
  mem_limit: 268435456
  ports:
    - "80:80"
  links:
    - mysql
mysql:
  image: mysql
  mem_limit: 268435456
  environment:
    MYSQL_ROOT_PASSWORD: password
```

ecs-cliで起動します。

```bash
$ ecs-cli compose service up
INFO[0000] Using ECS task definition                     TaskDefinition=ecscompose-compose:1
INFO[0000] Created an ECS Service                        serviceName=ecscompose-service-compose taskDefinition=ecscompose-compose:1
INFO[0000] Updated ECS service successfully              desiredCount=1 serviceName=ecscompose-service-compose
INFO[0001] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0031] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0061] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0091] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0121] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0151] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0181] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0211] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0241] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0271] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0301] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0331] Describe ECS Service status                   desiredCount=1 runningCount=0 serviceName=ecscompose-service-compose
INFO[0361] ECS Service has reached a stable state        desiredCount=1 runningCount=1 serviceName=ecscompose-service-compose
```

確認

```bash
 $ ecs-cli ps                                                                                                    
Name                                                                 State          Ports                            TaskDefinition
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/wordpress  RUNNING  xx.xx.xx.xx:80->80/tcp  ecscompose-compose:1
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/mysql      RUNNING                          ecscompose-compose:1

```

# スケール
インスタンスを増やします。

```bash
$ ecs-cli scale --capability-iam --size 2
INFO[0002] Waiting for your cluster resources to be updated
INFO[0003] Cloudformation stack status  
```

サービスを増やします。

```bash
$ ecs-cli compose service scale 2                                                                                                             [~/dev/ecs/compose]
INFO[0000] Updated ECS service successfully              desiredCount=2 serviceName=ecscompose-service-compose
INFO[0000] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0030] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0060] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0090] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0120] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0150] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0181] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0211] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0241] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0271] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0301] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0331] Describe ECS Service status                   desiredCount=2 runningCount=1 serviceName=ecscompose-service-compose
INFO[0361] ECS Service has reached a stable state        desiredCount=2 runningCount=2 serviceName=ecscompose-service-compose
```

確認

```
 $ ecs-cli ps                                                                                                    
Name                                                                 State          Ports                            TaskDefinition
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/wordpress  RUNNING  xx.xx.xx.xx:80->80/tcp  ecscompose-compose:1
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/mysql      RUNNING                          ecscompose-compose:1
yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy/wordpress  RUNNING  yy.yy.yy.yy:80->80/tcp  ecscompose-compose:1
yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy/mysql      RUNNING                          ecscompose-compose:1
```

# サービスの停止

```bash
$ ecs-cli compose service down                                                                                                                [~/dev/ecs/compose]
INFO[0000] Updated ECS service successfully              desiredCount=0 serviceName=ecscompose-service-compose
INFO[0000] Describe ECS Service status                   desiredCount=0 runningCount=2 serviceName=ecscompose-service-compose
INFO[0015] ECS Service has reached a stable state        desiredCount=0 runningCount=0 serviceName=ecscompose-service-compose
INFO[0015] Deleted ECS service                           service=ecscompose-service-compose
INFO[0015] ECS Service has reached a stable state        desiredCount=0 runningCount=0 serviceName=ecscompose-service-compose
```

# クラスタの削除

```bash
$ ecs-cli down --force                                   
INFO[0001] Waiting for your cluster resources to be deleted
INFO[0001] Cloudformation stack status                   stackStatus=DELETE_IN_PROGRESS
INFO[0062] Cloudformation stack status                   stackStatus=DELETE_IN_PROGRESS
INFO[0123] Deleted cluster                               cluster=CLUSTERNAME
```