Menu
Amazon CloudWatch Events
User Guide

Scenario 4: Log the State of an Auto Scaling Group

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

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

Step 1: Create an AWS Lambda function

To create an AWS Lambda function

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

  2. Choose Create a Lambda function, and then on the Select blueprint screen, choose hello-world.

  3. On the Configure function screen, in the Name field, enter AutoScalingLaunchTerminate.

  4. In the Lambda function code section, edit the sample code to match the following example:

    console.log('Loading function');
    
    exports.handler = function(event, context) {
        console.log('AutoScalingLaunchTerminate()');
        console.log('Here is the event:', JSON.stringify(event, null, 2));
        context.succeed('Ready');
    };
  5. Under Lambda function handler and role, in the Role field, if you have a lambda_basic_execution_rule, select it. Otherwise, create a new basic execution role.

  6. Choose Next, and then on the Review screen, choose Edit to make any changes. If you're satisfied with the rule, choose Create function.

Step 2: Create an Amazon CloudWatch Events Rule

To create a CloudWatch Events rule

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

  2. In the navigation pane, choose Events.

  3. Choose Create rule, and then under Event selector, choose Auto Scaling.

  4. Choose Any instance event to capture all successful and unsuccessful launch and terminate actions.

  5. Do one of the following:

    • To make the rule respond to any of your Auto Scaling groups in the region, choose Any group name.

    • To make the rule respond to a specific Auto Scaling group, choose Specific group name(s) and then in the text box, enter an Auto Scaling group name.

  6. Under Targets, choose Add target. In the Select target type list, choose Lambda function.

  7. In the Function list, select the AutoScalingLaunchTerminate function that you created in "Step 1: Create an AWS Lambda Function."

  8. Choose Configure input, and then choose one of the following options:

    • Matched event—Sends all of the data fields in the event to CloudWatch Logs.

    • Part of the matched event—Sends only the specified data field of the event to CloudWatch Logs. You specify the part of the event using a string formatted $.first_parameter.second_parameter. For example, to send just the detail part of the event, type $.detail.

    • Constant—Sends a JSON-formatted text string that you specify to CloudWatch Logs. For example, to send a text string for the event, type {"Name":"MyInstance"}. The constant must be valid JSON.

  9. Choose Configure details. On the Configure rule details screen, in the Name field, type a name for the rule.

  10. In the Description field, enter a brief description for your rule, for example, Log whenever an Auto Scaling group launches or terminates.

  11. If you're satisfied with the rule, choose Create rule.

Step 3: Test Your Amazon CloudWatch Events Rule with an Auto Scaling Group

You can test your rule by launching or terminating an Auto Scaling group using the Amazon EC2 console. After waiting a few minutes for the action to be logged, check your AWS Lambda in the CloudWatch console to verify that your Lambda function was invoked.

To test your CloudWatch Events rule with an Auto Scaling group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

    For more information about how to launch an instance, see Launch Your Instance in the Amazon EC2 User Guide for Linux Instances.

  2. Create an Auto Scaling group and let it launch an Amazon EC2 instance for you. For more information about Auto Scaling groups, see Launch Your Instance in the Auto Scaling User Guide.

  3. In the navigation pane, under Metrics, choose Lambda to view the metrics generated by your Lambda function.

  4. To view the output from your function, in the navigation pane, choose Logs, and then in the Log Groups list, select the /aws/lambda log group that contains the data.

  5. Under Log Streams, select a log stream to view the data about the Auto Scaling group that you launched or terminated.