Menu
Auto Scaling
User Guide

Getting SNS Notifications 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. Amazon SNS coordinates and manages the delivery or sending of notifications to subscribing clients or endpoints. You can configure Auto Scaling to send an SNS notification whenever your Auto Scaling group scales.

Amazon SNS can deliver notifications as HTTP or HTTPS POST, email (SMTP, either plain-text or in JSON format), or as a message posted to an Amazon SQS queue. For more information, see What Is Amazon SNS in the Amazon Simple Notification Service Developer Guide.

For example, if you configure your Auto Scaling group to use the autoscaling: EC2_INSTANCE_TERMINATE notification type, and your Auto Scaling group terminates an instance, it sends an email notification. This email contains the details of the terminated instance, such as the instance ID and the reason that the instance was terminated.

Tip

If you prefer, you can use Amazon CloudWatch Events to configure a target to invoke a Lambda function when your Auto Scaling group scales or when a lifecycle action occurs. For more information, see Getting CloudWatch Events When Your Auto Scaling Group Scales.

SNS Notifications

Auto Scaling supports sending Amazon SNS notifications when the following events occur:

Notification typeEvent

autoscaling:EC2_INSTANCE_LAUNCH

Successful instance launch

autoscaling:EC2_INSTANCE_LAUNCH_ERROR

Failed instance launch

autoscaling:EC2_INSTANCE_TERMINATE

Successful instance termination

autoscaling:EC2_INSTANCE_TERMINATE_ERROR

Failed instance termination

The message includes the following information:

  • AutoScalingGroupName. The name of the Auto Scaling group.

  • AccountId. The AWS account ID.

  • LifecycleTransition. The lifecycle hook type (for example, autoscaling:EC2_INSTANCE_LAUNCH).

  • LifecycleActionToken. The lifecycle action token.

  • EC2InstanceId. The EC2 instance ID.

  • LifecycleHookName. The name of the lifecycle hook.

  • NotificationMetadata. User-defined information.

Configure Amazon SNS

To use Amazon SNS to send email notifications, you must first create a topic and then subscribe your email addresses to the topic.

Create an Amazon SNS Topic

An SNS topic is a logical access point, a communication channel your Auto Scaling group uses to send the notifications. You create a topic by specifying a name for your topic.

For more information, see Create a Topic in the Amazon Simple Notification Service Developer Guide.

Subscribe to the Amazon SNS Topic

To receive the notifications that your Auto Scaling group sends to the topic, you must subscribe an endpoint to the topic. In this procedure, for Endpoint, specify the email address where you want to receive the notifications from Auto Scaling.

For more information, see Subscribe to a Topic in the Amazon Simple Notification Service Developer Guide.

Confirm Your Amazon SNS Subscription

Amazon SNS sends a confirmation email to the email address you specified in the previous step.

Make sure you open the email from AWS Notifications and choose the link to confirm the subscription before you continue with the next step.

You will receive an acknowledgement message from AWS. Amazon SNS is now configured to receive notifications and send the notification as an email to the email address that you specified.

Configure Your Auto Scaling Group to Send Notifications

You can configure your Auto Scaling group to send notifications to Amazon SNS when a scaling event, such as launching instances or terminating instances, takes place. Amazon SNS sends a notification with information about the instances to the email address that you specified.

To configure Amazon SNS notifications for your Auto Scaling group using the console

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

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

  3. Select your Auto Scaling group.

  4. On the Notifications tab, choose Create notification.

  5. On the Create notifications pane, do the following:

    1. For Send a notification to:, select your SNS topic.

    2. For Whenever instances, select the events to send the notifications for.

    3. Choose Save.

To configure Amazon SNS notifications for your Auto Scaling group

Use the following put-notification-configuration command:

aws autoscaling put-notification-configuration --auto-scaling-group-name my-asg --topic-arn arn --notification-types "autoscaling:EC2_INSTANCE_LAUNCH" "autoscaling:EC2_INSTANCE_TERMINATE"

Test the Notification Configuration

To cause the changes that generate notifications, update the Auto Scaling group by changing the desired capacity of the Auto Scaling group; for example, from 1 instance to 2 instances. After Auto Scaling launches the EC2 instance, you'll receive the email notification with a few minutes.

To change the desired capacity using the console

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

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

  3. Select your Auto Scaling group.

  4. On the Details tab, choose Edit.

  5. For Desired, enter 2.

  6. Choose Save.

To change the desired capacity using the AWS CLI

Use the following set-desired-capacity command:

aws autoscaling set-desired-capacity --auto-scaling-group-name my-asg --desired-capacity 2

Verify That You Received Notification of the Scaling Event

Check your email for a message from Amazon SNS and open the email. After you receive notification of a scaling event for your Auto Scaling group, you can confirm the scaling event by looking at the description of your Auto Scaling group. You'll need information from the notification email, such as the ID of the instance that was launched or terminated.

To verify that your Auto Scaling group has launched new instance using the console

  1. Select your Auto Scaling group.

  2. On the Activity History tab, the Status column shows the current status of your instance. For example, if the notification indicates that an instance has launched, use the refresh button to verify that the status of the launch activity is Successful.

  3. On the Instances tab, you can view the current Lifecycle state of the instance whose ID you received in the notification email. After a new instance starts, its lifecycle state changes to InService.

To verify that your Auto Scaling group has launched new instance using the AWS CLI

Use the following describe-auto-scaling-groups command to confirm that the size of your Auto Scaling group has changed:

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg

The following example output shows that the group has two instances. Check for the instance whose ID you received in the notification email.

{
    "AutoScalingGroups": [
        {
            "AutoScalingGroupARN": "arn",
            "HealthCheckGracePeriod": 0,
            "SuspendedProcesses": [],
            "DesiredCapacity": 2,
            "Tags": [],
            "EnabledMetrics": [],
            "LoadBalancerNames": [],
            "AutoScalingGroupName": "my-asg",
            "DefaultCooldown": 300,
            "MinSize": 1,
            "Instances": [
                {
                    "InstanceId": "i-d95eb0d4",
                    "AvailabilityZone": "us-west-2b",
                    "HealthStatus": "Healthy",
                    "LifecycleState": "InService",
                    "LaunchConfigurationName": "my-lc"
                },
                {
                    "InstanceId": "i-13d7dc1f",
                    "AvailabilityZone": "us-west-2a",
                    "HealthStatus": "Healthy",
                    "LifecycleState": "InService",
                    "LaunchConfigurationName": "my-lc"
                }
            ],
            "MaxSize": 5,
            "VPCZoneIdentifier": null,
            "TerminationPolicies": [
                "Default"
            ],
            "LaunchConfigurationName": "my-lc",
            "CreatedTime": "2015-03-01T16:12:35.608Z",
            "AvailabilityZones": [
                "us-west-2b",
                "us-west-2a"
            ],
            "HealthCheckType": "EC2"
        }
    ]
}

Delete the Notification Configuration

You can delete your Auto Scaling notification configuration at any time.

To delete Auto Scaling notification configuration using the console

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

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

  3. Select your Auto Scaling group.

  4. On the Notifications tab, choose Delete next to the notification.

To delete Auto Scaling notification configuration using the AWS CLI

Use the following delete-notification-configuration command:

aws autoscaling delete-notification-configuration --auto-scaling-group-name my-asg --topic-arn arn:aws:sns:us-west-2:123456789012:my-sns-topic

For information about deleting the Amazon SNS topic associated with your Auto Scaling group, and also deleting all the subscriptions to that topic, see Clean Up in the Amazon Simple Notification Service Developer Guide.