Menu
Auto Scaling
User Guide

Getting CloudWatch Events When Your Auto Scaling Group Scales

When you use Auto Scaling to scale your applications automatically, it is useful to know when Auto Scaling is launching or terminating the EC2 instances in your Auto Scaling group. You can configure Auto Scaling to send events to Amazon CloudWatch Events whenever your Auto Scaling group scales.

For more information, see the Amazon CloudWatch Events User Guide.

Auto Scaling Events

Auto Scaling supports sending events to CloudWatch Events when the following events occur:

EC2 Instance-launch Lifecycle Action

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-12345678", 
    "LifecycleTransition":"autoscaling:EC2_INSTANCE_LAUNCHING" 
  } 
}

EC2 Instance Launch Successful

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-12345678",
      "StartTime": "yyyy-mm-ddThh:mm:ssZ",
      "Cause": "description-text",
  }
}

EC2 Instance Launch Unsuccessful

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-12345678",
      "StartTime": "yyyy-mm-ddThh:mm:ssZ",
      "Cause": "description-text",
  }
}

EC2 Instance-terminate Lifecycle Action

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-12345678", 
    "LifecycleTransition":"autoscaling:EC2_INSTANCE_TERMINATING" 
  } 
}

EC2 Instance Terminate Successful

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-12345678",
      "StartTime": "yyyy-mm-ddThh:mm:ssZ",
      "Cause": "description-text",
  }
}

EC2 Instance Terminate Unsuccessful

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-12345678",
      "StartTime": "yyyy-mm-ddThh:mm:ssZ",
      "Cause": "description-text",
  }
}

Create a Lambda Function

Use the following procedure to create a Lambda function to handle an Auto Scaling event.

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, type 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. Type 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:

      Copy
      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 if you have an existing role that you'd like to use, and then choose your role from Existing role. Alternatively, to create a new role, choose one of the other options for Role and then follow the directions.

    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

Use the following procedure to route Auto Scaling events to your Lambda function.

To route events to your Lambda function

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

  2. On the navigation pane, choose Events.

  3. Choose Create rule.

  4. For Event selector, choose Auto Scaling as the event source. By default, the rule applies to all Auto Scaling events for all of your Auto Scaling groups. Alternatively, you can select specific events or a specific Auto Scaling group.

  5. For Targets, choose Add target. Choose Lambda function as the target type, and then select your Lambda function.

  6. Choose Configure details.

  7. For Rule definition, type a name and description for your rule.

  8. Choose Create rule.

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 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, choose Auto Scaling Groups, and then select your Auto Scaling group.

  3. On the Details tab, choose Edit.

  4. Change the value of Desired, and then choose Save.

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

  6. On the navigation pane, choose Logs.

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

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

    
                        Viewing event data for Auto Scaling in CloudWatch Logs.