AutoScaling GroupのInstance Refreshがいろいろ便利になりました(構成が一致しているインスタンスの更新回避/更新時のLaunch Template置き換え/EventBridge対応)

2021.08.30

中山(順)です

EC2 AutoScaling GroupでInstance Refreshに関する3つのアップデートが一度に発表されていました。

Amazon EC2 Auto Scaling enhances Instance Refresh with configuration checks, Launch Template validation, and Amazon EventBridge notifications

  • Instance Refresh now enables customers to avoid replacing instances if those instances already have the desired configuration.
  • Amazon EC2 Auto Scaling now supports updating the Launch Template definition upon successful completion of an Instance Refresh.
  • Amazon EC2 Auto Scaling now generates four new Amazon EventBridge event notifications for Instance Refresh.

今回はこの機能を確認していきたいと思います。

事前準備

事前準備として、以下の通りLaunch TemplateとAutoScaling Groupを作成しておきます。

Launch Template

AMI IDとインスタンスタイプだけ指定します。

LT_DATA_FILE_NAME='LaunchTemplateData.json'

cat << EOF > ${LT_DATA_FILE_NAME}
{
    "ImageId": "ami-09ebacdc178ae23b7",
    "InstanceType": "t3.nano"
}
EOF

aws ec2 create-launch-template \
    --launch-template-name test-lt-asg-termination \
    --launch-template-data file://${LT_DATA_FILE_NAME}
{
    "LaunchTemplate": {
        "LatestVersionNumber": 1, 
        "LaunchTemplateId": "lt-072dbbbcc70084c2f", 
        "LaunchTemplateName": "test-lt-asg-termination", 
        "DefaultVersionNumber": 1, 
        "CreatedBy": "arn:aws:sts::xxxxxxxxxxxx:assumed-role/AdminRoleForEC2/i-008c2734c7e6a6337", 
        "CreateTime": "2021-08-30T04:30:25.000Z"
    }
}

AutoScaling Group

先ほど作成したLaunch Templateに基づいてインスタンスを2つ起動するように設定します。

aws autoscaling create-auto-scaling-group \
    --auto-scaling-group-name test-asg-termination \
    --launch-template "LaunchTemplateId=lt-072dbbbcc70084c2f" \
    --min-size 0 \
    --max-size 6 \
    --desired-capacity 2 \
    --vpc-zone-identifier "subnet-0b33c6ab5dbe1dda5,subnet-09166ccaa28459f22,subnet-03267eb84db1ec870"

やってみた(構成が一致しているインスタンスの更新回避)

このアップデートでは、一部のインスタンスが古い起動設定に基づいて構成されていて、残りが(起動設定の更新とスケールアウトによって)最新の起動設定に基づいて構成されている時などに古い構成のインスタンス「だけ」を置き換えるようにできる、というアップデートです。 これにより更新に要する時間を短縮できます。

Launch Templateを更新

まず、Launch Templateを修正します。 今回はインスタンスタイプを変更します。

LT_DATA_FILE_NAME='LaunchTemplateData_2.json'

cat << EOF > ${LT_DATA_FILE_NAME}
{
    "InstanceType": "t3.micro"
}
EOF

aws ec2 create-launch-template-version \
    --launch-template-id lt-072dbbbcc70084c2f \
    --source-version 1 \
    --launch-template-data file://${LT_DATA_FILE_NAME}
{
    "LaunchTemplateVersion": {
        "LaunchTemplateId": "lt-072dbbbcc70084c2f", 
        "LaunchTemplateName": "test-lt-asg-termination", 
        "VersionNumber": 2, 
        "CreatedBy": "arn:aws:sts::xxxxxxxxxxxx:assumed-role/AdminRoleForEC2/i-008c2734c7e6a6337", 
        "LaunchTemplateData": {
            "InstanceType": "t3.micro", 
            "ImageId": "ami-09ebacdc178ae23b7"
        }, 
        "DefaultVersion": false, 
        "CreateTime": "2021-08-30T04:32:32.000Z"
    }
}

そして、新しいバージョンをデフォルトバージョンにします。

aws ec2 modify-launch-template \
    --launch-template-id lt-072dbbbcc70084c2f \
    --default-version 2
{
    "LaunchTemplate": {
        "LatestVersionNumber": 2, 
        "LaunchTemplateId": "lt-072dbbbcc70084c2f", 
        "LaunchTemplateName": "test-lt-asg-termination", 
        "DefaultVersionNumber": 2, 
        "CreatedBy": "arn:aws:sts::xxxxxxxxxxxx:assumed-role/AdminRoleForEC2/i-008c2734c7e6a6337", 
        "CreateTime": "1970-01-01T00:00:00.000Z"
    }
}

Desired Capacityを更新(スケールアウト)

それでは、Desired Capacityを増やしてスケールアウトします。

aws autoscaling update-auto-scaling-group \
    --auto-scaling-group-name test-asg-termination \
    --desired-capacity 4

すると、古いバージョンのLaunch Templateで起動したインスタンスと新しいバージョンで起動したインスタンスが混在した状態になります。

aws autoscaling describe-auto-scaling-groups \
    --auto-scaling-group-name test-asg-termination \
    --query AutoScalingGroups[0].Instances[]
[
    {
        "ProtectedFromScaleIn": false, 
        "AvailabilityZone": "ap-northeast-1d", 
        "LaunchTemplate": {
            "LaunchTemplateName": "test-lt-asg-termination", 
            "Version": "2", 
            "LaunchTemplateId": "lt-072dbbbcc70084c2f"
        }, 
        "InstanceId": "i-02704cfabd222617f", 
        "HealthStatus": "Healthy", 
        "LifecycleState": "InService", 
        "InstanceType": "t3.micro"
    }, 
    {
        "ProtectedFromScaleIn": false, 
        "AvailabilityZone": "ap-northeast-1a", 
        "LaunchTemplate": {
            "LaunchTemplateName": "test-lt-asg-termination", 
            "Version": "2", 
            "LaunchTemplateId": "lt-072dbbbcc70084c2f"
        }, 
        "InstanceId": "i-0bf7db01c1f64db57", 
        "HealthStatus": "Healthy", 
        "LifecycleState": "InService", 
        "InstanceType": "t3.micro"
    }, 
    {
        "ProtectedFromScaleIn": false, 
        "AvailabilityZone": "ap-northeast-1c", 
        "LaunchTemplate": {
            "LaunchTemplateName": "test-lt-asg-termination", 
            "Version": "1", 
            "LaunchTemplateId": "lt-072dbbbcc70084c2f"
        }, 
        "InstanceId": "i-0e498173c69aabb2a", 
        "HealthStatus": "Healthy", 
        "LifecycleState": "InService", 
        "InstanceType": "t3.nano"
    }, 
    {
        "ProtectedFromScaleIn": false, 
        "AvailabilityZone": "ap-northeast-1d", 
        "LaunchTemplate": {
            "LaunchTemplateName": "test-lt-asg-termination", 
            "Version": "1", 
            "LaunchTemplateId": "lt-072dbbbcc70084c2f"
        }, 
        "InstanceId": "i-0e5016b8585fab1c3", 
        "HealthStatus": "Healthy", 
        "LifecycleState": "InService", 
        "InstanceType": "t3.nano"
    }
]

Instance Refreshを実行

それでは、Instanceをリフレッシュします。 今回のアップデートを利用するためには、 --preferences "SkipMatching=true" をパラメーターとして設定します。

aws autoscaling start-instance-refresh \
    --auto-scaling-group-name test-asg-termination \
    --preferences "SkipMatching=true"
{
    "InstanceRefreshId": "f3a8cd40-829d-4c27-91a2-ec7182abd468"
}

Launch Templateのバージョンが揃っていることと併せて、新しいバージョンだったインスタンスIDが変わっていないことが確認できます。

aws autoscaling describe-auto-scaling-groups \
    --auto-scaling-group-name test-asg-termination \
    --query AutoScalingGroups[0].Instances[]
[
    {
        "InstanceId": "i-0195afc0d35c09b5a",
        "InstanceType": "t3.micro",
        "AvailabilityZone": "ap-northeast-1c",
        "LifecycleState": "InService",
        "HealthStatus": "Healthy",
        "LaunchTemplate": {
            "LaunchTemplateId": "lt-072dbbbcc70084c2f",
            "LaunchTemplateName": "test-lt-asg-termination",
            "Version": "2"
        },
        "ProtectedFromScaleIn": false
    },
    {
        "InstanceId": "i-02704cfabd222617f",
        "InstanceType": "t3.micro",
        "AvailabilityZone": "ap-northeast-1d",
        "LifecycleState": "InService",
        "HealthStatus": "Healthy",
        "LaunchTemplate": {
            "LaunchTemplateId": "lt-072dbbbcc70084c2f",
            "LaunchTemplateName": "test-lt-asg-termination",
            "Version": "2"
        },
        "ProtectedFromScaleIn": false
    },
    {
        "InstanceId": "i-0bf7db01c1f64db57",
        "InstanceType": "t3.micro",
        "AvailabilityZone": "ap-northeast-1a",
        "LifecycleState": "InService",
        "HealthStatus": "Healthy",
        "LaunchTemplate": {
            "LaunchTemplateId": "lt-072dbbbcc70084c2f",
            "LaunchTemplateName": "test-lt-asg-termination",
            "Version": "2"
        },
        "ProtectedFromScaleIn": false
    },
    {
        "InstanceId": "i-0d1907c0a9cf01dc8",
        "InstanceType": "t3.micro",
        "AvailabilityZone": "ap-northeast-1d",
        "LifecycleState": "InService",
        "HealthStatus": "Healthy",
        "LaunchTemplate": {
            "LaunchTemplateId": "lt-072dbbbcc70084c2f",
            "LaunchTemplateName": "test-lt-asg-termination",
            "Version": "2"
        },
        "ProtectedFromScaleIn": false
    }
]

アクティビティを確認すると、2つのインスタンスだけ入れ替えが発生していることを確認できます(直近の4件が今回のインスタンスリフレッシュに関連するアクティビティです)。

aws autoscaling describe-scaling-activities \
    --auto-scaling-group-name test-asg-termination
{
    "Activities": [
        {
            "ActivityId": "1475ee55-a397-6ed2-1665-615dbfafc8b1",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Launching a new EC2 instance: i-0195afc0d35c09b5a",
            "Cause": "At 2021-08-30T04:54:42Z an instance was launched in response to an instance refresh.",
            "StartTime": "2021-08-30T04:54:44.955000+00:00",
            "EndTime": "2021-08-30T04:57:00+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-03267eb84db1ec870\",\"Availability Zone\":\"ap-northeast-1c\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        },
        {
            "ActivityId": "e195f496-ab5e-40a8-9a20-4c363124e535",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Terminating EC2 instance: i-0e498173c69aabb2a",
            "Cause": "At 2021-08-30T04:54:42Z an instance was taken out of service in response to an instance refresh.  At 2021-08-30T04:54:42Z instance i-0e498173c69aabb2a was selected for termination.",
            "StartTime": "2021-08-30T04:54:42.696000+00:00",
            "EndTime": "2021-08-30T04:55:45+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-03267eb84db1ec870\",\"Availability Zone\":\"ap-northeast-1c\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        },
        {
            "ActivityId": "7665ee55-9a2d-5d4d-a668-23e03378aaf8",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Launching a new EC2 instance: i-0d1907c0a9cf01dc8",
            "Cause": "At 2021-08-30T04:52:08Z an instance was launched in response to an instance refresh.",
            "StartTime": "2021-08-30T04:52:10.711000+00:00",
            "EndTime": "2021-08-30T04:54:26+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-0b33c6ab5dbe1dda5\",\"Availability Zone\":\"ap-northeast-1d\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        },
        {
            "ActivityId": "76532302-317b-4b48-ad75-ca3cdf2f8722",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Terminating EC2 instance: i-0e5016b8585fab1c3",
            "Cause": "At 2021-08-30T04:52:08Z an instance was taken out of service in response to an instance refresh.  At 2021-08-30T04:52:08Z instance i-0e5016b8585fab1c3 was selected for termination.",
            "StartTime": "2021-08-30T04:52:08.076000+00:00",
            "EndTime": "2021-08-30T04:52:50+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-0b33c6ab5dbe1dda5\",\"Availability Zone\":\"ap-northeast-1d\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        },
        {
            "ActivityId": "cbd5ee55-635b-c848-f4b7-61adcdb3e6bd",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Launching a new EC2 instance: i-0bf7db01c1f64db57",
            "Cause": "At 2021-08-30T04:37:06Z a user request update of AutoScalingGroup constraints to min: 0, max: 6, desired: 4 changing the desired capacity from 2 to 4.  At 2021-08-30T04:37:10Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 2 to 4.",
            "StartTime": "2021-08-30T04:37:12.562000+00:00",
            "EndTime": "2021-08-30T04:37:29+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-09166ccaa28459f22\",\"Availability Zone\":\"ap-northeast-1a\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        },
        {
            "ActivityId": "1915ee55-6359-f517-188a-d92b9dd07f9a",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Launching a new EC2 instance: i-02704cfabd222617f",
            "Cause": "At 2021-08-30T04:37:06Z a user request update of AutoScalingGroup constraints to min: 0, max: 6, desired: 4 changing the desired capacity from 2 to 4.  At 2021-08-30T04:37:10Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 2 to 4.",
            "StartTime": "2021-08-30T04:37:12.445000+00:00",
            "EndTime": "2021-08-30T04:37:28+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-0b33c6ab5dbe1dda5\",\"Availability Zone\":\"ap-northeast-1d\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        },
        {
            "ActivityId": "fd05ee55-4e56-9f8b-a38a-686121cfda72",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Launching a new EC2 instance: i-0e498173c69aabb2a",
            "Cause": "At 2021-08-30T04:31:23Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 2.  At 2021-08-30T04:31:25Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 2.",
            "StartTime": "2021-08-30T04:31:28.167000+00:00",
            "EndTime": "2021-08-30T04:31:44+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-03267eb84db1ec870\",\"Availability Zone\":\"ap-northeast-1c\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        },
        {
            "ActivityId": "bca5ee55-4e53-2d6c-3552-cab8de77b226",
            "AutoScalingGroupName": "test-asg-termination",
            "Description": "Launching a new EC2 instance: i-0e5016b8585fab1c3",
            "Cause": "At 2021-08-30T04:31:23Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 2.  At 2021-08-30T04:31:25Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 2.",
            "StartTime": "2021-08-30T04:31:27.947000+00:00",
            "EndTime": "2021-08-30T04:31:44+00:00",
            "StatusCode": "Successful",
            "Progress": 100,
            "Details": "{\"Subnet ID\":\"subnet-0b33c6ab5dbe1dda5\",\"Availability Zone\":\"ap-northeast-1d\"}",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
        }
    ]
}

なお、 SkipMatching=false でInstance Refreshを実行すると、すべてのインスタンスが置換されます。 興味のある方は試してみてください。

やってみた(更新時のLaunch Template置き換え)

次のアップデートは、インスタンスリフレッシュのタイミングでAutoScaling Groupに設定されているLaunch Templateを更新できるというものです。

AutoScaling Groupの確認

まずは、現在設定されているLaunch Templateを確認します。最初に作成したものが設定されています。

aws autoscaling describe-auto-scaling-groups \
    --auto-scaling-group-names test-asg-termination
{
    "AutoScalingGroups": [
        {
            "AutoScalingGroupName": "test-asg-termination",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination",
            "LaunchTemplate": {
                "LaunchTemplateId": "lt-072dbbbcc70084c2f",
                "LaunchTemplateName": "test-lt-asg-termination"
            },
            "MinSize": 0,
            "MaxSize": 6,
            "DesiredCapacity": 4,
            "DefaultCooldown": 300,
            "AvailabilityZones": [
                "ap-northeast-1a",
                "ap-northeast-1c",
                "ap-northeast-1d"
            ],
            "LoadBalancerNames": [],
            "TargetGroupARNs": [],
            "HealthCheckType": "EC2",
            "HealthCheckGracePeriod": 0,
            "Instances": [
                {
                    "InstanceId": "i-091efa7e6b3d4ab55",
                    "InstanceType": "t3.micro",
                    "AvailabilityZone": "ap-northeast-1a",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-072dbbbcc70084c2f",
                        "LaunchTemplateName": "test-lt-asg-termination",
                        "Version": "2"
                    },
                    "ProtectedFromScaleIn": false
                },
                {
                    "InstanceId": "i-0a237ff0647fae12c",
                    "InstanceType": "t3.micro",
                    "AvailabilityZone": "ap-northeast-1d",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-072dbbbcc70084c2f",
                        "LaunchTemplateName": "test-lt-asg-termination",
                        "Version": "2"
                    },
                    "ProtectedFromScaleIn": false
                },
                {
                    "InstanceId": "i-0dc124ac2778aed26",
                    "InstanceType": "t3.micro",
                    "AvailabilityZone": "ap-northeast-1c",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-072dbbbcc70084c2f",
                        "LaunchTemplateName": "test-lt-asg-termination",
                        "Version": "2"
                    },
                    "ProtectedFromScaleIn": false
                },
                {
                    "InstanceId": "i-0fa7e39411c428231",
                    "InstanceType": "t3.micro",
                    "AvailabilityZone": "ap-northeast-1a",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-072dbbbcc70084c2f",
                        "LaunchTemplateName": "test-lt-asg-termination",
                        "Version": "2"
                    },
                    "ProtectedFromScaleIn": false
                }
            ],
            "CreatedTime": "2021-08-30T04:31:23.040000+00:00",
            "SuspendedProcesses": [],
            "VPCZoneIdentifier": "subnet-03267eb84db1ec870,subnet-0b33c6ab5dbe1dda5,subnet-09166ccaa28459f22",
            "EnabledMetrics": [],
            "Tags": [],
            "TerminationPolicies": [
                "Default"
            ],
            "NewInstancesProtectedFromScaleIn": false,
            "ServiceLinkedRoleARN": "arn:aws:iam::xxxxxxxxxxxx:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
        }
    ]
}

新しいLaunch Templateの作成

今回は新しいLaunch Templateを作成し、AutoScaling GroupのInstance Refreshを併せて設定の入れ替えを実行します。 なお、既存のLaunch Templateの新しいバージョンを指定することも可能です(今回はやりません)。

LT_DATA_FILE_NAME='LaunchTemplateData_3.json'

cat << EOF > ${LT_DATA_FILE_NAME}
{
    "ImageId": "ami-09ebacdc178ae23b7",
    "InstanceType": "t3.nano"
}
EOF

aws ec2 create-launch-template \
    --launch-template-name test-lt-asg-refresh2 \
    --launch-template-data file://${LT_DATA_FILE_NAME}
{
    "LaunchTemplate": {
        "LaunchTemplateId": "lt-036a73b4c3d6999b9",
        "LaunchTemplateName": "test-lt-asg-refresh2",
        "CreateTime": "2021-08-30T05:15:46+00:00",
        "CreatedBy": "arn:aws:sts::xxxxxxxxxxxx:assumed-role/cm-nakayama.nobuhiro/cm-nakayama.nobuhiro",
        "DefaultVersionNumber": 1,
        "LatestVersionNumber": 1
    }
}

Instance Refresh

それではInstance Refreshを実行します。 新しいLaunch Templateは、 --desired-configuration で設定します。

REFRESH_DATA_FILE_NAME='RefreshConfiguration.json'

cat << EOF > ${REFRESH_DATA_FILE_NAME}
{
  "LaunchTemplate": {
    "LaunchTemplateId": "lt-036a73b4c3d6999b9",
    "Version": "1"
  }
}
EOF

aws autoscaling start-instance-refresh \
    --auto-scaling-group-name test-asg-termination \
    --desired-configuration file://${REFRESH_DATA_FILE_NAME}
{
    "InstanceRefreshId": "45bff55a-4b65-4f8d-8a4e-6144ac298a1b"
}

インスタンス置き換えだけでなく、Launch Templateも変更されていることを確認できます。

aws autoscaling describe-auto-scaling-groups \
    --auto-scaling-group-names test-asg-termination
{
    "AutoScalingGroups": [
        {
            "AutoScalingGroupName": "test-asg-termination",
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination",
            "LaunchTemplate": {
                "LaunchTemplateId": "lt-036a73b4c3d6999b9",
                "LaunchTemplateName": "test-lt-asg-refresh2",
                "Version": "1"
            },
            "MinSize": 0,
            "MaxSize": 6,
            "DesiredCapacity": 4,
            "DefaultCooldown": 300,
            "AvailabilityZones": [
                "ap-northeast-1a",
                "ap-northeast-1c",
                "ap-northeast-1d"
            ],
            "LoadBalancerNames": [],
            "TargetGroupARNs": [],
            "HealthCheckType": "EC2",
            "HealthCheckGracePeriod": 0,
            "Instances": [
                {
                    "InstanceId": "i-03e3d730add256e20",
                    "InstanceType": "t3.nano",
                    "AvailabilityZone": "ap-northeast-1c",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-036a73b4c3d6999b9",
                        "LaunchTemplateName": "test-lt-asg-refresh2",
                        "Version": "1"
                    },
                    "ProtectedFromScaleIn": false
                },
                {
                    "InstanceId": "i-08ff825efc5764f50",
                    "InstanceType": "t3.nano",
                    "AvailabilityZone": "ap-northeast-1a",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-036a73b4c3d6999b9",
                        "LaunchTemplateName": "test-lt-asg-refresh2",
                        "Version": "1"
                    },
                    "ProtectedFromScaleIn": false
                },
                {
                    "InstanceId": "i-0da14dce522094b28",
                    "InstanceType": "t3.nano",
                    "AvailabilityZone": "ap-northeast-1d",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-036a73b4c3d6999b9",
                        "LaunchTemplateName": "test-lt-asg-refresh2",
                        "Version": "1"
                    },
                    "ProtectedFromScaleIn": false
                },
                {
                    "InstanceId": "i-0f4503101b781f1d1",
                    "InstanceType": "t3.nano",
                    "AvailabilityZone": "ap-northeast-1c",
                    "LifecycleState": "InService",
                    "HealthStatus": "Healthy",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-036a73b4c3d6999b9",
                        "LaunchTemplateName": "test-lt-asg-refresh2",
                        "Version": "1"
                    },
                    "ProtectedFromScaleIn": false
                }
            ],
            "CreatedTime": "2021-08-30T04:31:23.040000+00:00",
            "SuspendedProcesses": [],
            "VPCZoneIdentifier": "subnet-03267eb84db1ec870,subnet-0b33c6ab5dbe1dda5,subnet-09166ccaa28459f22",
            "EnabledMetrics": [],
            "Tags": [],
            "TerminationPolicies": [
                "Default"
            ],
            "NewInstancesProtectedFromScaleIn": false,
            "ServiceLinkedRoleARN": "arn:aws:iam::xxxxxxxxxxxx:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
        }
    ]
}

やってみた(EventBridge対応)

最後に、Event BridgeでInstance Refreshに関するいくつかのイベントが新たに拾えるようになったというアップデートです。

Event Ruleの作成

早速、Instance Refreshの開始・成功・失敗・キャンセル等のイベントを拾うルールを作成します。 マネージメントコンソールで設定する場合は以下のように簡単に設定できます。

{
  "source": [
    "aws.autoscaling"
  ],
  "detail-type": [
    "EC2 Auto Scaling Instance Refresh Checkpoint Reached",
    "EC2 Auto Scaling Instance Refresh Started",
    "EC2 Auto Scaling Instance Refresh Succeeded",
    "EC2 Auto Scaling Instance Refresh Failed",
    "EC2 Auto Scaling Instance Refresh Cancelled"
  ]
}

TargetをCloudWatch Logsにして結果を確認したところ、以下のようにイベントを記録できていました。

{
    "version": "0",
    "id": "bf044848-aa9f-1f77-af88-158f5049fd24",
    "detail-type": "EC2 Auto Scaling Instance Refresh Started",
    "source": "aws.autoscaling",
    "account": "xxxxxxxxxxxx",
    "time": "2021-08-30T05:43:19Z",
    "region": "ap-northeast-1",
    "resources": [
        "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
    ],
    "detail": {
        "InstanceRefreshId": "b7088b6e-d4fa-49d0-ab27-8d12e0b6b587",
        "AutoScalingGroupName": "test-asg-termination"
    }
}

{
    "version": "0",
    "id": "0a34cd6a-99c2-ce21-31e7-cadf0fdafdf2",
    "detail-type": "EC2 Auto Scaling Instance Refresh Succeeded",
    "source": "aws.autoscaling",
    "account": "xxxxxxxxxxxx",
    "time": "2021-08-30T05:53:37Z",
    "region": "ap-northeast-1",
    "resources": [
        "arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxxx:autoScalingGroup:f18fb04c-3f30-4c54-9b70-e87bb68cf267:autoScalingGroupName/test-asg-termination"
    ],
    "detail": {
        "InstanceRefreshId": "b7088b6e-d4fa-49d0-ab27-8d12e0b6b587",
        "AutoScalingGroupName": "test-asg-termination"
    }
}

まとめ

Instance Refreshは、AutoScaling Groupを構成するインスタンスを適切な状態へ簡単に最新化することができます。 今回のアップデートは、これまでよりかゆいところに手が届くようなアップデートかと思いますので、運用で活用している方は是非試してみてください。