[JAWS-UG CLI] AutoScaling:#7 Auto Scalingグループの削除




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


AutoScalingへの権限
-------------------

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


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

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


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

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

* AWS CLI 1.10.58

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

結果(例):

      aws-cli/1.10.58 Python/2.7.11 Darwin/15.6.0 botocore/1.4.48


0. 準備
=======


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

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


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

プロファイルが想定のものになっていることを確認します。

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

結果(例):

            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       ec2as_full-prjZ-mbp13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region        ap-northeast-1        env    AWS_DEFAULT_REGION


0.3. AutoScalingグループ名の指定
--------------------------------

```bash:変数の設定:
AS_GROUP_NAME="asgroup-handson-$(date +%Y%m%d)" \
          && echo ${AS_GROUP_NAME}
```

```bash:コマンド:
aws autoscaling describe-auto-scaling-groups \
        --auto-scaling-group-names ${AS_GROUP_NAME}
```

結果(例):

      {
        "AutoScalingGroups": [
          {
              "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:XXXXXXXXXXXX:autoScalingGroup:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:autoScalingGroupName/asgroup-handson-20160829",
              "HealthCheckGracePeriod": 0,
              "SuspendedProcesses": [],
              "DesiredCapacity": 11,
              "Tags": [],
              "EnabledMetrics": [],
              "LoadBalancerNames": [],
              "AutoScalingGroupName": "asgroup-handson-20160829",
              "DefaultCooldown": 300,
              "MinSize": |AS_GROUP_MIN|,
              "Instances": [
                  {
                      "InstanceId": "i-xxxxxxxx",
                      "AvailabilityZone": "ap-northeast-1",
                      "HealthStatus": "Healthy",
                      "LifecycleState": "InService",
                      "LaunchConfigurationName": "launchcongig-handson-20160829"
                  }
              ],
              "MaxSize": 2,
              "VPCZoneIdentifier": "subnet-xxxxxxxx",
              "TerminationPolicies": [
                  "Default"
              ],
              "LaunchConfigurationName": "launchcongig-handson-20160829",
              "CreatedTime": "2015-08-01T01:23:45.678Z",
              "AvailabilityZones": [
                  "ap-northeast-1a"
              ],
              "HealthCheckType": "EC2"
          }
        ]
      }


1. 事前作業
===========


1.1. 稼動インスタンスを確認
---------------------------

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

```bash:変数の設定:
EC2_INSTANCE_STATUS='running'
```

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

結果(例):

      i-xxxxxxxx

```bash:コマンド:
aws autoscaling describe-auto-scaling-instances 
```

結果(例):

      {
        "AutoScalingInstances": [
          {
              "AvailabilityZone": "ap-northeast-1a",
              "InstanceId": "i-xxxxxxxx",
              "AutoScalingGroupName": "asgroup-handson-20160829",
              "HealthStatus": "HEALTHY",
              "LifecycleState": "InService",
              "LaunchConfigurationName": "launchcongig-handson-20160829"
          }
        ]
      }


1.2. AutoScalingグループ名の最小値変更
--------------------------------------

```bash:変数の設定:
AS_GROUP_MIN='0'
```

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

        AS_GROUP_NAME: ${AS_GROUP_NAME}
        AS_GROUP_MIN:  ${AS_GROUP_MIN}

ETX
```

```bash:コマンド:
aws autoscaling update-auto-scaling-group \
        --auto-scaling-group-name ${AS_GROUP_NAME} \
        --min-size ${AS_GROUP_MIN}
```

結果(例):

      (戻り値なし)

```bash:コマンド:
AS_GROUP_MIN=$( \
        aws autoscaling describe-auto-scaling-groups \
          --auto-scaling-group-names ${AS_GROUP_NAME} \
          --query 'AutoScalingGroups[].MinSize' \
          --output text \
) && echo ${AS_GROUP_MIN}
```

結果(例):

      0


1.3. AutoScalingグループ名の稼動インスタンス数の変更
----------------------------------------------------

```bash:変数の設定:
AS_DESIRED_CAPACITY=0
```

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

        AS_GROUP_NAME:       ${AS_GROUP_NAME}
        AS_DESIRED_CAPACITY: ${AS_DESIRED_CAPACITY}

ETX
```

```bash:コマンド:
aws autoscaling set-desired-capacity \
        --auto-scaling-group-name ${AS_GROUP_NAME} \
        --desired-capacity ${AS_DESIRED_CAPACITY}
```

結果(例):

      (戻り値なし)

```bash:コマンド:
AS_DESIRED_CAPACITY=$( \
        aws autoscaling describe-auto-scaling-groups \
          --auto-scaling-group-names ${AS_GROUP_NAME} \
          --query 'AutoScalingGroups[].DesiredCapacity' \
          --output text \
) && echo ${AS_DESIRED_CAPACITY}
```

結果(例):

      0


1.4. activityの確認
-------------------

```bash:変数の設定:
MAX_ITEMS='1'
```

```bash:コマンド:
aws autoscaling describe-scaling-activities \
        --auto-scaling-group-name ${AS_GROUP_NAME} \
        --max-items ${MAX_ITEMS}
```

結果(例):

      {
        "Activities": [
          {
              "Description": "Terminating EC2 instance: i-xxxxxxxx",
              "AutoScalingGroupName": "asgroup-handson-20160829",
              "ActivityId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
              "Details": "{"Subnet ID":"subnet-xxxxxxxx","Availability Zone":"ap-northeast-1b"}",
              "StartTime": "2016-08-29T01:23:45.678Z",
              "Progress": 100,
              "EndTime": "2016-08-29T01:23:45Z",
              "Cause": "At 2016-08-29T01:23:45Z a user request explicitly set group desired capacity changing the desired capacity from 1 to 0.  At 2016-08-29T01:23:45Z an instance was taken out of service in response to a difference between desired and actual capacity, shrinking the capacity from 1 to 0.  At 2016-08-29T01:23:45Z instance i-xxxxxxxx was selected for termination.",
              "StatusCode": "Successful"
          }
        ],
        "NextToken": "eyJOZXh0VG9rZW4iOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAxfQ=="
      }


1.5. 稼動インスタンスの確認
---------------------------

```bash:コマンド:
AS_GROUP_INSTANCES=$( \
        aws autoscaling describe-auto-scaling-groups \
          --auto-scaling-group-names ${AS_GROUP_NAME} \
          --query 'AutoScalingGroups[].Instances' \
          --output text \
) && echo ${AS_GROUP_INSTANCES}
```

結果(例):

      (戻り値なし)

```bash:コマンド:
aws autoscaling describe-auto-scaling-instances 
```

結果(例):

      {
          "AutoScalingInstances": []
      }


2. AutoScalingグループの削除
============================


2.1. AutoScalingグループの削除
------------------------------

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

        AS_GROUP_NAME: ${AS_GROUP_NAME}

ETX
```

```bash:コマンド:
aws autoscaling delete-auto-scaling-group \
        --auto-scaling-group-name ${AS_GROUP_NAME}
```

結果:

      (戻り値なし)


2.2. AutoScalingグループの確認
------------------------------

```bash:コマンド:
aws autoscaling describe-auto-scaling-groups \
        --auto-scaling-group-names ${AS_GROUP_NAME} \
        --query 'AutoScalingGroups[].Status' \
        --output text
```

結果(例):

      Delete in progress"


2.3. Scalingアクティビティの確認
--------------------------------

ValidationErrorが表示されます。

```bash:コマンド:
aws autoscaling describe-scaling-activities \
        --auto-scaling-group-name ${AS_GROUP_NAME}
```

結果(例):

      A client error (ValidationError) occurred when calling the DescribeScalingActivities operation: AutoScalingGroup name not found - AutoScalingGroup asgroup-handson-20160829not found


完了
====