[JAWS-UG CLI] ECS #3 ECSコンテナインスタンスとECSクラスタの破棄

前提条件
========


ECSへの権限
-----------

* ECSに対してフル権限があること。


EC2への権限
-----------

* EC2に対してフル権限があること。


AWS CLIのバージョン
-------------------

* 以下のバージョンで動作確認済

  * AWS CLI 1.7.36

```bash:コマンド:
aws --version
```

```text:結果(例):
      aws-cli/1.7.28 Python/2.7.5 Darwin/13.4.0
```


0. 準備
=======


0.1. リージョンの決定
---------------------

```bash:変数の設定:
export AWS_DEFAULT_REGION='ap-northeast-1'
```


0.2. 変数の確認
---------------

```bash:変数の確認:
aws configure list
```

```text:結果(例):
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile   ec2ecs_full-prjZ-mbp13              env    AWS_DEFAULT_PROFILE
      access_key     ****************LOAQ shared-credentials-file
      secret_key     ****************I1O1 shared-credentials-file
          region           ap-northeast-1              env    AWS_DEFAULT_REGION
```


0.3. ECSクラスタ名の指定
------------------------

```bash:変数の設定:
ECS_CLUSTER_NAME='handson-cluster'
```


1. ECSコンテナインスタンスの登録削除
====================================


1.1. ECSクラスタの確認
----------------------

```bash:コマンド:
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
```

```json:結果(例)
       {
         "clusters": [
           {
             "status": "ACTIVE",
             "clusterName": "handson-cluster",
             "registeredContainerInstancesCount": 1,
             "pendingTasksCount": 0,
             "runningTasksCount": 0,
             "activeServicesCount": 0,
             "clusterArn": "arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:cluster/handson-cluster"
           }
         ],
         "failures": []
      }
```


1.2. ECSコンテナインスタンスの状態確認
--------------------------------------

```bash:コマンド:
ECS_INSTANCES=$( \
        aws ecs list-container-instances \
          --cluster ${ECS_CLUSTER_NAME} \
          --query 'containerInstanceArns[]' \
          --output text | \
        sed 's|^arn:aws:ecs:.*:container-instance/||' \
      ) \
        && echo ${ECS_INSTANCES}
```

```text:結果(例):
      xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
```

```bash:コマンド:
ARRAY_ECS_INSTANCE_STATUS=$( \
        aws ecs describe-container-instances \
          --cluster ${ECS_CLUSTER_NAME} \
          --container-instances "${ECS_INSTANCES}" \
          --query 'containerInstances[].status' \
          --output text \
      ) \
        && echo ${ARRAY_ECS_INSTANCE_STATUS}
```

```text:結果:
      ACTIVE
```


1.3. EC2インスタンスIDの取得
----------------------------

```bash:コマンド:
ARRAY_EC2_INSTANCE_ID=$( \
        aws ecs describe-container-instances \
          --cluster ${ECS_CLUSTER_NAME} \
          --container-instances "${ECS_INSTANCES}" \
          --query 'containerInstances[].ec2InstanceId' \
          --output text \
      ) \
        && echo ${ARRAY_EC2_INSTANCE_ID}
```

```text:結果(例):
      i-xxxxxxxx
```


1.4. ECSコンテナインスタンスの登録削除
--------------------------------------

```bash:変数の確認:
cat << ETX

        ECS_CLUSTER_NAME: ${ECS_CLUSTER_NAME}
        ECS_INSTANCES:    ${ECS_INSTANCES}

ETX
```

```bash:コマンド:
aws ecs deregister-container-instance \
        --cluster ${ECS_CLUSTER_NAME} \
        --container-instance ${ECS_INSTANCES}
```

```json:結果(例):
      {
        "containerInstance": {
          "status": "INACTIVE",
          "registeredResources": [
              {
                  "integerValue": 1024,
                  "longValue": 0,
                  "type": "INTEGER",
                  "name": "CPU",
                  "doubleValue": 0.0
              },
              {
                  "integerValue": 996,
                  "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": "i-xxxxxxxx",
          "agentConnected": true,
          "containerInstanceArn": "arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:container-instance/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
          "pendingTasksCount": 0,
          "remainingResources": [
              {
                  "integerValue": 1024,
                  "longValue": 0,
                  "type": "INTEGER",
                  "name": "CPU",
                  "doubleValue": 0.0
              },
              {
                  "integerValue": 996,
                  "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,
          "versionInfo": {
              "agentVersion": "1.2.1",
              "agentHash": "5da1555",
              "dockerVersion": "DockerVersion: 1.6.2"
          }
        }
      }
```


1.5. ECSコンテナインスタンスの状態確認
--------------------------------------

```bash:コマンド:
ARRAY_ECS_INSTANCE_STATUS=$( \
        aws ecs describe-container-instances \
          --cluster ${ECS_CLUSTER_NAME} \
          --container-instances "${ECS_INSTANCES}" \
          --query 'containerInstances[].status' \
          --output text \
      ) \
        && echo ${ARRAY_ECS_INSTANCE_STATUS}
```

```text:結果:
      INACTIVE
```


1.6. ECSクラスタの確認
----------------------

```bash:コマンド:
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
```

```json:結果(例):
      {
        "clusters": [
          {
              "status": "ACTIVE",
              "clusterName": "handson-cluster",
              "registeredContainerInstancesCount": 0,
              "pendingTasksCount": 0,
              "runningTasksCount": 0,
              "activeServicesCount": 0,
              "clusterArn": "arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:cluster/handson-cluster"
          }
        ],
        "failures": []
      }
```


2. EC2インスタンス削除
======================


2.1. EC2インスタンス削除
------------------------

```bash:変数の設定:
EC2_INSTANCE_ID=$(echo ${ARRAY_EC2_INSTANCE_ID} | sed 's/ .*$//') \
        && echo ${EC2_INSTANCE_ID}
```

```bash:変数の確認:
cat << ETX

        EC2_INSTANCE_ID: ${EC2_INSTANCE_ID}

ETX
```

```bash:コマンド:
aws ec2 terminate-instances --instance-ids ${EC2_INSTANCE_ID}
```

```json:結果(例):
      {
        "TerminatingInstances": [
          {
              "InstanceId": "i-xxxxxxxx",
              "CurrentState": {
                  "Code": 32,
                  "Name": "shutting-down"
              },
              "PreviousState": {
                  "Code": 16,
                  "Name": "running"
              }
          }
        ]
      }
```


2.2. EC2インスタンスの状態確認
------------------------------

```bash:コマンド:
EC2_INSTANCE_STATE=` \
        aws ec2 describe-instances \
        --instance-ids ${EC2_INSTANCE_ID} \
        --query 'Reservations[].Instances[].State.Name' \
        --output text \
      ` \
        && echo ${EC2_INSTANCE_STATE}
```

```text:結果:
      terminated
```

同一リージョンでインスタンスが起動していないことを確認します。

```bash:コマンド:
ARRAY_EC2_INSTANCE_ID=` \
        aws ec2 describe-instances \
          --filters Name=instance-state-name,Values=running \
          --query 'Reservations[].Instances[].InstanceId' \
          --output text \
      ` \
        && echo ${ARRAY_EC2_INSTANCE_ID}
```

```text:結果:
      (戻り値なし)
```


3. ECSクラスタの削除
====================


3.1. ECSクラスタの確認
----------------------

```bash:コマンド:
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
```

```json:結果(例):
      {
        "clusters": [
          {
              "status": "ACTIVE",
              "clusterName": "handson-cluster",
              "registeredContainerInstancesCount": 0,
              "pendingTasksCount": 0,
              "runningTasksCount": 0,
              "activeServicesCount": 0,
              "clusterArn": "arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:cluster/handson-cluster"
          }
        ],
        "failures": []
      }
```


3.2. ECSクラスタの削除
----------------------

```bash:変数の確認:
cat << ETX

        ECS_CLUSTER_NAME: ${ECS_CLUSTER_NAME}

ETX
```

```bash:コマンド:
aws ecs delete-cluster \
        --cluster ${ECS_CLUSTER_NAME}
```

```json:結果(例):
      {
        "cluster": {
          "status": "INACTIVE",
          "clusterName": "handson-cluster",
          "registeredContainerInstancesCount": 0,
          "pendingTasksCount": 0,
          "runningTasksCount": 0,
          "activeServicesCount": 0,
          "clusterArn": "arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:cluster/handson-cluster"
        }
      }
```


3.3. ECSクラスタの確認
----------------------

```bash:コマンド:
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
```

```json:結果(例)
      {
        "clusters": [],
        "failures": [
          {
              "reason": "MISSING",
              "arn": "arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:cluster/handson-cluster"
          }
        ]
      }
```

```bash:コマンド:
aws ecs list-clusters 
```

```json:結果:
      {
          "clusterArns": []
      }
```


完了
====