添加或更新生命周期挂钩时使用的事件类型和事件模式 - Amazon EC2 Auto Scaling
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

添加或更新生命周期挂钩时使用的事件类型和事件模式

将生命周期挂钩添加到 Auto Scaling 组时,事件将以 JSON 格式发送到 EventBridge。您可以创建一个 EventBridge 规则,该规则使用事件模式来筛选传入事件,然后调用 Lambda 函数或其他目标。

生命周期挂钩发出两种事件类型:

  • EC2 Instance-launch Lifecycle Action

  • EC2 Instance-terminate Lifecycle Action.

将热池添加到 Auto Scaling 组后,detail部分,用于EC2 Instance-launch Lifecycle Action事件包含新的OriginDestination字段。

OriginDestination可以如下:

EC2 | AutoScalingGroup | WarmPool

热泳池活动

本部分列出 Amazon EC2 Auto Scaling 的示例事件。尽最大努力发出事件。

绑定到温水池的实例

下面的示例显示了一个生命周期钩子的事件Warmed:Pending:Wait状态。它代表一个正在启动到温池中的实例。

{ "version": "0", "id": "18b2ec17-3e9b-4c15-8024-ff2e8ce8786a", "detail-type": "EC2 Instance-launch Lifecycle Action", "source": "aws.autoscaling", "account": "123456789012", "time": "2021-01-13T00:12:37.214Z", "region": "us-west-2", "resources": [ "arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:042cba90-ad2f-431c-9b4d-6d9055bcc9fb:autoScalingGroupName/my-asg" ], "detail": { "LifecycleActionToken": "71514b9d-6a40-4b26-8523-05e7eEXAMPLE", "AutoScalingGroupName": "my-asg", "LifecycleHookName": "my-lifecycle-hook-for-warming-instances", "EC2InstanceId": "i-1234567890abcdef0", "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING", "NotificationMetadata": "additional-info", "Origin": "EC2", "Destination": "WarmPool" } }

离开温池的实例

下面的示例显示了一个生命周期钩子的事件Pending:Wait状态。它代表一个由于扩展事件而离开温暖池的实例。

{ "version": "0", "id": "5985cdde-1f01-9ae2-1498-3c8ea162d141", "detail-type": "EC2 Instance-launch Lifecycle Action", "source": "aws.autoscaling", "account": "123456789012", "time": "2021-01-19T00:35:52.359Z", "region": "us-west-2", "resources": [ "arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:042cba90-ad2f-431c-9b4d-6d9055bcc9fb:autoScalingGroupName/my-asg" ], "detail": { "LifecycleActionToken": "19cc4d4a-e450-4d1c-b448-0de67EXAMPLE", "AutoScalingGroupName": "my-asg", "LifecycleHookName": "my-lifecycle-hook-for-warmed-instances", "EC2InstanceId": "i-1234567890abcdef0", "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING", "NotificationMetadata": "additional-info", "Origin": "WarmPool", "Destination": "AutoScalingGroup" } }

在温池外启动的实例

下面的示例显示了一个生命周期钩子的事件Pending:Wait状态。它表示直接在 Auto Scaling 组中启动的实例。

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance-launch Lifecycle Action", "source": "aws.autoscaling", "account": "123456789012", "time": "2021-02-01T17:18:06.082Z", "region": "us-west-2", "resources": [ "arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:042cba90-ad2f-431c-9b4d-6d9055bcc9fb:autoScalingGroupName/my-asg" ], "detail": { "LifecycleActionToken": "87654321-4321-4321-4321-21098EXAMPLE", "AutoScalingGroupName": "my-asg", "LifecycleHookName": "my-lifecycle-hook-for-launching-instances", "EC2InstanceId": "i-1234567890abcdef0", "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING", "NotificationMetadata": "additional-info", "Origin": "EC2", "Destination": "AutoScalingGroup" } }

示例事件模式

前一节提供 Amazon EC2 Auto Scaling 发出的事件示例。

EventBridge 事件模式与它们匹配的事件具有相同的结构。模式引用了您要匹配的字段,并提供您所查找的值。

事件中的下列字段构成规则中定义的事件模式以调用操作:

"source": "aws.autoscaling"

确定事件来自 Amazon EC2 Auto Scaling。

"detail-type": "EC2 Instance-launch Lifecycle Action"

识别事件类型。

"Origin": "EC2"

标识实例的来源。

"Destination": "WarmPool"

标识实例的目标位置。

使用以下示例事件模式捕获与进入温池的实例相关联的所有事件。

{ "source": [ "aws.autoscaling" ], "detail-type": [ "EC2 Instance-launch Lifecycle Action" ], "detail": { "Origin": [ "EC2" ], "Destination": [ "WarmPool" ] } }

使用以下示例事件模式捕获与因向外扩展事件而离开热池的实例相关联的所有事件。

{ "source": [ "aws.autoscaling" ], "detail-type": [ "EC2 Instance-launch Lifecycle Action" ], "detail": { "Origin": [ "WarmPool" ], "Destination": [ "AutoScalingGroup" ] } }

使用以下示例事件模式捕获与直接在 Auto Scaling 组中启动的实例相关联的所有事件。

{ "source": [ "aws.autoscaling" ], "detail-type": [ "EC2 Instance-launch Lifecycle Action" ], "detail": { "Origin": [ "EC2" ], "Destination": [ "AutoScalingGroup" ] } }

使用以下示例事件模式捕获与EC2 Instance-launch Lifecycle Action,无论起点或目的地如何。

{ "source": [ "aws.autoscaling" ], "detail-type": [ "EC2 Instance-launch Lifecycle Action" ] }