Auto Scaling
Developer Guide (API Version 2011-01-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Auto Scaling With Email Notifications

You want to know right away when your Auto Scaling group changes—that is, when Auto Scaling launches or terminates instances in your group.

Starting with API version 2011-01-01, you can use an Amazon Simple Notification Service (Amazon SNS)-backed Auto Scaling feature that sends notifications each time specified events take place. To enable this feature, you will need the following:

  • An Amazon Resource Name (ARN), which you generate when you create an Amazon Simple Notification Service (Amazon SNS) topic. An endpoint, such as an email address, must be subscribed to the topic in order for the endpoint to receive messages published to the topic. In this section, we assume that you already have created an Amazon SNS topic and that you have an ARN. For more information, see Create a Topic in the Amazon Simple Notification Service Getting Started Guide.

  • An Auto Scaling Group, which you created when you went through Basic Auto Scaling Configuration.

  • A notification configuration. You configure an Auto Scaling group to send notifications when specified events take place by calling the as-put-notification-configuration CLI command or the PutNotificationConfiguration API. We will discuss the steps in setting up a notification configuration in the topic Set Up the Notification Configuration later in this section. For more information about the command, go to PutNotificationConfiguration in the Auto Scaling API Reference.

  • A list of Auto Scaling notification types, which are events that will cause the notification to be sent. The following table lists the available notification types:

    Notification typeEvent

    autoscaling:EC2_INSTANCE_LAUNCH

    Successful instance launch by Auto Scaling.

    autoscaling:EC2_INSTANCE_LAUNCH_ERROR

    Failed instance launch by Auto Scaling.

    autoscaling:EC2_INSTANCE_TERMINATE

    Successful instance termination by Auto Scaling.

    autoscaling:EC2_INSTANCE_TERMINATE_ERROR

    Failed instance termination by Auto Scaling.

    autoscaling:TEST_NOTIFICATION

    Validate a configured SNS topic (as a result of calling PutNotificationConfiguration action)

    For the most updated list of notification types, use the as-describe-auto-scaling-notification-types CLI command or the DescribeAutoScalingNotificationTypes API. For more information, go to the DescribeAutoScalingNotificationTypes in the Auto Scaling API Reference.

    For information on troubleshooting EC2_INSTANCE_LAUNCH_ERROR, see Troubleshooting Auto Scaling: Amazon EC2 Instance Launch Failure.

In this section, we set up SNS to send email notifications. When Auto Scaling launches instances to reach or maintain desired capacity, as specified in your Auto Scaling group, SNS sends a notification to your email address with information about the instances. You can check your email Inbox for this information, then run as-describe-auto-scaling-group to get information about current instances in the Auto Scaling group and confirm that the instances listed in your email actually exist.

Tools You Will Use

We walk you through the steps of the basic scenario using the command line tools. You can also use the API actions that correspond to the command line calls. For more information, go to the Auto Scaling API Reference.

You will be using the following command line tool commands.

CommandDescription

as-put-notification-configuration

Configures an Auto Scaling group to send notifications when specified events take place.

as-describe-notification-configurations

Returns a list of notification actions associated with Auto Scaling groups for specified events.

as-describe-auto-scaling-notification-types

Returns a list of all notification types that are supported by Auto Scaling.

as-delete-notification-configuration

Deletes notifications created by PutNotificationConfiguration.

as-delete-auto-scaling-group

Deletes the specified Auto Scaling group.

as-set-desired-capacity

Sets the desired capacity of the Auto Scaling group.

Set Up the Notification Configuration

After you've created your Amazon SNS topic and you have the ARN, you are ready to set up the notification configuration. To configure your Auto Scaling group to send notifications when specified events take place, use as-put-notification-configuration.

The as-put-notification-configuration command takes the following arguments:

as-put-notification-configuration AutoScalingGroupName --notification-types value --topic-arn topic-ARN [General Options]

You need to specify the Auto Scaling group name, the ARN, and the notification types.

For this example, specify:

  • Auto Scaling group name: MyGroup

  • ARN:arn:placeholder:MyTopic

    Note

    ARNs are unique identifiers for Amazon Web Services (AWS) resources. Replace the ARN placeholder with your ARN.

  • Notification types: autoscaling:EC2_Instance_Launch, autoscaling:EC2_Instance_Terminate

Open a command prompt and enter the as-put-notification-configuration command.

as-put-notification-configuration MyGroup --topic-arn arn:placeholder:MyTopic --notification-types autoscaling:EC2_INSTANCE_LAUNCH, autoscaling:EC2_INSTANCE_TERMINATE

Auto Scaling returns the following:

OK-Put Notification Configuration

You now have a notification configuration that sends a notification to the endpoint subscribed in the arn:placeholder:MyTopic ARN whenever instances are launched or terminated.

Verify Notification Configuration

To verify the notification actions associated with your Auto Scaling group when specified events take place, use as-describe-notification-configurations.

The as-describe-notification-configurations command takes the following arguments:

as-describe-notification-configurations [--auto-scaling-groups value[,value...]] [--maxrecords value] [General Options]

If you specify the Auto Scaling group, this command returns a full list of all notification configuration for the Auto Scaling group listed. If you don't provide an Auto Scaling group name, the service returns the full details of all Auto Scaling groups. The command also returns a token if there are more pages to retrieve. To get the next page, call this action again with the returned token as the next-token argument. For this example, specify:

  • Auto Scaling group name: MyGroup

Open a command prompt and enter the as-describe-notification-configurations command.

as-describe-notification-configurations --auto-scaling-groups MyGroup -headers

Auto Scaling returns the following:

NOTIFICATION-CONFIG  GROUP-NAME  TOPIC-ARN  NOTIFICATION-TYPE-NAME
NOTIFICATION-CONFIG  MyGroup  arn:placeholder:MyTopic  autoscaling:EC2_INSTANCE_LAUNCH
NOTIFICATION-CONFIG  MyGroup  arn:placeholder:MyTopic  autoscaling:EC2_INSTANCE_TERMINATE

You have confirmed that you have a notification configuration set up for the MyGroup Auto Scaling group.

Update and Generate Notifications

To cause the changes that will generate notifications, let's update the Auto Scaling group by changing the desired capacity of the MyGroup Auto Scaling group from 1 instance to 5 instances. When Auto Scaling launches the EC2 instances to the new desired capacity, SNS will send an email notification.

For this example, you will use as-set-desired-capacity command to change the desired capacity of the Auto Scaling group, and the as-describe-auto-scaling-groups to verify the instances of your Auto Scaling group.

The as-set-desired-capacity command takes the following arguments:

as-set-desired-capacity AutoScalingGroupName --desired-capacity value [--honor-cooldown|no-honor-cooldown] [General Options]

To use as-set-desired-capacity, you must specify the Auto Scaling group name (MyGroup) and the new number of instances as the desired capacity (5).

Open a command prompt and enter the commands.

as-set-desired-capacity MyGroup --desired-capacity 5

Auto Scaling returns the following:

OK-Desired Capacity Set

Within a few minutes of calling as-set-desired-capacity, you should get an email notification that instances for MyGroup were launched. When you receive this notification, you can confirm it by using as-describe-auto-scaling-groups and specifying MyGroup as the Auto Scaling group name.

Delete Notification Configuration

Here's how you clean up when you're done: Delete the notification configuration, which we discuss in this section, and then delete the Auto Scaling group, which we discuss in the next section.

To delete the notification configuration, use as-delete-notification-configuration. The as-delete-notification-configuration command takes the following arguments:

as-delete-notification-configuration AutoScalingGroupNames --topic-ARN value [General Options]

Both the Auto Scaling group name and ARN are required. For this example, specify:

  • Auto Scaling group name: MyGroup

  • ARN:arn:placeholder:MyTopic

Open a command prompt and enter the command:

as-delete-notification-configuration MyGroup --topic-ARN arn:placeholder:MyTopic 

After confirming that you want to delete the notification configuration, Auto Scaling returns the following:

OK-Deleted Notification Configuration

Delete Auto Scaling Group

To delete the Auto Scaling group, use as-delete-auto-scaling-group. Starting with API version 2011-01-01, you can use the new --force-delete argument to delete the Auto Scaling group and terminate all instances associated with it with one call. Previously, you would perform these two tasks by setting the desired capacity to zero (0), waiting until all instances are deleted, and then deleting the Auto Scaling group.

The as-delete-auto-scaling-group command takes the following arguments:

as-delete-auto-scaling-group AutoScalingGroupNames [--force-delete] [General Options]

In this example, we will use the optional --force-delete argument, so specify:

  • Auto Scaling group name: MyGroup

  • --force-delete

Open a command prompt and enter the commands:

as-delete-auto-scaling-group MyGroup --force-delete

After confirming that you want to delete the Auto Scaling group, Auto Scaling returns the following:

OK-Deleted Auto Scaling group

You have just deleted your notification configuration, instances, and Auto Scaling group. All that's left is the launch configuration you created in the Create a Launch Configuration section. To delete launch configurations, use the as-delete-launch-config CLI command with the launch configuration name.

The command looks like this:

as-delete-launch-config MyLC

For information on how to delete the SNS topic, go to DeleteTopic in the Amazon Simple Notification Service Getting Started Guide.

Tasks Completed

You just performed the following tasks:

  • Set up a notification configuration

  • Updated and generated notifications

  • Deleted a notification configuration

  • Deleted an Auto Scaling group

Following is the complete snippet used to perform these tasks. You can copy the snippet, replace the values with your own, and use the code to get started.

as-put-notification-configuration MyGroup --topic-arn arn:placeholder:MyTopic --notification-types autoscaling:EC2_INSTANCE_LAUNCH, autoscaling:EC2_INSTANCE_TERMINATE
as-describe-notification-configurations MyGroup -headers
as-set-desired-capacity MyGroup --desired-capacity 5  
as-describe-auto-scaling-groups MyGroup
as-delete-notification-configuration MyGroup --topic-ARN arn:placeholder:MyTopic 
as-delete-auto-scaling-group MyGroup --force-delete