Amazon Elastic Compute Cloud
User Guide (API Version 2013-02-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...

Launching Spot Instances with Auto Scaling

In this section, we will create an Auto Scaling launch configuration and an Auto Scaling group that launch Spot Instances. We will use the Auto Scaling command line interface (CLI) commands to create the launch configuration and the Auto Scaling group, and to verify and obtain information about them and the Amazon EC2 instances that they launch.

Prerequisites

If you're not familiar with how to create a launch configuration or an Auto Scaling group, we recommend that you go through the steps in the Basic Scenario in Auto Scaling in the Auto Scaling Developer Guide. Use the basic scenario to get started with the infrastructure that you need in most Auto Scaling scenarios.

If you don't have the Auto Scaling CLI tools installed on your computer, you must install them to do this walkthrough. For information, see Auto Scaling Tools You Will Use in the Amazon EC2 User Guide. You can also use the information in Using the Command Line Tools in the Auto Scaling Developer Guide.

In this scenario, we will perform the following tasks:

If you already have a launch configuration and Auto Scaling group, here are other related Spot Instance tasks that you can do using Auto Scaling:

For more information about Auto Scaling, see Understanding Auto Scaling in the Auto Scaling Developer Guide. For information about scenarios using Auto Scaling, see Using Auto Scaling also in the Auto Scaling Developer Guide.

Step 1: Create a Launch Configuration

An Auto Scaling launch configuration is a template that contains the parameters necessary to launch new Amazon EC2 instances. You can attach only one launch configuration to an Auto Scaling group at a time. You have to create a launch configuration and then an Auto Scaling group in order to launch instances with Auto Scaling.

To place bids for Spot Instances, use the as-create-launch-config Auto Scaling CLI command with the --spot-price option. Specify the maximum price you want to pay for an instance. This price will be used by Auto Scaling to bid for your instances, but this price is not necessarily what you pay for the instances when they are launched. You will pay the Spot price. For example, you bid $0.05 for m1.small instances. Your bid gets fulfilled if the current market price for m1.small Spot Instance is $0.03, or any other price below $0.05, and you will be charged the current price of $0.03 per hour. In fact, as long as your current bid is higher than the market price, your bid will be fulfilled and a Spot Instance will be launched for you.

You can get guidance on the price to bid by checking Spot price history. You can do this by using the AWS Management Console, CLI, or API. For more information, go to Viewing Spot Price History.

The as-create-launch-config command takes the following arguments:

as-create-launch-config LaunchConfigurationName --image-id value --instance-type value [--spot-price value] [--iam-instance-profile value] [--block-device-mapping "key1=value1,key2=value2..." ] [--monitoring-enabled|--monitoring-disabled] [--kernel value ] [--key value ] [--ramdisk value] [--group value[,value...] ] [--user-data value] [--user-data-file value] [General Options]

The only required options are the launch configuration name, image ID, and instance type. For this walkthrough, specify:

  • Launch configuration name = spotlc-5cents

    Note

    When Auto Scaling launches instances, it does not distinguish the Spot Instances from the On-Demand instances. To help you identify which of your instances are Spot Instances, consider assigning a launch configuration name that includes spot and the bid price.

  • Image ID = ami-e565ba8c

    The Image ID identifies the Amazon Machine Image (AMI) that will be used to launch the instances. If you don't have an AMI, and you want to find a suitable one, see Amazon Machine Images (AMIs).

  • Instance type = m1.small

  • Spot price = $0.05

    This parameter is optional. If you want to launch Spot Instances, you must specify the Spot bid price that will be used to bid for Spot Instances. Spot Instance bid prices must be specified in US dollars.

Your command should look similar to the following example:

as-create-launch-config spotlc-5cents --image-id ami-e565ba8c --instance-type m1.small --spot-price "0.05"

You should get a confirmation like the following example:

OK-Created launch config

Step 2: Create an Auto Scaling Group

An Auto Scaling group is a collection of Amazon EC2 instances that shares similar characteristics and to which you want to apply certain scaling actions. You can use the Auto Scaling group to automatically scale the number of instances or maintain a fixed number of instances. You can attach only one launch configuration to an Auto Scaling group at a time. You have to create a launch configuration and then an Auto Scaling group in order to launch Amazon EC2 instances with Auto Scaling.

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

as-create-auto-scaling-group AutoScalingGroupName --availability-zones value[,value...] --launch-configuration value --max-size value --min-size value [--default-cooldown value] [--desired-capacity value] [--grace-period value] [--health-check-type value] [--load-balancers value[, value]] [--placement-group value] [--vpc-zone-identifier value] [General Options]

This command requires that you specify a name for your Auto Scaling group, a launch configuration, one or more Availability Zones, a minimum group size, and a maximum group size. The Availability Zones you choose determine the physical location of your Auto Scaling instances. The minimum and maximum group size tells Auto Scaling the minimum and maximum number of instances the Auto Scaling group should have.

Desired capacity is an important component of the as-create-auto-scaling-group command. Although it is an optional parameter, desired capacity tells Auto Scaling the number of instances you want to run initially. To adjust the number of instances you want running in your Auto Scaling group, you change the value of --desired-capacity. If you don't specify --desired-capacity, its value is the same as minimum group size.

For more detailed information on the syntax of the as-create-auto-scaling-group command, see Create an Auto Scaling Group in the Auto Scaling Developer Guide. You can also get help information from the command line: Run the as-create-auto-scaling-group --help. For more information, go to Resources for Using Auto Scaling CLI Tools.

For this walkthrough, specify these values for the command:

  • Auto Scaling Group name = spotasg

    Note

    When Auto Scaling launches instances, it does not distinguish the Spot Instances from the On-Demand instances. To help you identify which of your instances are Spot Instances, consider assigning spot-specific names to the Auto Scaling group that launches Spot Instances.

  • Launch configuration name = spotlc-5cents

  • Availability Zone = us-east-1a,us-east-1b

  • Max size = 5

  • Min size = 1

  • Desired capacity = 3

Your command should look like the following example:

as-create-auto-scaling-group spotasg --launch-configuration spotlc-5cents --availability-zones "us-east-1a,us-east-1b" --max-size 5 --min-size 1 --desired-capacity 3

You should get confirmation like the following example:

OK-Created AutoScalingGroup
   

What do you want to do next?

For more information about Auto Scaling, see Understanding Auto Scaling in the Auto Scaling Developer Guide. For information about scenarios using Auto Scaling, see Using Auto Scaling also in the Auto Scaling Developer Guide.