Menu
Auto Scaling
User Guide

Launching Spot Instances in Your Auto Scaling Group

Spot instances are a cost-effective choice compared to On-Demand instances, if you can be flexible about when your applications run and if your applications can be interrupted. You can set up Auto Scaling to launch Spot instances instead of On-Demand instances.

Before launching Spot instances using Auto Scaling, we recommend that you become familiar with launching and managing Spot instances using Amazon EC2. For more information, see Spot Instances in the Amazon EC2 User Guide for Linux Instances.

Here's how Spot instances work with Auto Scaling:

  • Setting your bid price. When you use Auto Scaling to launch Spot instances, you set your bid price in the launch configuration. You can't use a single launch configuration to launch both On-Demand instances and Spot instances.

  • Changing your bid price. To change your Spot bid price, you must create a launch configuration with the new bid price, and then associate it with your Auto Scaling group. Note that the existing instances continue to run as long as the bid price specified in the launch configuration used for those instances is higher than the current Spot market price.

  • Spot market price and your bid price. If the market price for Spot instances rises above your Spot bid price for a running instance in your Auto Scaling group, Amazon EC2 terminates your instance. If your Spot bid price exactly matches the Spot market price, whether your bid is fulfilled depends on several factors—such as available Spot instance capacity.

  • Maintaining your Spot instances. When your Spot instance is terminated, Auto Scaling attempts to launch a replacement instance to maintain the desired capacity for the group. If the bid price is higher than the market price, then it launches a Spot instance. Otherwise, Auto Scaling keeps trying.

  • Auto Scaling and Spot instance termination. Auto Scaling can terminate or replace Spot instances just as it can terminate or replace On-Demand instances. For more information, see Controlling Which Instances Auto Scaling Terminates During Scale In.

Launching Spot Instances Using the AWS Management Console

To create an Auto Scaling group that launches Spot instances, complete the following tasks:

Create a Launch Configuration

To create a launch configuration

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

  2. On the navigation pane, under Auto Scaling, choose Launch Configurations. If you are new to Auto Scaling, you see a welcome page; choose Create Auto Scaling group.

  3. Choose Create launch configuration.

  4. On the Choose AMI page, select an AMI.

  5. On the Choose Instance Type page, select a hardware configuration for your instance. Choose Next: Configure details.

  6. On the Configure Details page, do the following:

    1. For Name, type a name for your launch configuration. Consider including "Spot" and the bid price in this name.

    2. Select Request Spot Instances. When you select this option, you'll see the current prices for the Availability Zones in the region. For Maximum price, type your bid price.

    3. For Advanced Details, select an IP address type. If you want to connect to an instance in a VPC, you must select an option that assigns a public IP address. If you want to connect to your instance but aren't sure whether you have a default VPC, select Assign a public IP address to every instance.

    4. Choose Skip to review.

  7. On the Review page, choose Edit security groups. Follow the instructions to choose an existing security group, and then choose Review.

  8. On the Review page, notice that the launch configuration details include your bid price. Choose Create launch configuration.

  9. On the Select an existing key pair or create a new key pair page, select one of the listed options. Select the acknowledgment check box, and then choose Create launch configuration.

    Warning

    Do not select Proceed without a key pair if you need to connect to your instance.

Create an Auto Scaling Group

When you create your Auto Scaling group, specify the launch configuration that you just created. Remember that the launch configuration is a template for your instances, and it includes the bid price for your Spot instances.

For more information and directions for creating your Auto Scaling group using the AWS Management Console, see Creating an Auto Scaling Group.

Verify and Check Your Instances

To confirm that Auto Scaling is launching your Spot instances

  1. Select your new Auto Scaling group.

  2. On the Activity History tab, it shows that Auto Scaling successfully launched the Spot instances that you requested.

    Activity History tab.
  3. On the Instances tab, it shows details about your Spot instances. You'll see that Auto Scaling is launching the instances you requested in the Availability Zones that you specified.

(Optional) Get Notifications When the Auto Scaling Group Changes

To set up notifications

  1. Select the Auto Scaling group.

  2. On the Notifications tab, choose Create notification.

  3. Choose create topic, specify the following, and then choose Save:

    • Send a notification to - AutoScalingSpot

    • With these recipients - your email account

    • Whenever instances - One or more of launch, terminate, fail to launch, and fail to terminate

    Create notification for your Spot requests using Auto Scaling.

As soon as your notification topic is created, the email account you specified receives an email confirmation.

(Optional) Update the Bid Price

To update the bid price for the Spot instances

  1. Create a launch configuration with the same specifications as in Create a Launch Configuration, but with a different name and maximum price.

  2. Select your Auto Scaling group.

  3. On the Details tab, choose Edit.

  4. Select the launch configuration that you just created, and then choose Save.

Clean Up

After you're finished using your instances and your Auto Scaling group, it is a good practice to clean up. When you delete an Auto Scaling group, this also deletes all the Spot instances and outstanding Spot bids for the group.

To clean up Auto Scaling group and instances

  1. Select your Auto Scaling group.

  2. Choose Actions, Delete.

  3. When prompted for confirmation, choose Yes, Delete.

Launching Spot Instances Using the AWS CLI

To create an Auto Scaling group that launches Spot instances, complete the following tasks:

Create a Launch Configuration

To place bids for Spot instances using Auto Scaling, specify the maximum price you are willing to pay for an instance by using the --spot-price option with the create-launch-configuration command as follows:

aws autoscaling create-launch-configuration --launch-configuration-name spot-lc-5cents --image-id ami-1a2bc4d --instance-type m1.small --spot-price "0.05"

Create an Auto Scaling Group

Create your Auto Scaling group using the create-auto-scaling-group command with the launch configuration that you just created. The following command launches two Spot instances:

aws autoscaling create-auto-scaling-group --auto-scaling-group-name spot-asg --launch-configuration-name spot-lc-5cents --availability-zones "us-west-2a" "us-west-2b" --max-size 5 --min-size 1 --desired-capacity 2

Verify and Check Your Instances

Use the describe-scaling-activities command as follows to list the activities that Auto Scaling performed for your Auto Scaling group:

aws autoscaling describe-scaling-activities --auto-scaling-group-name spot-asg

If both bids can't be fulfilled initially, the output looks similar to the following example, where one bid is successful and Auto Scaling is waiting for the other bid:

{
    "Activities": [
        {
            "Description": "Placing Spot instance request. Status Reason: Placed Spot instance request: sir-036wjsp9. Waiting for instance(s)",
            "AutoScalingGroupName": "spot-asg",
            "ActivityId": "28189e6b-e14f-4783-8d48-4d03b40b1354",
            "Details": "{\"Availability Zone\":\"us-west-2a\"}",
            "StartTime": "2015-03-01T16:21:41.578Z",
            "Progress": 20,
            "Cause": "At 2015-03-01T16:21:40Z a difference between desired and actual capacity changing the desired capacity, increasing the capacity from 0 to 2.",
            "StatusMessage": "Placed Spot instance request: sir-036wjsp9. Waiting for instance(s)",
            "StatusCode": "WaitingForSpotInstanceId"
        },
        {
            "Description": "Launching a new EC2 instance: i-d95eb0d4",
            "AutoScalingGroupName": "spot-asg",
            "ActivityId": "b987ab02-f7c3-4948-a0bc-5d1449de30ec",
            "Details": "{\"Availability Zone\":\"us-west-2b\"}",
            "StartTime": "2015-03-01T16:21:41.578Z",
            "Progress": 100,
            "EndTime": "2015-03-01T16:29:46Z",
            "Cause": "At 2015-03-01T16:21:40Z a difference between desired and actual capacity changing the desired capacity, increasing the capacity from 0 to 2.",
            "StatusCode": "Successful"
        }
    ]
}

If the output of as-describe-scaling-activities includes Failed activities, check the response for details. For example, it's possible that the AMI ID is no longer valid or that it's incompatible with the instance type that you selected. If no reason is given, check whether your bid price is above the Spot market price for that Availability Zone.

To view information about the instances for your Auto Scaling group, use the describe-auto-scaling-groups command as follows:

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

The following is example output that shows the Auto Scaling launched two instances, as you specified, and they are both running:

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

In addition to using describe-auto-scaling-groups, you can use the describe-auto-scaling-instances command as follows:

aws autoscaling describe-auto-scaling-instances

The following is example output:

{
    "AutoScalingInstances": [
        {
            "AvailabilityZone": "us-west-2a",
            "InstanceId": "i-13d7dc1f",
            "AutoScalingGroupName": "spot-asg",
            "HealthStatus": "HEALTHY",
            "LifecycleState": "InService",
            "LaunchConfigurationName": "spot-lc-5cents"
        },
        {
            "AvailabilityZone": "us-west-2b",
            "InstanceId": "i-d95eb0d4",
            "AutoScalingGroupName": "spot-asg",
            "HealthStatus": "HEALTHY",
            "LifecycleState": "InService",
            "LaunchConfigurationName": "spot-lc-5cents"
        }
    ]
}

(Optional) Get Notifications When the Auto Scaling Group Changes

For information about setting up email notifications in Auto Scaling, see Getting SNS Notifications When Your Auto Scaling Group Scales.

(Optional) Update the Bid Price for the Spot Instances

To update the bid price for Spot instances

  1. Create a launch configuration with the same specifications as before, but with a different name and maximum price, as follows:

    aws autoscaling create-launch-configuration --launch-configuration-name spot-lc-7cents --image-id ami-1a2b3c4d --instance-type m1.small --spot-price "0.07"
  2. Modify your Auto Scaling group to use the new launch configuration, by using the update-auto-scaling-group command as follows:

    aws autoscaling update-auto-scaling-group --auto-scaling-group-name spot-asg --launch-configuration-name spot-lc-7cents
  3. View your changes using the describe-scaling-activities command as follows:

    aws autoscaling describe-scaling-activities --auto-scaling-group-name spot-asg

Clean Up

After you're finished using your instances and your Auto Scaling group, it is a good practice to clean up. Use the delete-auto-scaling-group command as follows with the optional --force-delete parameter, which specifies that the instances that are part of the Auto Scaling group are terminated with the Auto Scaling group, even if they are still running. Otherwise, you must terminate these instances before you can delete your Auto Scaling group.

aws autoscaling delete-auto-scaling-group --auto-scaling-group-name spot-asg --force-delete