4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-07-06

前提条件

ECSへの権限

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

EC2への権限

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

AWS CLIのバージョン

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

    • AWS CLI 1.7.36
コマンド
aws --version
結果(例)
      aws-cli/1.7.28 Python/2.7.5 Darwin/13.4.0
  1. 準備
    =======

0.1. リージョンの決定

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

0.2. 変数の確認

変数の確認
aws configure list
結果(例)
            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クラスタ名の指定

変数の設定
ECS_CLUSTER_NAME='handson-cluster'
  1. ECSコンテナインスタンスの登録削除
    ====================================

1.1. ECSクラスタの確認

コマンド
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
結果(例)
       {
         "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コンテナインスタンスの状態確認

コマンド
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}
結果(例)
      xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
コマンド
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}
結果
      ACTIVE

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

コマンド
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}
結果(例)
      i-xxxxxxxx

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

変数の確認
cat << ETX

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

ETX
コマンド
aws ecs deregister-container-instance \
        --cluster ${ECS_CLUSTER_NAME} \
        --container-instance ${ECS_INSTANCES}
結果(例)
      {
        "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コンテナインスタンスの状態確認

コマンド
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}
結果
      INACTIVE

1.6. ECSクラスタの確認

コマンド
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
結果(例)
      {
        "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": []
      }
  1. EC2インスタンス削除
    ======================

2.1. EC2インスタンス削除

変数の設定
EC2_INSTANCE_ID=$(echo ${ARRAY_EC2_INSTANCE_ID} | sed 's/ .*$//') \
        && echo ${EC2_INSTANCE_ID}
変数の確認
cat << ETX

        EC2_INSTANCE_ID: ${EC2_INSTANCE_ID}

ETX
コマンド
aws ec2 terminate-instances --instance-ids ${EC2_INSTANCE_ID}
結果(例)
      {
        "TerminatingInstances": [
          {
              "InstanceId": "i-xxxxxxxx",
              "CurrentState": {
                  "Code": 32,
                  "Name": "shutting-down"
              },
              "PreviousState": {
                  "Code": 16,
                  "Name": "running"
              }
          }
        ]
      }

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

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

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

コマンド
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}
結果
      (戻り値なし)
  1. ECSクラスタの削除
    ====================

3.1. ECSクラスタの確認

コマンド
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
結果(例)
      {
        "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クラスタの削除

変数の確認
cat << ETX

        ECS_CLUSTER_NAME: ${ECS_CLUSTER_NAME}

ETX
コマンド
aws ecs delete-cluster \
        --cluster ${ECS_CLUSTER_NAME}
結果(例)
      {
        "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クラスタの確認

コマンド
aws ecs describe-clusters \
        --clusters ${ECS_CLUSTER_NAME}
結果(例)
      {
        "clusters": [],
        "failures": [
          {
              "reason": "MISSING",
              "arn": "arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:cluster/handson-cluster"
          }
        ]
      }
コマンド
aws ecs list-clusters 
結果
      {
          "clusterArns": []
      }

完了

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?