ecs-cli

ただの作業メモ

### ecs-cliインストール

```bash

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

```bash

$ sudo chmod +x /usr/local/bin/ecs-cli
$ ecs-cli --version
ecs-cli version 1.19.1 (a20cf74)
```

### ecs-cli設定

既存クラスタの設定

```bash

$ ecs-cli configure --cluster tmp --default-launch-type FARGATE --config-name tmp --region  ap-northeast-1
INFO[0000] Saved ECS CLI cluster configuration tmp. 
```

```bash

$ ecs-cli ps
tmp/a84d9e85f592485093c3079c2f545731/log_router  RUNNING         example-firelens:5  UNKNOWN
tmp/a84d9e85f592485093c3079c2f545731/echoClient  RUNNING         example-firelens:5  UNKNOWN
```

## その他手順

### タスク定義変更

メモリを0.5GB->1.0GB

```bash

$ cat ecs-params.yml
version: 1
task_definition:
  task_execution_role: arn:aws:iam::380900262471:role/container-demo-ECSTaskExecutionRole-11E15F6FQ4LK0
  ecs_network_mode: awsvpc
  task_size:
    mem_limit: 1.0GB
    cpu_limit: 256
run_params:
  network_configuration:
    awsvpc_configuration:
      subnets:
        - "subnet-04ae449a78ae0e9db"
        - "subnet-0e78e9b63b1b5d766"
        - "subnet-0c799d4a80156b236"
      security_groups:
        - "sg-08c5535a1025d630f"
      assign_public_ip: DISABLED
```

```bash
$ecs-cli compose --project-name ecsdemo-frontend up     --cluster-config container-demo
INFO[0001] Using ECS task definition                     TaskDefinition="ecsdemo-frontend:2"
INFO[0002] Starting container...                         container=5079778a-b223-4560-97d6-d20e3a68a164/ecsdemo-frontend
INFO[0002] Describe ECS container status                 container=5079778a-b223-4560-97d6-d20e3a68a164/ecsdemo-frontend desiredStatus=RUNNING lastStatus=PROVISIONING taskDefinition="ecsdemo-frontend:2"
INFO[0015] Describe ECS container status                 container=5079778a-b223-4560-97d6-d20e3a68a164/ecsdemo-frontend desiredStatus=RUNNING lastStatus=PROVISIONING taskDefinition="ecsdemo-frontend:2"
INFO[0028] Describe ECS container status                 container=5079778a-b223-4560-97d6-d20e3a68a164/ecsdemo-frontend desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecsdemo-frontend:2"
INFO[0041] Started container...                          container=5079778a-b223-4560-97d6-d20e3a68a164/ecsdemo-frontend desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecsdemo-frontend:2"
```

タスク定義のrevision2が増えた

<img width="805" alt="スクリーンショット 0002-06-05 9.14.04.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/27932/027911a4-749f-618e-6277-398835d7709a.png">

### タスク数変更

3->1に変更

```bash
$ ecs-cli compose --project-name ecsdemo-frontend service scale 1 --cluster-config container-demo
INFO[0001] Updated ECS service successfully              desiredCount=1 force-deployment=false service=ecsdemo-frontend
INFO[0001] Service status                                desiredCount=1 runningCount=3 serviceName=ecsdemo-frontend
INFO[0016] (service ecsdemo-frontend) has begun draining connections on 2 tasks.  timestamp="2020-06-05 00:21:40 +0000 UTC"
INFO[0016] (service ecsdemo-frontend) deregistered 2 targets in (target-group arn:aws:elasticloadbalancing:us-west-2:380900262471:targetgroup/conta-Dummy-1WSAQNY1HLA5Y/e3853fd9df7d3590)  timestamp="2020-06-05 00:21:40 +0000 UTC"
INFO[0022] Service status                                desiredCount=1 runningCount=1 serviceName=ecsdemo-frontend
INFO[0022] (service ecsdemo-frontend) has stopped 2 running tasks: (task deb83674-e824-47bc-81e2-5f2c6e78b627) (task 7bbb5734-e89f-4252-bd6e-668f42020cdb).  timestamp="2020-06-05 00:21:52 +0000 UTC"
INFO[0022] ECS Service has reached a stable state        desiredCount=1 runningCount=1 serviceName=ecsdemo-frontend
```

### タスク/コンテナ停止

```bash
$  ecs-cli compose --project-name ecsdemo-frontend service scale 0 --cluster-config container-demo
INFO[0001] Updated ECS service successfully              desiredCount=0 force-deployment=false service=ecsdemo-frontend
INFO[0001] Service status                                desiredCount=0 runningCount=3 serviceName=ecsdemo-frontend
INFO[0022] (service ecsdemo-frontend) has begun draining connections on 3 tasks.  timestamp="2020-06-05 23:27:17 +0000 UTC"
INFO[0022] (service ecsdemo-frontend) deregistered 3 targets in (target-group arn:aws:elasticloadbalancing:us-west-2:380900262471:targetgroup/conta-Dummy-1WSAQNY1HLA5Y/e3853fd9df7d3590)  timestamp="2020-06-05 23:27:17 +0000 UTC"
INFO[0033] Service status                                desiredCount=0 runningCount=0 serviceName=ecsdemo-frontend
INFO[0033] (service ecsdemo-frontend) has stopped 3 running tasks: (task e2fda2c5-7260-4938-b30f-1a6c114fabe4) (task 722bbc2c-0efb-489e-b040-7aedf2a52a43) (task a8d250bf-71f7-4b7f-ac09-9d0c40d811c2).  timestamp="2020-06-05 23:27:29 +0000 UTC"
INFO[0033] ECS Service has reached a stable state        desiredCount=0 runningCount=0 serviceName=ecsdemo-frontend
f018984101a4:ecsdemo-frontend umakoto$ ecs-cli compose --project-name ecsdemo-frontend service ps \
>     --cluster-config container-demo
Name                                                   State                Ports                        TaskDefinition      Health
722bbc2c-0efb-489e-b040-7aedf2a52a43/ecsdemo-frontend  STOPPED ExitCode: 0  10.0.102.211:3000->3000/tcp  ecsdemo-frontend:4  UNKNOWN
a8d250bf-71f7-4b7f-ac09-9d0c40d811c2/ecsdemo-frontend  STOPPED ExitCode: 0  10.0.101.253:3000->3000/tcp  ecsdemo-frontend:4  UNKNOWN
e2fda2c5-7260-4938-b30f-1a6c114fabe4/ecsdemo-frontend  STOPPED ExitCode: 0  10.0.100.131:3000->3000/tcp  ecsdemo-frontend:4  UNKNOWN
```

### タスク/コンテナ起動

```bash
$  ecs-cli compose --project-name ecsdemo-frontend service scale 3 --cluster-config container-demo
INFO[0001] Updated ECS service successfully              desiredCount=3 force-deployment=false service=ecsdemo-frontend
INFO[0006] (service ecsdemo-frontend) has started 3 tasks: (task 71408cb4-8d26-4967-93a6-1157f29a63fd) (task 4b76792c-07ce-413e-b573-dc27080d7489) (task a3ca5d35-2973-4080-aac9-3fee4dba1b3b).  timestamp="2020-06-05 23:29:11 +0000 UTC"
INFO[0064] Service status                                desiredCount=3 runningCount=2 serviceName=ecsdemo-frontend
INFO[0069] Service status                                desiredCount=3 runningCount=3 serviceName=ecsdemo-frontend
INFO[0069] ECS Service has reached a stable state        desiredCount=3 runningCount=3 serviceName=ecsdemo-frontend
f018984101a4:ecsdemo-frontend umakoto$ ecs-cli compose --project-name ecsdemo-frontend service ps     --cluster-config container-demo
Name                                                   State                Ports                        TaskDefinition      Health
4b76792c-07ce-413e-b573-dc27080d7489/ecsdemo-frontend  RUNNING              10.0.102.213:3000->3000/tcp  ecsdemo-frontend:4  UNKNOWN
71408cb4-8d26-4967-93a6-1157f29a63fd/ecsdemo-frontend  RUNNING              10.0.101.236:3000->3000/tcp  ecsdemo-frontend:4  UNKNOWN
a3ca5d35-2973-4080-aac9-3fee4dba1b3b/ecsdemo-frontend  RUNNING              10.0.100.126:3000->3000/tcp  ecsdemo-frontend:4  UNKNOWN
722bbc2c-0efb-489e-b040-7aedf2a52a43/ecsdemo-frontend  STOPPED ExitCode: 0  3000->3000/tcp               ecsdemo-frontend:4  UNKNOWN
a8d250bf-71f7-4b7f-ac09-9d0c40d811c2/ecsdemo-frontend  STOPPED ExitCode: 0  3000->3000/tcp               ecsdemo-frontend:4  UNKNOWN
e2fda2c5-7260-4938-b30f-1a6c114fabe4/ecsdemo-frontend  STOPPED ExitCode: 0  3000->3000/tcp               ecsdemo-frontend:4  UNKNOWN
f018984101a4:ecsdemo-frontend umakoto$ 
```

### params.ymlの更新

params.ymlの中のタスク定義部分を更新
そのあとで以下のcompose service upを実施すると、タスク定義のrevisionが4->5になり、ECSのサービスが新しいタスク定義で更新されます。古いコンテナーは自動的に停止され、新しいコンテナーに置き換えられます

```bash
ecs-cli compose --project-name ecsdemo-frontend service up   --private-dns-namespace service       --cluster-config container-demo     --vpc $vpc
config container-demo     --vpc $vpc
INFO[0001] Using ECS task definition                     TaskDefinition="ecsdemo-frontend:5"
INFO[0002] Updated the ECS service with a new task definition. Old containers will be stopped automatically, and replaced with new ones  desiredCount=3 force-deployment=false service=ecsdemo-frontend
INFO[0002] Service status                                desiredCount=3 runningCount=3 serviceName=ecsdemo-frontend
INFO[0018] (service ecsdemo-frontend) has started 3 tasks: (task 0a423400-fdd8-446f-b056-a7a462a07e72) (task 21430037-5b4f-45e3-a1b5-4dae69c01f16) (task 2fde5c1f-2f29-4ee2-b3e3-4bd698156851).  timestamp="2020-06-05 23:40:11 +0000 UTC"
INFO[0075] Service status                                desiredCount=3 runningCount=5 serviceName=ecsdemo-frontend
INFO[0081] Service status                                desiredCount=3 runningCount=6 serviceName=ecsdemo-frontend
INFO[0081] (service ecsdemo-frontend) registered 1 targets in (target-group arn:aws:elasticloadbalancing:us-west-2:380900262471:targetgroup/conta-Dummy-1WSAQNY1HLA5Y/e3853fd9df7d3590)  timestamp="2020-06-05 23:41:13 +0000 UTC"
INFO[0091] (service ecsdemo-frontend) registered 2 targets in (target-group arn:aws:elasticloadbalancing:us-west-2:380900262471:targetgroup/conta-Dummy-1WSAQNY1HLA5Y/e3853fd9df7d3590)  timestamp="2020-06-05 23:41:26 +0000 UTC"
INFO[0117] (service ecsdemo-frontend) deregistered 3 targets in (target-group arn:aws:elasticloadbalancing:us-west-2:380900262471:targetgroup/conta-Dummy-1WSAQNY1HLA5Y/e3853fd9df7d3590)  timestamp="2020-06-05 23:41:57 +0000 UTC"
INFO[0117] (service ecsdemo-frontend) has begun draining connections on 3 tasks.  timestamp="2020-06-05 23:41:57 +0000 UTC"
INFO[0128] Service status                                desiredCount=3 runningCount=3 serviceName=ecsdemo-frontend
INFO[0133] (service ecsdemo-frontend) has stopped 3 running tasks: (task 4b76792c-07ce-413e-b573-dc27080d7489) (task a3ca5d35-2973-4080-aac9-3fee4dba1b3b) (task 71408cb4-8d26-4967-93a6-1157f29a63fd).  timestamp="2020-06-05 23:42:07 +0000 UTC"
INFO[0144] ECS Service has reached a stable state        desiredCount=3 runningCount=3 serviceName=ecsdemo-frontend
```

### リビジョン指定(タスク)

リビジョン4

```
$ cat ecs-params-4.yml 
version: 7
task_definition:
  task_execution_role: arn:aws:iam::380900262471:role/container-demo-ECSTaskExecutionRole-11E15F6FQ4LK0
  ecs_network_mode: awsvpc
  task_size:
    mem_limit: 1.0GB
    cpu_limit: 256
run_params:
  network_configuration:
    awsvpc_configuration:
      subnets:
        - "subnet-04ae449a78ae0e9db"
        - "subnet-0e78e9b63b1b5d766"
        - "subnet-0c799d4a80156b236"
      security_groups:
        - "sg-08c5535a1025d630f"
      assign_public_ip: DISABLED
```

リビジョン5

```
$ cat ecs-params-5.yml 
version: 7
task_definition:
  task_execution_role: arn:aws:iam::380900262471:role/container-demo-ECSTaskExecutionRole-11E15F6FQ4LK0
  ecs_network_mode: awsvpc
  task_size:
    mem_limit: 0.5GB
    cpu_limit: 256
run_params:
  network_configuration:
    awsvpc_configuration:
      subnets:
        - "subnet-04ae449a78ae0e9db"
        - "subnet-0e78e9b63b1b5d766"
        - "subnet-0c799d4a80156b236"
      security_groups:
        - "sg-08c5535a1025d630f"
      assign_public_ip: DISABLED
```

```bash

ecs-cli compose --project-name ecsdemo-frontend --ecs-params ecs-params-5.yml up     --cluster-config container-demo 
```

revition5, revision6は作った。次にecs-params-6.ymlを作り上記コマンド実行。revition7が出来た。(一度revision6を作ってしまってrevisionの解除をしてたので1つ飛んでしまった。まぁいいや)

```bash
$ ecs-cli compose --project-name ecsdemo-frontend --ecs-params ecs-params-6.yml up     --cluster-config container-demo 
INFO[0001] Using ECS task definition                     TaskDefinition="ecsdemo-frontend:7"
INFO[0001] Found existing ECS tasks for project          CountOfTasks=1 ProjectName=ecsdemo-frontend
INFO[0001] Updating to new task definition               taskDefinition="arn:aws:ecs:us-west-2:380900262471:task-definition/ecsdemo-frontend:7"
INFO[0002] Describe ECS container status                 container=8a13bb04-6605-4d1c-b9b6-205b039665a0/ecsdemo-frontend desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecsdemo-frontend:4"
INFO[0002] Describe ECS container status                 container=e235f2a5-491a-4dc3-8a79-0e549d125fd9/ecsdemo-frontend desiredStatus=RUNNING lastStatus=PROVISIONING taskDefinition="ecsdemo-frontend:7"
INFO[0016] Describe ECS container status                 container=8a13bb04-6605-4d1c-b9b6-205b039665a0/ecsdemo-frontend desiredStatus=STOPPED lastStatus=DEPROVISIONING taskDefinition="ecsdemo-frontend:4"
INFO[0016] Describe ECS container status                 container=e235f2a5-491a-4dc3-8a79-0e549d125fd9/ecsdemo-frontend desiredStatus=RUNNING lastStatus=PROVISIONING taskDefinition="ecsdemo-frontend:7"
INFO[0029] Describe ECS container status                 container=e235f2a5-491a-4dc3-8a79-0e549d125fd9/ecsdemo-frontend desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecsdemo-frontend:7"
INFO[0029] Stopped container...                          container=8a13bb04-6605-4d1c-b9b6-205b039665a0/ecsdemo-frontend desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecsdemo-frontend:4"
INFO[0041] Started container...                          container=e235f2a5-491a-4dc3-8a79-0e549d125fd9/ecsdemo-frontend desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecsdemo-frontend:7"
```

revision4に戻す

```
$ ecs-cli compose --project-name ecsdemo-frontend --ecs-params ecs-params-4.yml up     --cluster-config container-demo 
```

### リビジョン指定(サービス)

タスクの時と同様にリビジョンごとのecs-params.ymlを用意した。ecs-cli compose service upにそれぞれ引数として渡せればいいができない。

ドキュメントにはないが、以下のコマンドはecs-params.ymlがないと Error registering task definitionとエラーが出る。ecs-params.ymlに書かれたタスク定義をロードしようとしてるっぽいことは確認できた。

明示的に引数でecs-params.ymlの指定できないので、指定したいリビジョンのファイルのシンボリックリンクをecs-params.ymlの名前にしておく。それで指定したリビジョンのタスク定義でECSサービスを更新できる。

```
ecs-cli compose --project-name ecsdemo-frontend service up   --private-dns-namespace service       --cluster-config container-demo     --vpc $vpc
```

## コマンド 

### ecs-cli compose up

Amazon ECS タスク定義が存在しない場合は、Compose ファイルから作成し、そのタスクの 1 つのインスタンスをクラスターで実行します




# AWS公式のecs-cliハンズオン

https://ecsworkshop.com/frontend/frontend/#deploy-frontend-1