「AWS Hands-on for Beginners Amazon EC2 Auto Scaling スケーリング基礎編 」をAWS CLIでやってみる

https://pages.awscloud.com/JAPAN-event-OE-Hands-on-for-Beginners-Auto_Scaling-2022-reg-event.html?trk=aws_introduction_page

上記、「AWS Hands-on for Beginners Amazon EC2 Auto Scaling スケーリング基礎編」 をAWS CLIでやってみる

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3297641/aba15f67-64e4-cc9d-99e2-aa858a878938.png)
ハンズオンから引用

# 1. ハンズオンの事前準備編
## 変数
```bash:コマンド
# スタック名
STACK_NAME="h4b-stack" \
&& echo ${STACK_NAME}

# スタックファイル
STACK_FILE="h4b_ec2autoscaling_template.json" \
&& echo ${STACK_FILE}
```
```bash:出力
[cloudshell-user@ip-10-130-32-232 ~]$ # スタック名
[cloudshell-user@ip-10-130-32-232 ~]$ STACK_NAME="h4b-stack" \
> && echo ${STACK_NAME}
h4b-stack
[cloudshell-user@ip-10-130-32-232 ~]$ 
[cloudshell-user@ip-10-130-32-232 ~]$ # スタックファイル
[cloudshell-user@ip-10-130-32-232 ~]$ STACK_FILE="h4b_ec2autoscaling_template.json" \
> && echo ${STACK_FILE}
h4b_ec2autoscaling_template.json
```
## スタックの作成
```bash:コマンド
aws cloudformation create-stack \
    --stack-name ${STACK_NAME} \
    --template-body file://${STACK_FILE}
```
```bash:出力
[cloudshell-user@ip-10-130-32-232 ~]$ aws cloudformation create-stack \
>     --stack-name ${STACK_NAME} \
>     --template-body file://${STACK_FILE}
{
    "StackId": "arn:aws:cloudformation:ap-northeast-1:999999999999:stack/h4b-stack/969bf810-3b93-11ef-9fae-0ece4a7ae6a7"
}
```
# 2. スケジュールスケーリング（１）～起動テンプレートの作成～
## 変数設定
```bash:コマンド
# 起動テンプレート名
TEMPLATE_NAME="h4b-template" \
&& echo ${TEMPLATE_NAME}

# インスタンスタイプ
INSTANCETYPE="t2.micro" \
&& echo ${INSTANCETYPE}

# セキュリティグループID
LOGICAL_RESOURCE_ID="EC2SecurityGroup" \
&& echo ${LOGICAL_RESOURCE_ID}

EC2SECURITYGROUP_ID=$(
    aws cloudformation describe-stack-resources \
        --stack-name ${STACK_NAME} \
        --logical-resource-id ${LOGICAL_RESOURCE_ID} \
        --query "StackResources[*].PhysicalResourceId" \
        --output text
) \
&& echo ${EC2SECURITYGROUP_ID}
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ # 起動テンプレート名
[cloudshell-user@ip-10-130-61-139 ~]$ TEMPLATE_NAME="h4b-template" \
> && echo ${TEMPLATE_NAME}
h4b-template
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # インスタンスタイプ
[cloudshell-user@ip-10-130-61-139 ~]$ INSTANCETYPE="t2.micro" \
> && echo ${INSTANCETYPE}
t2.micro
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # セキュリティグループID
[cloudshell-user@ip-10-130-61-139 ~]$ LOGICAL_RESOURCE_ID="EC2SecurityGroup" \
> && echo ${LOGICAL_RESOURCE_ID}
EC2SecurityGroup
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ EC2SECURITYGROUP_ID=$(
>     aws cloudformation describe-stack-resources \
>         --stack-name ${STACK_NAME} \
>         --logical-resource-id ${LOGICAL_RESOURCE_ID} \
>         --query "StackResources[*].PhysicalResourceId" \
>         --output text
> ) \
> && echo ${EC2SECURITYGROUP_ID}
sg-0155c9aadcfb1cee8
```
## ユーザーデータ作成
```bash:コマンド
cat <<'EOF' > launch_user_data.txt
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo amazon-linux-extras install epel -y
sudo yum install stress -y
sudo systemctl start httpd
sudo echo `hostname` > /var/www/html/index.html
sudo echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
sudo echo "ClientAliveCountMax 120" >> /etc/ssh/sshd_config
sudo systemctl restart sshd.service
EOF
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ cat <<'EOF' > launch_user_data.txt
> #!/bin/bash
> sudo yum update -y
> sudo yum install -y httpd
> sudo amazon-linux-extras install epel -y
> sudo yum install stress -y
> sudo systemctl start httpd
> sudo echo `hostname` > /var/www/html/index.html
> sudo echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
> sudo echo "ClientAliveCountMax 120" >> /etc/ssh/sshd_config
> sudo systemctl restart sshd.service
> EOF
```
## 起動テンプレートの作成
```bash:コマンド
# 起動テンプレートJSONの作成
LAUNCH_TEMPLATE_DATA_JSON=$(cat << EOF
{
    "InstanceType": "${INSTANCETYPE}",
    "ImageId": "resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2",
    "SecurityGroupIds": ["${EC2SECURITYGROUP_ID}"],
    "TagSpecifications": [
        {
            "ResourceType": "instance",
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "h4b-instance"
                }
            ]
        }
    ],
    "Monitoring": {
        "Enabled": true
    },
    "UserData": "`cat launch_user_data.txt | base64 -w 0`"
}
EOF
) \
&& echo ${LAUNCH_TEMPLATE_DATA_JSON}

# 起動テンプレートの作成
aws ec2 create-launch-template \
    --launch-template-name ${TEMPLATE_NAME}\
    --launch-template-data "${LAUNCH_TEMPLATE_DATA_JSON}"
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ # 起動テンプレートJSONの作成
[cloudshell-user@ip-10-130-61-139 ~]$ LAUNCH_TEMPLATE_DATA_JSON=$(cat << EOF
> {
>     "InstanceType": "${INSTANCETYPE}",
>     "ImageId": "resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2",
>     "SecurityGroupIds": ["${EC2SECURITYGROUP_ID}"],
>     "TagSpecifications": [
>         {
>             "ResourceType": "instance",
>             "Tags": [
>                 {
>                     "Key": "Name",
>                     "Value": "h4b-instance"
>                 }
>             ]
>         }
>     ],
>     "Monitoring": {
>         "Enabled": true
>     },
>     "UserData": "`cat launch_user_data.txt | base64 -w 0`"
> }
> EOF
> ) \
> && echo ${LAUNCH_TEMPLATE_DATA_JSON}
{ "InstanceType": "t2.micro", "ImageId": "resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2", "SecurityGroupIds": ["sg-0155c9aadcfb1cee8"], "TagSpecifications": [ { "ResourceType": "instance", "Tags": [ { "Key": "Name", "Value": "h4b-instance" } ] } ], "Monitoring": { "Enabled": true }, "UserData": "IyEvYmluL2Jhc2gKc3VkbyB5dW0gdXBkYXRlIC15CnN1ZG8geXVtIGluc3RhbGwgLXkgaHR0cGQKc3VkbyBhbWF6b24tbGludXgtZXh0cmFzIGluc3RhbGwgZXBlbCAteQpzdWRvIHl1bSBpbnN0YWxsIHN0cmVzcyAteQpzdWRvIHN5c3RlbWN0bCBzdGFydCBodHRwZApzdWRvIGVjaG8gYGhvc3RuYW1lYCA+IC92YXIvd3d3L2h0bWwvaW5kZXguaHRtbApzdWRvIGVjaG8gIkNsaWVudEFsaXZlSW50ZXJ2YWwgNjAiID4+IC9ldGMvc3NoL3NzaGRfY29uZmlnCnN1ZG8gZWNobyAiQ2xpZW50QWxpdmVDb3VudE1heCAxMjAiID4+IC9ldGMvc3NoL3NzaGRfY29uZmlnCnN1ZG8gc3lzdGVtY3RsIHJlc3RhcnQgc3NoZC5zZXJ2aWNlCg==" }
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 起動テンプレートの作成
[cloudshell-user@ip-10-130-61-139 ~]$ aws ec2 create-launch-template \
>     --launch-template-name ${TEMPLATE_NAME}\
>     --launch-template-data "${LAUNCH_TEMPLATE_DATA_JSON}"
{
    "LaunchTemplate": {
        "LaunchTemplateId": "lt-0eee1100adcbf7f0d",
        "LaunchTemplateName": "h4b-template",
        "CreateTime": "2024-07-07T10:55:55+00:00",
        "CreatedBy": "arn:aws:iam::999999999999:user/admin",
        "DefaultVersionNumber": 1,
        "LatestVersionNumber": 1
    }
}
```
# 3. スケジュールスケーリング（２）～Auto Scalingグループの作成、スケジュールの設定～
## Auto Scaling グループ作成
### 変数設定
```bash:コマンド
# Auto Scalingグループ名
AUTO_SCALING_GROUP_NAME="h4b-autoscaling-group" \
&& echo ${AUTO_SCALING_GROUP_NAME}

# テンプレートバージョン
TEMPLATE_VERSION=1 \
&& echo ${TEMPLATE_VERSION}

# サブネット1
LOGICAL_RESOURCE_ID="PublicSubnet1" \
&& echo ${LOGICAL_RESOURCE_ID}

SUBNET1_ID=$(
    aws cloudformation describe-stack-resources \
        --stack-name ${STACK_NAME} \
        --logical-resource-id ${LOGICAL_RESOURCE_ID} \
        --query "StackResources[*].PhysicalResourceId" \
        --output text
) \
&& echo ${SUBNET1_ID}

# サブネット2
LOGICAL_RESOURCE_ID="PublicSubnet2" \
&& echo ${LOGICAL_RESOURCE_ID}

SUBNET2_ID=$(
    aws cloudformation describe-stack-resources \
        --stack-name ${STACK_NAME} \
        --logical-resource-id ${LOGICAL_RESOURCE_ID} \
        --query "StackResources[*].PhysicalResourceId" \
        --output text
) \
&& echo ${SUBNET2_ID}

# ロードバランサーターゲットグループ
LOGICAL_RESOURCE_ID="ALBTargetGroup" \
&& echo ${LOGICAL_RESOURCE_ID}

ALBTARGETGROUP_ID=$(
    aws cloudformation describe-stack-resources \
        --stack-name ${STACK_NAME} \
        --logical-resource-id ${LOGICAL_RESOURCE_ID} \
        --query "StackResources[*].PhysicalResourceId" \
        --output text
) \
&& echo ${ALBTARGETGROUP_ID}

# 希望する容量
DESIREDCAPACITY=1 \
&& echo ${DESIREDCAPACITY}

# 最小キャパシティ
MIN_SIZE=1 \
&& echo ${MIN_SIZE}

# 最大キャパシティ
MAX_SIZE=4 \
&& echo ${MAX_SIZE}
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ # Auto Scalingグループ名
[cloudshell-user@ip-10-130-61-139 ~]$ AUTO_SCALING_GROUP_NAME="h4b-autoscaling-group" \
> && echo ${AUTO_SCALING_GROUP_NAME}
h4b-autoscaling-group
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # テンプレートバージョン
[cloudshell-user@ip-10-130-61-139 ~]$ TEMPLATE_VERSION=1 \
> && echo ${TEMPLATE_VERSION}
1
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # サブネット1
[cloudshell-user@ip-10-130-61-139 ~]$ LOGICAL_RESOURCE_ID="PublicSubnet1" \
> && echo ${LOGICAL_RESOURCE_ID}
PublicSubnet1
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ SUBNET1_ID=$(
>     aws cloudformation describe-stack-resources \
>         --stack-name ${STACK_NAME} \
>         --logical-resource-id ${LOGICAL_RESOURCE_ID} \
>         --query "StackResources[*].PhysicalResourceId" \
>         --output text
> ) \
> && echo ${SUBNET1_ID}
subnet-08dcdae2784aa9284
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # サブネット2
[cloudshell-user@ip-10-130-61-139 ~]$ LOGICAL_RESOURCE_ID="PublicSubnet2" \
> && echo ${LOGICAL_RESOURCE_ID}
PublicSubnet2
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ SUBNET2_ID=$(
>     aws cloudformation describe-stack-resources \
>         --stack-name ${STACK_NAME} \
>         --logical-resource-id ${LOGICAL_RESOURCE_ID} \
>         --query "StackResources[*].PhysicalResourceId" \
>         --output text
> ) \
> && echo ${SUBNET2_ID}
subnet-0f3426a69c28ae825
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # ロードバランサーターゲットグループ
[cloudshell-user@ip-10-130-61-139 ~]$ LOGICAL_RESOURCE_ID="ALBTargetGroup" \
> && echo ${LOGICAL_RESOURCE_ID}
ALBTargetGroup
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ ALBTARGETGROUP_ID=$(
>     aws cloudformation describe-stack-resources \
>         --stack-name ${STACK_NAME} \
>         --logical-resource-id ${LOGICAL_RESOURCE_ID} \
>         --query "StackResources[*].PhysicalResourceId" \
>         --output text
> ) \
> && echo ${ALBTARGETGROUP_ID}
arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/h4b-tg/ae82dba2a6637734
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 希望する容量
[cloudshell-user@ip-10-130-61-139 ~]$ DESIREDCAPACITY=1 \
> && echo ${DESIREDCAPACITY}
1
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 最小キャパシティ
[cloudshell-user@ip-10-130-61-139 ~]$ MIN_SIZE=1 \
> && echo ${MIN_SIZE}
1
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 最大キャパシティ
[cloudshell-user@ip-10-130-61-139 ~]$ MAX_SIZE=4 \
> && echo ${MAX_SIZE}
4
```
## Auto Scaling グループ作成
### Auto Scaling グループ作成
```bash:コマンド
aws autoscaling create-auto-scaling-group \
    --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME} \
    --launch-template "LaunchTemplateName=${TEMPLATE_NAME},Version=${TEMPLATE_VERSION}" \
    --min-size ${MIN_SIZE} \
    --max-size ${MAX_SIZE} \
    --desired-capacity ${DESIREDCAPACITY} \
    --vpc-zone-identifier "${SUBNET1_ID},${SUBNET2_ID}" \
    --health-check-grace-period 300
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling create-auto-scaling-group \
>     --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME} \
>     --launch-template "LaunchTemplateName=${TEMPLATE_NAME},Version=${TEMPLATE_VERSION}" \
>     --min-size ${MIN_SIZE} \
>     --max-size ${MAX_SIZE} \
>     --desired-capacity ${DESIREDCAPACITY} \
>     --vpc-zone-identifier "${SUBNET1_ID},${SUBNET2_ID}" \
>     --health-check-grace-period 300
```
### アクティビティ確認
```bash:コマンド
aws autoscaling describe-scaling-activities \
    --output table
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling describe-scaling-activities \
>     --output table
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                                                                     DescribeScalingActivities                                                                                                                                    |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
||                                                                                                                                           Activities                                                                                                                                           ||
|+----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|
||  ActivityId          |  4ca64233-6738-f50d-f5fa-66e57baf7512                                                                                                                                                                                                                                   ||
||  AutoScalingGroupARN |  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group                                                                                                                       ||
||  AutoScalingGroupName|  h4b-autoscaling-group                                                                                                                                                                                                                                                  ||
||  Cause               |  At 2024-07-07T10:59:58Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1.  At 2024-07-07T11:00:00Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1.   ||
||  Description         |  Launching a new EC2 instance: i-0a6ae9cd3459acce1                                                                                                                                                                                                                      ||
||  Details             |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a"}                                                                                                                                                                                         ||
||  EndTime             |  2024-07-07T11:00:34+00:00                                                                                                                                                                                                                                              ||
||  Progress            |  100                                                                                                                                                                                                                                                                    ||
||  StartTime           |  2024-07-07T11:00:02.749000+00:00                                                                                                                                                                                                                                       ||
||  StatusCode          |  Successful                                                                                                                                                                                                                                                             ||
|+----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|

```
## 予定されたアクションを作成する
### 変数設定
```bash:コマンド
# 名前
SCHEDULED_ACTION_NAME="h4b-schedule" \
&& echo ${SCHEDULED_ACTION_NAME}

# 希望する容量
SCHEDULED_DESIREDCAPACITY=2 \
&& echo ${SCHEDULED_DESIREDCAPACITY}

# 最小キャパシティ
SCHEDULED_MIN_SIZE=2 \
&& echo ${SCHEDULED_MIN_SIZE}

# 最大キャパシティ
SCHEDULED_MAX_SIZE=4 \
&& echo ${SCHEDULED_MAX_SIZE}

# 開始時間
START_TIME="2024-07-07T20:10:00+09:00" \
&& echo ${START_TIME}

# タイムゾーン
TIME_ZONE="Japan" \
&& echo ${TIME_ZONE}

```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ # 名前
[cloudshell-user@ip-10-130-61-139 ~]$ SCHEDULED_ACTION_NAME="h4b-schedule" \
> && echo ${SCHEDULED_ACTION_NAME}
h4b-schedule
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 希望する容量
[cloudshell-user@ip-10-130-61-139 ~]$ SCHEDULED_DESIREDCAPACITY=2 \
> && echo ${SCHEDULED_DESIREDCAPACITY}
2
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 最小キャパシティ
[cloudshell-user@ip-10-130-61-139 ~]$ SCHEDULED_MIN_SIZE=2 \
> && echo ${SCHEDULED_MIN_SIZE}
2
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 最大キャパシティ
[cloudshell-user@ip-10-130-61-139 ~]$ SCHEDULED_MAX_SIZE=4 \
> && echo ${SCHEDULED_MAX_SIZE}
4
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # 開始時間
[cloudshell-user@ip-10-130-61-139 ~]$ START_TIME="2024-07-07T20:10:00+09:00" \
> && echo ${START_TIME}
2024-07-07T20:10:00+09:00
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # タイムゾーン
[cloudshell-user@ip-10-130-61-139 ~]$ TIME_ZONE="Japan" \
> && echo ${TIME_ZONE}
Japan
```
### 予定されたアクションを作成
```bash:コマンド
aws autoscaling put-scheduled-update-group-action \
    --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME} \
    --scheduled-action-name ${SCHEDULED_ACTION_NAME} \
    --start-time ${START_TIME} \
    --min-size ${SCHEDULED_MIN_SIZE} \
    --max-size ${SCHEDULED_MAX_SIZE} \
    --desired-capacity ${SCHEDULED_DESIREDCAPACITY} \
    --time-zone ${TIME_ZONE}
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling put-scheduled-update-group-action \
>     --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME} \
>     --scheduled-action-name ${SCHEDULED_ACTION_NAME} \
>     --start-time ${START_TIME} \
>     --min-size ${SCHEDULED_MIN_SIZE} \
>     --max-size ${SCHEDULED_MAX_SIZE} \
>     --desired-capacity ${SCHEDULED_DESIREDCAPACITY} \
>     --time-zone ${TIME_ZONE}
```
### アクティビティ確認
```bash:コマンド
aws autoscaling describe-scaling-activities \
    --output table
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling describe-scaling-activities \
>     --output table
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                                                                                                                                                                                                                                                                                                                                                                               DescribeScalingActivities                                                                                                                                                                                                                                                                                                                                                                                                                                               |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
||                                                                                                                                                                                                                                                                                                                                                                                                                                                     Activities                                                                                                                                                                                                                                                                                                                                                                                                                                                      ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+---------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
||              ActivityId              |                                                                AutoScalingGroupARN                                                                | AutoScalingGroupName   |                                                                                                                                                                                                                            Cause                                                                                                                                                                                                                             |                    Description                     |                                     Details                                     |          EndTime           | Progress  |             StartTime             | StatusCode   ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+---------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
||  44664233-8cd5-7b10-afe7-7677a28fab2f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:10:05Z the scheduled action h4b-schedule executed. Setting min size from 1 to 2. Setting desired capacity from 1 to 2.  At 2024-07-07T11:10:05Z a scheduled action update of AutoScalingGroup constraints to min: 2, max: 4, desired: 2 changing the desired capacity from 1 to 2.  At 2024-07-07T11:10:16Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 1 to 2.  |  Launching a new EC2 instance: i-0d320d40f79d1d661 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c"} |  2024-07-07T11:10:50+00:00 |  100      |  2024-07-07T11:10:18.974000+00:00 |  Successful  ||
||  75c64233-8bfe-a323-57f3-7d7c5cf0282f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |                                                                                                                                                                                                                                                                                                                                                                                                                                                              |  Executing scheduled action h4b-schedule           |  {}                                                                             |  2024-07-07T11:10:05+00:00 |  100      |  2024-07-07T11:10:05.224000+00:00 |  Successful  ||
||  4ca64233-6738-f50d-f5fa-66e57baf7512|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T10:59:58Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1.  At 2024-07-07T11:00:00Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1.                                                                                                                                                                                        |  Launching a new EC2 instance: i-0a6ae9cd3459acce1 |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a"} |  2024-07-07T11:00:34+00:00 |  100      |  2024-07-07T11:00:02.749000+00:00 |  Successful  ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+---------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
```
# 4. ターゲット追跡スケーリング（１）～ターゲット追跡スケーリングの設定～
## 動的スケーリングポリシーを作成
### 変数
```bash:コマンド
# スケーリングポリシー名
SCALING_POLICY_NAME="Target Tracking Policy" \
&& echo ${SCALING_POLICY_NAME}

# ポリシータイプ
SCALING_POLICY_TYPE="TargetTrackingScaling" \
&& echo ${SCALING_POLICY_TYPE}

# メトリクスタイプ
PREDEFINEDMETRICTYPE="ASGAverageCPUUtilization" \
&& echo ${PREDEFINEDMETRICTYPE}

# ターゲット値
TARGETVALUE=80.0 \
&& echo ${TARGETVALUE}

# スケーリングポリシー
SCALING_POLICY_JSON=$(cat <<EOF
{
    "PredefinedMetricSpecification": {
        "PredefinedMetricType": "${PREDEFINEDMETRICTYPE}"
    },
    "TargetValue": ${TARGETVALUE}
}
EOF
) \
&& echo ${SCALING_POLICY_JSON}
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ # スケーリングポリシー名
[cloudshell-user@ip-10-130-61-139 ~]$ SCALING_POLICY_NAME="Target Tracking Policy" \
> && echo ${SCALING_POLICY_NAME}
Target Tracking Policy
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # ポリシータイプ
[cloudshell-user@ip-10-130-61-139 ~]$ SCALING_POLICY_TYPE="TargetTrackingScaling" \
> && echo ${SCALING_POLICY_TYPE}
TargetTrackingScaling
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # メトリクスタイプ
[cloudshell-user@ip-10-130-61-139 ~]$ PREDEFINEDMETRICTYPE="ASGAverageCPUUtilization" \
> && echo ${PREDEFINEDMETRICTYPE}
ASGAverageCPUUtilization
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # ターゲット値
[cloudshell-user@ip-10-130-61-139 ~]$ TARGETVALUE=80.0 \
> && echo ${TARGETVALUE}
80.0
[cloudshell-user@ip-10-130-61-139 ~]$ 
[cloudshell-user@ip-10-130-61-139 ~]$ # スケーリングポリシー
[cloudshell-user@ip-10-130-61-139 ~]$ SCALING_POLICY_JSON=$(cat <<EOF
> {
>     "PredefinedMetricSpecification": {
>         "PredefinedMetricType": "${PREDEFINEDMETRICTYPE}"
>     },
>     "TargetValue": ${TARGETVALUE}
> }
> EOF
> ) \
> && echo ${SCALING_POLICY_JSON}
{ "PredefinedMetricSpecification": { "PredefinedMetricType": "ASGAverageCPUUtilization" }, "TargetValue": 80.0 }
```
### 動的スケーリングポリシーを作成
```bash:コマンド
aws autoscaling put-scaling-policy \
    --auto-scaling-group-name "${AUTO_SCALING_GROUP_NAME}" \
    --policy-name "${SCALING_POLICY_NAME}" \
    --policy-type "${SCALING_POLICY_TYPE}" \
    --target-tracking-configuration "${SCALING_POLICY_JSON}"
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling put-scaling-policy \
>     --auto-scaling-group-name "${AUTO_SCALING_GROUP_NAME}" \
>     --policy-name "${SCALING_POLICY_NAME}" \
>     --policy-type "${SCALING_POLICY_TYPE}" \
>     --target-tracking-configuration "${SCALING_POLICY_JSON}"
{
    "PolicyARN": "arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy",
    "Alarms": [
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6",
            "AlarmARN": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6"
        },
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmLow-e613add2-6f7a-4410-aaa1-e47be0f4e1c4",
            "AlarmARN": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-e613add2-6f7a-4410-aaa1-e47be0f4e1c4"
        }
    ]
}
```
### アラームを確認
```bash:コマンド
aws cloudwatch describe-alarms
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws cloudwatch describe-alarms
{
    "MetricAlarms": [
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6",
            "AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6",
            "AlarmDescription": "DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.",
            "AlarmConfigurationUpdatedTimestamp": "2024-07-07T11:17:41.379000+00:00",
            "ActionsEnabled": true,
            "OKActions": [],
            "AlarmActions": [
                "arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy"
            ],
            "InsufficientDataActions": [],
            "StateValue": "OK",
            "StateReason": "Threshold Crossed: 3 datapoints [0.3279569892473105 (07/07/24 11:16:00), 0.16671297582662697 (07/07/24 11:15:00), 0.2528248587570475 (07/07/24 11:14:00)] were not greater than the threshold (80.0).",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2024-07-07T11:18:27.727+0000\",\"startDate\":\"2024-07-07T11:14:00.000+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.2528248587570475,0.16671297582662697,0.3279569892473105],\"threshold\":80.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2024-07-07T11:16:00.000+0000\",\"sampleCount\":2.0,\"value\":0.3279569892473105}]}",
            "StateUpdatedTimestamp": "2024-07-07T11:18:27.730000+00:00",
            "MetricName": "CPUUtilization",
            "Namespace": "AWS/EC2",
            "Statistic": "Average",
            "Dimensions": [
                {
                    "Name": "AutoScalingGroupName",
                    "Value": "h4b-autoscaling-group"
                }
            ],
            "Period": 60,
            "EvaluationPeriods": 3,
            "Threshold": 80.0,
            "ComparisonOperator": "GreaterThanThreshold",
            "StateTransitionedTimestamp": "2024-07-07T11:18:27.730000+00:00"
        },
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmLow-51ad2255-1198-44ef-8358-c1bc1cc5af51",
            "AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-51ad2255-1198-44ef-8358-c1bc1cc5af51",
            "AlarmDescription": "DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.",
            "AlarmConfigurationUpdatedTimestamp": "2024-07-07T11:19:07.735000+00:00",
            "ActionsEnabled": true,
            "OKActions": [],
            "AlarmActions": [
                "arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy"
            ],
            "InsufficientDataActions": [],
            "StateValue": "ALARM",
            "StateReason": "Threshold Crossed: 15 datapoints were less than the threshold (56.0). The most recent datapoints which crossed the threshold: [0.170952659263591 (07/07/24 11:17:00), 0.3279569892473105 (07/07/24 11:16:00), 0.16671297582662697 (07/07/24 11:15:00), 0.2528248587570475 (07/07/24 11:14:00), 0.2459016393442715 (07/07/24 11:13:00)].",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2024-07-07T11:19:15.242+0000\",\"startDate\":\"2024-07-07T11:03:00.000+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.166666666666657,0.344827586206901,0.161290322580647,0.333333333333326,0.169491525423731,0.322580645161283,0.333333333333338,0.169491525423743,34.75000000000001,0.2514124293785345,0.2459016393442715,0.2528248587570475,0.16671297582662697,0.3279569892473105,0.170952659263591],\"threshold\":56.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2024-07-07T11:17:00.000+0000\",\"sampleCount\":2.0,\"value\":0.170952659263591},{\"timestamp\":\"2024-07-07T11:16:00.000+0000\",\"sampleCount\":2.0,\"value\":0.3279569892473105},{\"timestamp\":\"2024-07-07T11:15:00.000+0000\",\"sampleCount\":2.0,\"value\":0.16671297582662697},{\"timestamp\":\"2024-07-07T11:14:00.000+0000\",\"sampleCount\":2.0,\"value\":0.2528248587570475},{\"timestamp\":\"2024-07-07T11:13:00.000+0000\",\"sampleCount\":2.0,\"value\":0.2459016393442715},{\"timestamp\":\"2024-07-07T11:12:00.000+0000\",\"sampleCount\":2.0,\"value\":0.2514124293785345},{\"timestamp\":\"2024-07-07T11:11:00.000+0000\",\"sampleCount\":2.0,\"value\":34.75000000000001},{\"timestamp\":\"2024-07-07T11:10:00.000+0000\",\"sampleCount\":1.0,\"value\":0.169491525423743},{\"timestamp\":\"2024-07-07T11:09:00.000+0000\",\"sampleCount\":1.0,\"value\":0.333333333333338},{\"timestamp\":\"2024-07-07T11:08:00.000+0000\",\"sampleCount\":1.0,\"value\":0.322580645161283},{\"timestamp\":\"2024-07-07T11:07:00.000+0000\",\"sampleCount\":1.0,\"value\":0.169491525423731},{\"timestamp\":\"2024-07-07T11:06:00.000+0000\",\"sampleCount\":1.0,\"value\":0.333333333333326},{\"timestamp\":\"2024-07-07T11:05:00.000+0000\",\"sampleCount\":1.0,\"value\":0.161290322580647},{\"timestamp\":\"2024-07-07T11:04:00.000+0000\",\"sampleCount\":1.0,\"value\":0.344827586206901},{\"timestamp\":\"2024-07-07T11:03:00.000+0000\",\"sampleCount\":1.0,\"value\":0.166666666666657}]}",
            "StateUpdatedTimestamp": "2024-07-07T11:19:15.238000+00:00",
            "MetricName": "CPUUtilization",
            "Namespace": "AWS/EC2",
            "Statistic": "Average",
            "Dimensions": [
                {
                    "Name": "AutoScalingGroupName",
                    "Value": "h4b-autoscaling-group"
                }
            ],
            "Period": 60,
            "EvaluationPeriods": 15,
            "Threshold": 56.0,
            "ComparisonOperator": "LessThanThreshold",
            "StateTransitionedTimestamp": "2024-07-07T11:19:15.238000+00:00"
        }
    ],
    "CompositeAlarms": []
}
```

# 5. ターゲット追跡スケーリング（２）～負荷をかけてスケールアウトを確認～
## 負荷実施
```bash:コマンド (EC2 1台目)
stress -c 1
```
```bash:出力
[ec2-user@ip-10-0-0-251 ~]$ stress -c 1
stress: info: [3868] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
```
```bash:コマンド (EC2 2台目)
stress -c 1
```
```bash:出力
[ec2-user@ip-10-0-1-99 ~]$ stress -c 1
stress: info: [3850] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
```
## アラーム確認
```bash:コマンド
aws cloudwatch describe-alarms
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws cloudwatch describe-alarms
{
    "MetricAlarms": [
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6",
            "AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6",
            "AlarmDescription": "DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.",
            "AlarmConfigurationUpdatedTimestamp": "2024-07-07T11:17:41.379000+00:00",
            "ActionsEnabled": true,
            "OKActions": [],
            "AlarmActions": [
                "arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy"
            ],
            "InsufficientDataActions": [],
            "StateValue": "ALARM",
            "StateReason": "Threshold Crossed: 3 datapoints [99.42486338797809 (07/07/24 11:28:00), 100.0 (07/07/24 11:27:00), 99.0833333333333 (07/07/24 11:26:00)] were greater than the threshold (80.0).",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2024-07-07T11:30:27.720+0000\",\"startDate\":\"2024-07-07T11:26:00.000+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[99.0833333333333,100.0,99.42486338797809],\"threshold\":80.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2024-07-07T11:28:00.000+0000\",\"sampleCount\":2.0,\"value\":99.42486338797809},{\"timestamp\":\"2024-07-07T11:27:00.000+0000\",\"sampleCount\":2.0,\"value\":100.0},{\"timestamp\":\"2024-07-07T11:26:00.000+0000\",\"sampleCount\":2.0,\"value\":99.0833333333333}]}",
            "StateUpdatedTimestamp": "2024-07-07T11:30:27.722000+00:00",
            "MetricName": "CPUUtilization",
            "Namespace": "AWS/EC2",
            "Statistic": "Average",
            "Dimensions": [
                {
                    "Name": "AutoScalingGroupName",
                    "Value": "h4b-autoscaling-group"
                }
            ],
            "Period": 60,
            "EvaluationPeriods": 3,
            "Threshold": 80.0,
            "ComparisonOperator": "GreaterThanThreshold",
            "StateTransitionedTimestamp": "2024-07-07T11:30:27.722000+00:00"
        },
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmLow-51ad2255-1198-44ef-8358-c1bc1cc5af51",
            "AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-51ad2255-1198-44ef-8358-c1bc1cc5af51",
            "AlarmDescription": "DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.",
            "AlarmConfigurationUpdatedTimestamp": "2024-07-07T11:19:07.735000+00:00",
            "ActionsEnabled": true,
            "OKActions": [],
            "AlarmActions": [
                "arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy"
            ],
            "InsufficientDataActions": [],
            "StateValue": "OK",
            "StateReason": "Threshold Crossed: 1 datapoint [99.0833333333333 (07/07/24 11:26:00)] was not less than the threshold (56.0).",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2024-07-07T11:28:15.241+0000\",\"startDate\":\"2024-07-07T11:12:00.000+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.2514124293785345,0.2459016393442715,0.2528248587570475,0.16671297582662697,0.3279569892473105,0.170952659263591,0.327868852459021,0.25740580960598003,1.667129758266185,0.24590163934426,0.2514124293785285,0.24726775956283897,0.2528248587570595,16.759887005649702,99.0833333333333],\"threshold\":56.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2024-07-07T11:26:00.000+0000\",\"sampleCount\":2.0,\"value\":99.0833333333333}]}",
            "StateUpdatedTimestamp": "2024-07-07T11:28:15.243000+00:00",
            "MetricName": "CPUUtilization",
            "Namespace": "AWS/EC2",
            "Statistic": "Average",
            "Dimensions": [
                {
                    "Name": "AutoScalingGroupName",
                    "Value": "h4b-autoscaling-group"
                }
            ],
            "Period": 60,
            "EvaluationPeriods": 15,
            "Threshold": 56.0,
            "ComparisonOperator": "LessThanThreshold",
            "StateTransitionedTimestamp": "2024-07-07T11:28:15.243000+00:00"
        }
    ],
    "CompositeAlarms": []
}
```

### アクティビティ確認
```bash:コマンド
aws autoscaling describe-scaling-activities \
    --output table
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling describe-scaling-activities \
>     --output table
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        DescribeScalingActivities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
||                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Activities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+----------------------------+|
||              ActivityId              |                                                                AutoScalingGroupARN                                                                | AutoScalingGroupName   |                                                                                                                                                                                                                            Cause                                                                                                                                                                                                                            |                    Description                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Details                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |          EndTime           | Progress  |             StartTime             |        StatusCode          ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+----------------------------+|
||  a5464233-ed5b-43b4-44ae-ede2b49a874c|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:36:27Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 3 to 4.  At 2024-07-07T11:36:38Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 3 to 4.                                                                       |  Launching a new EC2 instance: i-03c0d6d6195fac8ec |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"GreaterThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":3,"Unit":null,"Namespace":"AWS/EC2","Threshold":80},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"ALARM","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720351061379,"StateChangeTime":1720351827722}]}  |                            |  90       |  2024-07-07T11:36:40.400000+00:00 |  WaitingForInstanceWarmup  ||
||  a1064233-d72b-f3ed-4260-df3c5a026e59|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:30:27Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 2 to 3.  At 2024-07-07T11:30:35Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 2 to 3.                                                                       |  Launching a new EC2 instance: i-047e958c8728771c2 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"GreaterThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":3,"Unit":null,"Namespace":"AWS/EC2","Threshold":80},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"OK","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720351061379,"StateChangeTime":1720351827722}]}     |  2024-07-07T11:36:08+00:00 |  100      |  2024-07-07T11:30:36.924000+00:00 |  Successful                ||
||  44664233-8cd5-7b10-afe7-7677a28fab2f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:10:05Z the scheduled action h4b-schedule executed. Setting min size from 1 to 2. Setting desired capacity from 1 to 2.  At 2024-07-07T11:10:05Z a scheduled action update of AutoScalingGroup constraints to min: 2, max: 4, desired: 2 changing the desired capacity from 1 to 2.  At 2024-07-07T11:10:16Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 1 to 2. |  Launching a new EC2 instance: i-0d320d40f79d1d661 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |  2024-07-07T11:10:50+00:00 |  100      |  2024-07-07T11:10:18.974000+00:00 |  Successful                ||
||  75c64233-8bfe-a323-57f3-7d7c5cf0282f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |                                                                                                                                                                                                                                                                                                                                                                                                                                                             |  Executing scheduled action h4b-schedule           |  {}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |  2024-07-07T11:10:05+00:00 |  100      |  2024-07-07T11:10:05.224000+00:00 |  Successful                ||
||  4ca64233-6738-f50d-f5fa-66e57baf7512|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T10:59:58Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1.  At 2024-07-07T11:00:00Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1.                                                                                                                                                                                       |  Launching a new EC2 instance: i-0a6ae9cd3459acce1 |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |  2024-07-07T11:00:34+00:00 |  100      |  2024-07-07T11:00:02.749000+00:00 |  Successful                ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+----------------------------+|
```

# 6. ターゲット追跡スケーリング（３）～負荷をとめてスケールインを確認～
```bash:出力 (EC2 1台目)
[ec2-user@ip-10-0-0-251 ~]$ stress -c 1
stress: info: [3868] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
^C
```
```bash:出力 (EC2 2台目)
[ec2-user@ip-10-0-1-99 ~]$ stress -c 1
stress: info: [3850] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
^C
```
### アラーム確認
```bash:コマンド
aws cloudwatch describe-alarms
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws cloudwatch describe-alarms
{
    "MetricAlarms": [
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6",
            "AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6",
            "AlarmDescription": "DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.",
            "AlarmConfigurationUpdatedTimestamp": "2024-07-07T11:17:41.379000+00:00",
            "ActionsEnabled": true,
            "OKActions": [],
            "AlarmActions": [
                "arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy"
            ],
            "InsufficientDataActions": [],
            "StateValue": "OK",
            "StateReason": "Threshold Crossed: 1 datapoint [66.61299435028253 (07/07/24 11:40:00)] was not greater than the threshold (80.0).",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2024-07-07T11:42:27.721+0000\",\"startDate\":\"2024-07-07T11:38:00.000+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[99.672131147541,99.25,66.61299435028253],\"threshold\":80.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2024-07-07T11:40:00.000+0000\",\"sampleCount\":3.0,\"value\":66.61299435028253}]}",
            "StateUpdatedTimestamp": "2024-07-07T11:42:27.724000+00:00",
            "MetricName": "CPUUtilization",
            "Namespace": "AWS/EC2",
            "Statistic": "Average",
            "Dimensions": [
                {
                    "Name": "AutoScalingGroupName",
                    "Value": "h4b-autoscaling-group"
                }
            ],
            "Period": 60,
            "EvaluationPeriods": 3,
            "Threshold": 80.0,
            "ComparisonOperator": "GreaterThanThreshold",
            "StateTransitionedTimestamp": "2024-07-07T11:42:27.724000+00:00"
        },
        {
            "AlarmName": "TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16",
            "AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16",
            "AlarmDescription": "DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.",
            "AlarmConfigurationUpdatedTimestamp": "2024-07-07T11:58:11.971000+00:00",
            "ActionsEnabled": true,
            "OKActions": [],
            "AlarmActions": [
                "arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy"
            ],
            "InsufficientDataActions": [],
            "StateValue": "ALARM",
            "StateReason": "Threshold Crossed: 15 datapoints were less than the threshold (56.0). The most recent datapoints which crossed the threshold: [0.3315735852552157 (07/07/24 11:57:00), 0.2083333333333185 (07/07/24 11:56:00), 0.16807909604521126 (07/07/24 11:55:00), 0.2882954345143112 (07/07/24 11:54:00), 0.208356487913326 (07/07/24 11:53:00)].",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2024-07-07T11:58:52.191+0000\",\"startDate\":\"2024-07-07T11:43:00.000+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.22131147540980764,0.22316384180801663,0.2486570343612865,0.1666898212467545,0.7090627026025165,4.626436781609228,0.3348407763331857,0.20637453055945149,0.20705759066886775,0.25143558395854926,0.208356487913326,0.2882954345143112,0.16807909604521126,0.2083333333333185,0.3315735852552157],\"threshold\":56.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2024-07-07T11:57:00.000+0000\",\"sampleCount\":3.0,\"value\":0.3315735852552157},{\"timestamp\":\"2024-07-07T11:56:00.000+0000\",\"sampleCount\":4.0,\"value\":0.2083333333333185},{\"timestamp\":\"2024-07-07T11:55:00.000+0000\",\"sampleCount\":4.0,\"value\":0.16807909604521126},{\"timestamp\":\"2024-07-07T11:54:00.000+0000\",\"sampleCount\":4.0,\"value\":0.2882954345143112},{\"timestamp\":\"2024-07-07T11:53:00.000+0000\",\"sampleCount\":4.0,\"value\":0.208356487913326},{\"timestamp\":\"2024-07-07T11:52:00.000+0000\",\"sampleCount\":4.0,\"value\":0.25143558395854926},{\"timestamp\":\"2024-07-07T11:51:00.000+0000\",\"sampleCount\":4.0,\"value\":0.20705759066886775},{\"timestamp\":\"2024-07-07T11:50:00.000+0000\",\"sampleCount\":4.0,\"value\":0.20637453055945149},{\"timestamp\":\"2024-07-07T11:49:00.000+0000\",\"sampleCount\":4.0,\"value\":0.3348407763331857},{\"timestamp\":\"2024-07-07T11:48:00.000+0000\",\"sampleCount\":4.0,\"value\":4.626436781609228},{\"timestamp\":\"2024-07-07T11:47:00.000+0000\",\"sampleCount\":4.0,\"value\":0.7090627026025165},{\"timestamp\":\"2024-07-07T11:46:00.000+0000\",\"sampleCount\":4.0,\"value\":0.1666898212467545},{\"timestamp\":\"2024-07-07T11:45:00.000+0000\",\"sampleCount\":4.0,\"value\":0.2486570343612865},{\"timestamp\":\"2024-07-07T11:44:00.000+0000\",\"sampleCount\":3.0,\"value\":0.22316384180801663},{\"timestamp\":\"2024-07-07T11:43:00.000+0000\",\"sampleCount\":3.0,\"value\":0.22131147540980764}]}",
            "StateUpdatedTimestamp": "2024-07-07T11:58:52.193000+00:00",
            "MetricName": "CPUUtilization",
            "Namespace": "AWS/EC2",
            "Statistic": "Average",
            "Dimensions": [
                {
                    "Name": "AutoScalingGroupName",
                    "Value": "h4b-autoscaling-group"
                }
            ],
            "Period": 60,
            "EvaluationPeriods": 15,
            "Threshold": 56.0,
            "ComparisonOperator": "LessThanThreshold",
            "StateTransitionedTimestamp": "2024-07-07T11:58:52.193000+00:00"
        }
    ],
    "CompositeAlarms": []
}
```
### アクティビティ確認
```bash:コマンド
aws autoscaling describe-scaling-activities \
    --output table
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling describe-scaling-activities \
>     --output table
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 DescribeScalingActivities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
||                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Activities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
||              ActivityId              |                                                                AutoScalingGroupARN                                                                | AutoScalingGroupName   |                                                                                                                                                                                                                                        Cause                                                                                                                                                                                                                                         |                    Description                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           Details                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |          EndTime           | Progress  |             StartTime             | StatusCode   ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
||  db865bfc-552e-4925-af6a-a7a9d64366b7|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:58:52Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 3 to 2.  At 2024-07-07T11:59:01Z an instance was taken out of service in response to a difference between desired and actual capacity, shrinking the capacity from 3 to 2.  At 2024-07-07T11:59:01Z instance i-0d320d40f79d1d661 was selected for termination. |  Terminating EC2 instance: i-0d320d40f79d1d661     |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"LessThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":15,"Unit":null,"Namespace":"AWS/EC2","Threshold":56},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"INSUFFICIENT_DATA","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720353491971,"StateChangeTime":1720353532193}]}  |  2024-07-07T11:59:43+00:00 |  100      |  2024-07-07T11:59:01.562000+00:00 |  Successful  ||
||  a1a6c473-fa33-4821-ab24-27e648ced4c4|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:58:11Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmLow-2f2f99f8-1a6d-4361-b759-ad502b4c9a7e in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 4 to 3.  At 2024-07-07T11:58:16Z an instance was taken out of service in response to a difference between desired and actual capacity, shrinking the capacity from 4 to 3.  At 2024-07-07T11:58:16Z instance i-0a6ae9cd3459acce1 was selected for termination. |  Terminating EC2 instance: i-0a6ae9cd3459acce1     |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-2f2f99f8-1a6d-4361-b759-ad502b4c9a7e","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"LessThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":15,"Unit":null,"Namespace":"AWS/EC2","Threshold":60},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmLow-2f2f99f8-1a6d-4361-b759-ad502b4c9a7e","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"OK","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720352187862,"StateChangeTime":1720353491536}]}                 |  2024-07-07T11:58:58+00:00 |  100      |  2024-07-07T11:58:16.868000+00:00 |  Successful  ||
||  a5464233-ed5b-43b4-44ae-ede2b49a874c|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:36:27Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 3 to 4.  At 2024-07-07T11:36:38Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 3 to 4.                                                                                                |  Launching a new EC2 instance: i-03c0d6d6195fac8ec |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"GreaterThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":3,"Unit":null,"Namespace":"AWS/EC2","Threshold":80},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"ALARM","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720351061379,"StateChangeTime":1720351827722}]}          |  2024-07-07T11:42:11+00:00 |  100      |  2024-07-07T11:36:40.400000+00:00 |  Successful  ||
||  a1064233-d72b-f3ed-4260-df3c5a026e59|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:30:27Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 2 to 3.  At 2024-07-07T11:30:35Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 2 to 3.                                                                                                |  Launching a new EC2 instance: i-047e958c8728771c2 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"GreaterThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":3,"Unit":null,"Namespace":"AWS/EC2","Threshold":80},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"OK","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720351061379,"StateChangeTime":1720351827722}]}             |  2024-07-07T11:36:08+00:00 |  100      |  2024-07-07T11:30:36.924000+00:00 |  Successful  ||
||  44664233-8cd5-7b10-afe7-7677a28fab2f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:10:05Z the scheduled action h4b-schedule executed. Setting min size from 1 to 2. Setting desired capacity from 1 to 2.  At 2024-07-07T11:10:05Z a scheduled action update of AutoScalingGroup constraints to min: 2, max: 4, desired: 2 changing the desired capacity from 1 to 2.  At 2024-07-07T11:10:16Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 1 to 2.                          |  Launching a new EC2 instance: i-0d320d40f79d1d661 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |  2024-07-07T11:10:50+00:00 |  100      |  2024-07-07T11:10:18.974000+00:00 |  Successful  ||
||  75c64233-8bfe-a323-57f3-7d7c5cf0282f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |  Executing scheduled action h4b-schedule           |  {}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |  2024-07-07T11:10:05+00:00 |  100      |  2024-07-07T11:10:05.224000+00:00 |  Successful  ||
||  4ca64233-6738-f50d-f5fa-66e57baf7512|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T10:59:58Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1.  At 2024-07-07T11:00:00Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1.                                                                                                                                                                                                                |  Launching a new EC2 instance: i-0a6ae9cd3459acce1 |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |  2024-07-07T11:00:34+00:00 |  100      |  2024-07-07T11:00:02.749000+00:00 |  Successful  ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
```
# 7. 異常なインスタンスの置き換え
## インスタンス停止
```bash:コマンド
aws ec2 stop-instances --instance-ids i-047e958c8728771c2
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws ec2 stop-instances --instance-ids i-047e958c8728771c2
{
    "StoppingInstances": [
        {
            "CurrentState": {
                "Code": 64,
                "Name": "stopping"
            },
            "InstanceId": "i-047e958c8728771c2",
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}
```
### アクティビティ確認
```bash:コマンド
aws autoscaling describe-scaling-activities \
    --output table
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling describe-scaling-activities \
>     --output table
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 DescribeScalingActivities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
||                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Activities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
||              ActivityId              |                                                                AutoScalingGroupARN                                                                | AutoScalingGroupName   |                                                                                                                                                                                                                                        Cause                                                                                                                                                                                                                                         |                    Description                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           Details                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |          EndTime           | Progress  |             StartTime             | StatusCode   ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
||  f7064234-7d1a-1ea4-4532-650a790fa562|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T12:15:53Z an instance was launched in response to an unhealthy instance needing to be replaced.                                                                                                                                                                                                                                                                                                                                                                       |  Launching a new EC2 instance: i-099a851196e3367a2 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |  2024-07-07T12:16:58+00:00 |  100      |  2024-07-07T12:15:55.527000+00:00 |  Successful  ||
||  cdc64234-7cf8-e966-acea-8a0f9bbd7101|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T12:15:53Z an instance was taken out of service in response to an EC2 health check indicating it has been terminated or stopped.                                                                                                                                                                                                                                                                                                                                       |  Terminating EC2 instance: i-047e958c8728771c2     |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |  2024-07-07T12:16:15+00:00 |  100      |  2024-07-07T12:15:53.402000+00:00 |  Successful  ||
||  db865bfc-552e-4925-af6a-a7a9d64366b7|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:58:52Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 3 to 2.  At 2024-07-07T11:59:01Z an instance was taken out of service in response to a difference between desired and actual capacity, shrinking the capacity from 3 to 2.  At 2024-07-07T11:59:01Z instance i-0d320d40f79d1d661 was selected for termination. |  Terminating EC2 instance: i-0d320d40f79d1d661     |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"LessThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":15,"Unit":null,"Namespace":"AWS/EC2","Threshold":56},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmLow-6e9d4da8-711f-4d02-b553-622bff3a9c16","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"INSUFFICIENT_DATA","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720353491971,"StateChangeTime":1720353532193}]}  |  2024-07-07T11:59:43+00:00 |  100      |  2024-07-07T11:59:01.562000+00:00 |  Successful  ||
||  a1a6c473-fa33-4821-ab24-27e648ced4c4|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:58:11Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmLow-2f2f99f8-1a6d-4361-b759-ad502b4c9a7e in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 4 to 3.  At 2024-07-07T11:58:16Z an instance was taken out of service in response to a difference between desired and actual capacity, shrinking the capacity from 4 to 3.  At 2024-07-07T11:58:16Z instance i-0a6ae9cd3459acce1 was selected for termination. |  Terminating EC2 instance: i-0a6ae9cd3459acce1     |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmLow-2f2f99f8-1a6d-4361-b759-ad502b4c9a7e","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"LessThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":15,"Unit":null,"Namespace":"AWS/EC2","Threshold":60},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmLow-2f2f99f8-1a6d-4361-b759-ad502b4c9a7e","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"OK","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720352187862,"StateChangeTime":1720353491536}]}                 |  2024-07-07T11:58:58+00:00 |  100      |  2024-07-07T11:58:16.868000+00:00 |  Successful  ||
||  a5464233-ed5b-43b4-44ae-ede2b49a874c|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:36:27Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 3 to 4.  At 2024-07-07T11:36:38Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 3 to 4.                                                                                                |  Launching a new EC2 instance: i-03c0d6d6195fac8ec |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"GreaterThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":3,"Unit":null,"Namespace":"AWS/EC2","Threshold":80},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"ALARM","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720351061379,"StateChangeTime":1720351827722}]}          |  2024-07-07T11:42:11+00:00 |  100      |  2024-07-07T11:36:40.400000+00:00 |  Successful  ||
||  a1064233-d72b-f3ed-4260-df3c5a026e59|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:30:27Z a monitor alarm TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6 in state ALARM triggered policy Target Tracking Policy changing the desired capacity from 2 to 3.  At 2024-07-07T11:30:35Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 2 to 3.                                                                                                |  Launching a new EC2 instance: i-047e958c8728771c2 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c","InvokingAlarms":[{"AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:999999999999:alarm:TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","Trigger":{"MetricName":"CPUUtilization","EvaluateLowSampleCountPercentile":"","ComparisonOperator":"GreaterThanThreshold","TreatMissingData":"","Statistic":"AVERAGE","StatisticType":"Statistic","Period":60,"EvaluationPeriods":3,"Unit":null,"Namespace":"AWS/EC2","Threshold":80},"AlarmName":"TargetTracking-h4b-autoscaling-group-AlarmHigh-54e9563c-780b-46e5-8b28-5d16b35e74e6","AlarmDescription":"DO NOT EDIT OR DELETE. For TargetTrackingScaling policy arn:aws:autoscaling:ap-northeast-1:999999999999:scalingPolicy:8b5e614e-acbe-4e3e-b13c-766c6fd60bf8:autoScalingGroupName/h4b-autoscaling-group:policyName/Target Tracking Policy.","AWSAccountId":"999999999999","OldStateValue":"OK","Region":"Asia Pacific (Tokyo)","NewStateValue":"ALARM","AlarmConfigurationUpdatedTimestamp":1720351061379,"StateChangeTime":1720351827722}]}             |  2024-07-07T11:36:08+00:00 |  100      |  2024-07-07T11:30:36.924000+00:00 |  Successful  ||
||  44664233-8cd5-7b10-afe7-7677a28fab2f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T11:10:05Z the scheduled action h4b-schedule executed. Setting min size from 1 to 2. Setting desired capacity from 1 to 2.  At 2024-07-07T11:10:05Z a scheduled action update of AutoScalingGroup constraints to min: 2, max: 4, desired: 2 changing the desired capacity from 1 to 2.  At 2024-07-07T11:10:16Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 1 to 2.                          |  Launching a new EC2 instance: i-0d320d40f79d1d661 |  {"Subnet ID":"subnet-0f3426a69c28ae825","Availability Zone":"ap-northeast-1c"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |  2024-07-07T11:10:50+00:00 |  100      |  2024-07-07T11:10:18.974000+00:00 |  Successful  ||
||  75c64233-8bfe-a323-57f3-7d7c5cf0282f|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |  Executing scheduled action h4b-schedule           |  {}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |  2024-07-07T11:10:05+00:00 |  100      |  2024-07-07T11:10:05.224000+00:00 |  Successful  ||
||  4ca64233-6738-f50d-f5fa-66e57baf7512|  arn:aws:autoscaling:ap-northeast-1:999999999999:autoScalingGroup:09669af7-3e49-4792-bf45-9d4aedfb2c18:autoScalingGroupName/h4b-autoscaling-group |  h4b-autoscaling-group |  At 2024-07-07T10:59:58Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1.  At 2024-07-07T11:00:00Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1.                                                                                                                                                                                                                |  Launching a new EC2 instance: i-0a6ae9cd3459acce1 |  {"Subnet ID":"subnet-08dcdae2784aa9284","Availability Zone":"ap-northeast-1a"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |  2024-07-07T11:00:34+00:00 |  100      |  2024-07-07T11:00:02.749000+00:00 |  Successful  ||
|+--------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+-----------------------------------+--------------+|
```
# リソースの削除
## Auto Scaling グループ
```bash:コマンド
aws autoscaling delete-auto-scaling-group \
    --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME} \
    --force-delete
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws autoscaling delete-auto-scaling-group \
>     --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME} \
>     --force-delete
```
## 起動テンプレート
```bash:コマンド
aws ec2 delete-launch-template \
    --launch-template-name ${TEMPLATE_NAME}
```
```bash:出力
[cloudshell-user@ip-10-130-61-139 ~]$ aws ec2 delete-launch-template \
>     --launch-template-name ${TEMPLATE_NAME}
{
    "LaunchTemplate": {
        "LaunchTemplateId": "lt-0eee1100adcbf7f0d",
        "LaunchTemplateName": "h4b-template",
        "CreateTime": "2024-07-07T10:55:55+00:00",
        "CreatedBy": "arn:aws:iam::999999999999:user/admin",
        "DefaultVersionNumber": 1,
        "LatestVersionNumber": 1
    }
}
```
## Cloud Formationスタック
```bash:コマンド
aws cloudformation delete-stack \
  --stack-name ${STACK_NAME}
```
```bash:出力
cloudshell-user@ip-10-130-61-139 ~]$ aws cloudformation delete-stack \
>   --stack-name ${STACK_NAME}
```