Using Amazon EC2 Auto Scaling with EventBridge - Amazon EC2 Auto Scaling

Using Amazon EC2 Auto Scaling with EventBridge

Amazon EventBridge, formerly called CloudWatch Events, helps you set up event-driven rules that monitor resources and initiate target actions that use other AWS services.

Events from Amazon EC2 Auto Scaling are delivered to EventBridge in near real time. You can establish EventBridge rules that trigger programmatic actions and notifications in response to a variety of these events. For example, while instances are in the process of launching or terminating, you can trigger an AWS Lambda function to perform a preconfigured task. Or, you can trigger notifications to an Amazon SNS topic to monitor the progress of an instance refresh and to perform validations at specific checkpoints.

In addition to invoking Lambda functions and notifying Amazon SNS topics, EventBridge supports other types of targets and actions, such as relaying events to Amazon Kinesis streams, activating AWS Step Functions state machines, and invoking the AWS Systems Manager run command. For information about supported targets, see Amazon EventBridge targets in the Amazon EventBridge User Guide.

For more information about EventBridge, see Getting started with Amazon EventBridge in the Amazon EventBridge User Guide. Note that you can also create rules that trigger on Amazon EC2 Auto Scaling API calls. For more information, see Creating an EventBridge rule that triggers on an AWS API call using AWS CloudTrail in the Amazon EventBridge User Guide.

Auto Scaling events

The following are example events from Amazon EC2 Auto Scaling. Events are emitted on a best effort basis.

For examples of the events delivered from Amazon EC2 Auto Scaling to EventBridge when you use a warm pool, see Warm pool events.

For an example of the event for a Spot Instance interruption, see Spot Instance interruption notices in the Amazon EC2 User Guide for Linux Instances.

EC2 Instance-launch Lifecycle Action

Amazon EC2 Auto Scaling moved an instance to a Pending:Wait state due to a lifecycle hook.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance-launch Lifecycle Action", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "LifecycleActionToken": "87654321-4321-4321-4321-210987654321", "AutoScalingGroupName": "my-asg", "LifecycleHookName": "my-lifecycle-hook", "EC2InstanceId": "i-1234567890abcdef0", "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING", "NotificationMetadata": "additional-info" } }

EC2 Instance Launch Successful

Amazon EC2 Auto Scaling successfully launched an instance.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance Launch Successful", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn", "instance-arn" ], "detail": { "StatusCode": "InProgress", "Description": "Launching a new EC2 instance: i-12345678", "AutoScalingGroupName": "my-auto-scaling-group", "ActivityId": "87654321-4321-4321-4321-210987654321", "Details": { "Availability Zone": "us-west-2b", "Subnet ID": "subnet-12345678" }, "RequestId": "12345678-1234-1234-1234-123456789012", "StatusMessage": "", "EndTime": "yyyy-mm-ddThh:mm:ssZ", "EC2InstanceId": "i-1234567890abcdef0", "StartTime": "yyyy-mm-ddThh:mm:ssZ", "Cause": "description-text" } }

EC2 Instance Launch Unsuccessful

Amazon EC2 Auto Scaling failed to launch an instance.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance Launch Unsuccessful", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn", "instance-arn" ], "detail": { "StatusCode": "Failed", "AutoScalingGroupName": "my-auto-scaling-group", "ActivityId": "87654321-4321-4321-4321-210987654321", "Details": { "Availability Zone": "us-west-2b", "Subnet ID": "subnet-12345678" }, "RequestId": "12345678-1234-1234-1234-123456789012", "StatusMessage": "message-text", "EndTime": "yyyy-mm-ddThh:mm:ssZ", "EC2InstanceId": "i-1234567890abcdef0", "StartTime": "yyyy-mm-ddThh:mm:ssZ", "Cause": "description-text" } }

EC2 Instance-terminate Lifecycle Action

Amazon EC2 Auto Scaling moved an instance to a Terminating:Wait state due to a lifecycle hook.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance-terminate Lifecycle Action", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "LifecycleActionToken":"87654321-4321-4321-4321-210987654321", "AutoScalingGroupName":"my-asg", "LifecycleHookName":"my-lifecycle-hook", "EC2InstanceId":"i-1234567890abcdef0", "LifecycleTransition":"autoscaling:EC2_INSTANCE_TERMINATING", "NotificationMetadata":"additional-info" } }

EC2 Instance Terminate Successful

Amazon EC2 Auto Scaling successfully terminated an instance.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance Terminate Successful", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn", "instance-arn" ], "detail": { "StatusCode": "InProgress", "Description": "Terminating EC2 instance: i-12345678", "AutoScalingGroupName": "my-auto-scaling-group", "ActivityId": "87654321-4321-4321-4321-210987654321", "Details": { "Availability Zone": "us-west-2b", "Subnet ID": "subnet-12345678" }, "RequestId": "12345678-1234-1234-1234-123456789012", "StatusMessage": "", "EndTime": "yyyy-mm-ddThh:mm:ssZ", "EC2InstanceId": "i-1234567890abcdef0", "StartTime": "yyyy-mm-ddThh:mm:ssZ", "Cause": "description-text" } }

EC2 Instance Terminate Unsuccessful

Amazon EC2 Auto Scaling failed to terminate an instance.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance Terminate Unsuccessful", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn", "instance-arn" ], "detail": { "StatusCode": "Failed", "AutoScalingGroupName": "my-auto-scaling-group", "ActivityId": "87654321-4321-4321-4321-210987654321", "Details": { "Availability Zone": "us-west-2b", "Subnet ID": "subnet-12345678" }, "RequestId": "12345678-1234-1234-1234-123456789012", "StatusMessage": "message-text", "EndTime": "yyyy-mm-ddThh:mm:ssZ", "EC2InstanceId": "i-1234567890abcdef0", "StartTime": "yyyy-mm-ddThh:mm:ssZ", "Cause": "description-text" } }

EC2 Auto Scaling Instance Refresh Checkpoint Reached

During an instance refresh, Amazon EC2 Auto Scaling emits events when the number of instances that have been replaced reaches the percentage threshold defined for the checkpoint.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Auto Scaling Instance Refresh Checkpoint Reached", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "InstanceRefreshId": "ab00cf8f-9126-4f3c-8010-dbb8cad6fb86", "AutoScalingGroupName": "my-auto-scaling-group", "CheckpointPercentage": "50", "CheckpointDelay": "300" } }

EC2 Auto Scaling Instance Refresh Started

Amazon EC2 Auto Scaling emits events when the status of an instance refresh changes to InProgress.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Auto Scaling Instance Refresh Started", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "InstanceRefreshId": "c613620e-07e2-4ed2-a9e2-ef8258911ade", "AutoScalingGroupName": "my-auto-scaling-group" } }

EC2 Auto Scaling Instance Refresh Succeeded

Amazon EC2 Auto Scaling emits events when the status of an instance refresh changes to Succeeded.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Auto Scaling Instance Refresh Succeeded", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "InstanceRefreshId": "c613620e-07e2-4ed2-a9e2-ef8258911ade", "AutoScalingGroupName": "my-auto-scaling-group" } }

EC2 Auto Scaling Instance Refresh Failed

Amazon EC2 Auto Scaling emits events when the status of an instance refresh changes to Failed.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Auto Scaling Instance Refresh Failed", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "InstanceRefreshId": "c613620e-07e2-4ed2-a9e2-ef8258911ade", "AutoScalingGroupName": "my-auto-scaling-group" } }

EC2 Auto Scaling Instance Refresh Cancelled

Amazon EC2 Auto Scaling emits events when the status of an instance refresh changes to Cancelled.

Event Data

The following is example data for this event.

{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Auto Scaling Instance Refresh Cancelled", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "InstanceRefreshId": "c613620e-07e2-4ed2-a9e2-ef8258911ade", "AutoScalingGroupName": "my-auto-scaling-group" } }

Using AWS Lambda to handle events

AWS Lambda is a compute service that you can use to run code without provisioning or managing servers. You package your code and upload it to AWS Lambda as a Lambda function. AWS Lambda then runs the function when the function is invoked. A function can be invoked manually by you, automatically in response to events, or in response to requests from applications or services.

To help you get started with Lambda, follow the procedure below. This section shows you how to create a Lambda function and an EventBridge rule causing all instance launch and terminate events to be logged in Amazon CloudWatch Logs.

Create a Lambda function

Use the following procedure to create a Lambda function using the hello-world blueprint to serve as the target for events.

To create a Lambda function

  1. Open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. If you are new to Lambda, you see a welcome page; choose Get Started Now; otherwise, choose Create a Lambda function.

  3. On the Select blueprint page, enter hello-world for Filter, and then select the hello-world blueprint.

  4. On the Configure triggers page, choose Next.

  5. On the Configure function page, do the following:

    1. Enter a name and description for the Lambda function.

    2. Edit the code for the Lambda function. For example, the following code simply logs the event.

      console.log('Loading function'); exports.handler = function(event, context) { console.log("AutoScalingEvent()"); console.log("Event data:\n" + JSON.stringify(event, null, 4)); context.succeed("..."); };
    3. For Role, choose Choose an existing role. For Existing role, select your basic execution role. Otherwise, create a basic execution role.

    4. (Optional) For Advanced settings, make any changes that you need.

    5. Choose Next.

  6. On the Review page, choose Create function.

Route events to your Lambda function

Create a rule that matches selected events and routes them to your Lambda function to take action.

To create a rule that routes events to your Lambda function

  1. Open the Amazon EventBridge console at https://console.aws.amazon.com/events/.

  2. In the navigation pane, under Events, choose Rules.

  3. Choose Create rule.

  4. Enter a name and description for the rule.

  5. For Define pattern, do the following:

    1. Choose Event Pattern.

    2. For Event matching pattern, choose Pre-defined by service.

      Tip

      You can also create a rule that uses a custom event pattern to detect and act upon only a subset of Amazon EC2 Auto Scaling events. This subset can be based on specific fields that Amazon EC2 Auto Scaling includes in its events. For more information, see Event patterns in the Amazon EventBridge User Guide. For an example event pattern, see Step 3: Create an EventBridge rule in the tutorial for lifecycle hooks.

    3. For Service provider, choose Amazon Web Services.

    4. For Service Name, choose Auto Scaling.

    5. For Event type, choose Instance Launch and Terminate.

    6. To capture all successful and unsuccessful instance launch and terminate events, choose Any instance event.

    7. By default, the rule matches any Auto Scaling group in the Region. To make the rule match a specific Auto Scaling group, choose Specific group name(s) and select one or more Auto Scaling groups.

  6. For Select event bus, choose AWS default event bus. When an AWS service in your account emits an event, it always goes to your account's default event bus.

  7. For Target, choose Lambda function.

  8. For Function, select the Lambda function that you created.

  9. Choose Create.

To test your rule, change the size of your Auto Scaling group. If you used the example code for your Lambda function, it logs the event to CloudWatch Logs.

To test your rule

  1. Open the Amazon EC2 Auto Scaling console at https://console.aws.amazon.com/ec2autoscaling/.

  2. On the Details tab, choose Edit from the right side of the page.

  3. Change the value of Desired capacity, and then choose Update.

  4. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

  5. On the navigation pane, choose Logs.

  6. Select the log group for your Lambda function (for example, /aws/lambda/my-function).

  7. Select a log stream to view the event data. The data is displayed, similar to the following:

    
                            Viewing event data for Amazon EC2 Auto Scaling in CloudWatch Logs.

See also

For more information about using EventBridge and Amazon EC2 Auto Scaling, see Creating EventBridge rules for instance refresh events and Creating EventBridge rules for warm pool events.

For a step-by-step tutorial that shows you how to create a Lambda function to perform lifecycle actions, see Tutorial: Configure a lifecycle hook that invokes a Lambda function. This tutorial covers how to get started with lifecycle hooks. With lifecycle hooks, you can use Lambda to perform tasks on instances before they are put into service or before they are terminated.