Amazon CloudWatch Events
User Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Tutorial: Log the State of an Auto Scaling Group Using CloudWatch Events

You can run an AWS Lambda function that logs an event whenever an Auto Scaling group launches or terminates an Amazon EC2 instance and whether the launch or terminate event was successful.

For information about additional CloudWatch Events scenarios using Amazon EC2 Auto Scaling events, see Getting CloudWatch Events When Your Auto Scaling Group Scales in the Amazon EC2 Auto Scaling User Guide.

Step 1: Create an AWS Lambda Function

Create a Lambda function to log the scale-out and scale-in events for your Auto Scaling group. Specify this function when you create your rule.

To create a Lambda function

  1. Open the AWS Lambda console at

  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 for the filter and choose 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. For example, name the function "LogAutoScalingEvent".

    2. Edit the sample code for the Lambda function. For example:

      'use strict'; exports.handler = (event, context, callback) => { console.log('LogAutoScalingEvent'); console.log('Received event:', JSON.stringify(event, null, 2)); callback(null, 'Finished'); };
    3. For Role, choose Choose an existing role. For Existing role, select your basic execution role. Otherwise, create a new basic execution role.

    4. Choose Next.

  6. Choose Create function.

Step 2: Create a Rule

Create a rule to run your Lambda function whenever your Auto Scaling group launches or terminates an instance.

To create a rule

  1. Open the CloudWatch console at

  2. In the navigation pane, choose Events, Create rule.

  3. For Event source, do the following:

    1. Choose Event Pattern.

    2. Choose Build event pattern to match events by service.

    3. Choose Auto Scaling, Instance Launch and Terminate.

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

                            The Event selector pane
  4. 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 then select one or more Auto Scaling groups.

  5. For Targets, choose Add target, Lambda function.

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

  7. Choose Configure details.

  8. For Rule definition, type a name and description for the rule. For example, describe the rule as "Log whenever an Auto Scaling group scales out or in".

  9. Choose Create rule.

Step 3: Test the Rule

You can test your rule by manually scaling an Auto Scaling group so that it launches an instance. After waiting a few minutes for the scale-out event to occur, verify that your Lambda function was invoked.

To test your rule using an Auto Scaling group

  1. To increase the size of your Auto Scaling group, do the following:

    1. Open the Amazon EC2 console at

    2. On the navigation pane, choose Auto Scaling, Auto Scaling Groups.

    3. Select the check box for your Auto Scaling group.

    4. On the Details tab, choose Edit. For Desired, increase the desired capacity by one. For example, if the current value is 2, type 3. The desired capacity must be less than or equal to the maximum size of the group. If your new value for Desired is greater than Max, you must update Max. When you are finished, choose Save.

  2. Open the CloudWatch console at

  3. In the navigation pane, choose Events, Rules, select the name of the rule that you created, and then choose Show metrics for the rule.

  4. To view the output from your Lambda function, do the following:

    1. In the navigation pane, choose Logs.

    2. Select the name of the log group for your Lambda function (/aws/lambda/function-name).

    3. Select the name of log stream to view the data provided by the function for the instance that you launched.

  5. (Optional) When you are finished, you can decrease the desired capacity by one so that the Auto Scaling group returns to its previous size.