Help us understand the problem. What is going on with this article?

[JAWS-UG CLI] AutoScaling:#6 異常インスタンスの停止

More than 3 years have passed since last update.

前提条件

AutoScalingへの権限

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

EC2への権限

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

AWS CLIのバージョン

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

  • AWS CLI 1.10.58
コマンド
aws --version

結果(例):

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

0. 準備

0.1. リージョンの決定

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

0.2. 変数の確認:

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

変数の確認
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

1. 事前作業

対象インスタンスの指定

変数の設定
EC2_INSTANCE_ID=<稼動中のインスタンスのID>

2. 異常インスタンスの停止

変数の設定
AS_INSTANCE_HEALTH_STAT='Unhealthy'
変数の確認
cat << ETX

        EC2_INSTANCE_ID:         ${EC2_INSTANCE_ID}
        AS_INSTANCE_HEALTH_STAT: ${AS_INSTANCE_HEALTH_STAT}

ETX
コマンド
aws autoscaling set-instance-health \
        --instance-id ${EC2_INSTANCE_ID} \
        --health-status ${AS_INSTANCE_HEALTH_STAT}

結果(例):

  (戻り値なし)

3. 事後作業

コマンド
aws autoscaling describe-auto-scaling-instances 

結果(例):

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

"HealthStatus"が"UNHEALTHY"になり、"LifecycleState"が"Terminating"になります。

しばらくすると、以下のように別のインスタンスが起動してきます。

結果(例):

  {
    "AutoScalingInstances": [
      {
          "ProtectedFromScaleIn": false,
          "AvailabilityZone": "ap-northeast-1c",
          "InstanceId": "i-xxxxxxxx",
          "AutoScalingGroupName": "asgroup-handson-20160829",
          "HealthStatus": "HEALTHY",
          "LifecycleState": "InService",
          "LaunchConfigurationName": "launchcongig-handson-20160829"
      }
    ]
  }
コマンド
aws ec2 describe-instances \
        --filters Name=instance-state-name,Values=${EC2_INSTANCE_STATUS} \
        --query 'Reservations[].Instances[].InstanceId'

結果(例):

  [
    "i-xxxxxxxx"
  ]

完了

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away