Enable Capacity Rebalancing using the Amazon Web Services Management Console or Amazon CLI - Amazon EC2 Auto Scaling
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Enable Capacity Rebalancing using the Amazon Web Services Management Console or Amazon CLI

You can use the Amazon Web Services Management Console or Amazon CLI to enable Capacity Rebalancing for your Auto Scaling group. Amazon EC2 Auto Scaling attempts to proactively replace the Spot Instances in your group that have received a rebalance recommendation.

Enable Capacity Rebalancing (console)

You can enable or disable Capacity Rebalancing when you create or update an Auto Scaling group.

To enable Capacity Rebalancing for a new Auto Scaling group
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/, and choose Auto Scaling Groups from the navigation pane.

  2. Choose Create Auto Scaling group.

  3. For Step 1: Choose launch template or configuration, enter a name for the Auto Scaling group, choose a launch template, and then choose Next to proceed to the next step.

  4. For Step 2: Choose instance launch options, for Instance type requirements, choose settings to create a mixed instances group. This includes the instance types that it can launch, instance purchase options, and allocation strategies for Spot and On-Demand Instances. By default, these settings are not configured. To configure them, you must select Override launch template. For more information about creating a mixed instances group, see Auto Scaling groups with multiple instance types and purchase options.

  5. Under Network, choose the options as desired. Verify that the subnets you want to use are in different Availability Zones.

  6. Under the Allocation strategies section, choose a Spot allocation strategy. Enable or disable Capacity Rebalancing by selecting or clearing the check box under Capacity Rebalancing. You only see this option when you request a percentage of the Auto Scaling group to be launched as Spot Instances in the Instance purchase options section.

  7. Create the Auto Scaling group.

  8. (Optional) Add lifecycle hooks as needed. For more information, see Add lifecycle hooks to your Auto Scaling group.

To enable or disable Capacity Rebalancing for an existing Auto Scaling group
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/, and choose Auto Scaling Groups from the navigation pane.

  2. Select the check box next to your Auto Scaling group. A split pane opens in the bottom of the page.

  3. On the Details tab, choose Allocation strategies, Edit.

  4. Under the Allocation strategies section, enable or disable Capacity Rebalancing by selecting or clearing the check box under Capacity Rebalancing.

  5. Choose Update.

Enable Capacity Rebalancing (Amazon CLI)

The following examples show how to use the Amazon CLI to enable and disable Capacity Rebalancing.

Use the create-auto-scaling-group or update-auto-scaling-group command with the following parameter:

  • --capacity-rebalance / --no-capacity-rebalance – Boolean value that indicates whether Capacity Rebalancing is enabled.

Before you call the create-auto-scaling-group command, you need the name of a launch template that is configured for use with an Auto Scaling group. For more information, see Create a launch template for an Auto Scaling group.

Note

The following procedures show how to use a configuration file formatted in JSON or YAML. If you use Amazon CLI version 1, you must specify a JSON-formatted configuration file. If you use Amazon CLI version 2, you can specify a configuration file formatted in either YAML or JSON.

To create and configure a new Auto Scaling group
  • Use the following create-auto-scaling-group command to create a new Auto Scaling group and enable Capacity Rebalancing. This command references a JSON file as the sole parameter for your Auto Scaling group.

    aws autoscaling create-auto-scaling-group --cli-input-json file://~/config.json

    If you don't already have a CLI configuration file that specifies a mixed instances policy, create one.

    Add the following line to the top-level JSON object in the configuration file.

    { "CapacityRebalance": true }

    The following is an example config.json file.

    { "AutoScalingGroupName": "my-asg", "DesiredCapacity": 12, "MinSize": 12, "MaxSize": 15, "CapacityRebalance": true, "MixedInstancesPolicy": { "InstancesDistribution": { "OnDemandBaseCapacity": 0, "OnDemandPercentageAboveBaseCapacity": 25, "SpotAllocationStrategy": "price-capacity-optimized" }, "LaunchTemplate": { "LaunchTemplateSpecification": { "LaunchTemplateName": "my-launch-template", "Version": "$Default" }, "Overrides": [ { "InstanceType": "c5.large" }, { "InstanceType": "c5a.large" }, { "InstanceType": "m5.large" }, { "InstanceType": "m5a.large" }, { "InstanceType": "c4.large" }, { "InstanceType": "m4.large" }, { "InstanceType": "c3.large" }, { "InstanceType": "m3.large" } ] } }, "TargetGroupARNs": "arn:aws-cn:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-alb-target-group/943f017f100becff", "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782" }
To create and configure a new Auto Scaling group
  • Use the following create-auto-scaling-group command to create a new Auto Scaling group and enable Capacity Rebalancing. This command references a YAML file as the sole parameter for your Auto Scaling group.

    aws autoscaling create-auto-scaling-group --cli-input-yaml file://~/config.yaml

    Add the following line to your configuration file formatted in YAML.

    CapacityRebalance: true

    The following is an example config.yaml file.

    --- AutoScalingGroupName: my-asg DesiredCapacity: 12 MinSize: 12 MaxSize: 15 CapacityRebalance: true MixedInstancesPolicy: InstancesDistribution: OnDemandBaseCapacity: 0 OnDemandPercentageAboveBaseCapacity: 25 SpotAllocationStrategy: price-capacity-optimized LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateName: my-launch-template Version: $Default Overrides: - InstanceType: c5.large - InstanceType: c5a.large - InstanceType: m5.large - InstanceType: m5a.large - InstanceType: c4.large - InstanceType: m4.large - InstanceType: c3.large - InstanceType: m3.large TargetGroupARNs: - arn:aws-cn:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-alb-target-group/943f017f100becff VPCZoneIdentifier: subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782
To enable Capacity Rebalancing for an existing Auto Scaling group
  • Use the following update-auto-scaling-group command to enable Capacity Rebalancing.

    aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \ --capacity-rebalance
To verify that Capacity Rebalancing is enabled for an Auto Scaling group
  • Use the following describe-auto-scaling-groups command to verify that Capacity Rebalancing is enabled and to view the details.

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

    The following is an example response.

    { "AutoScalingGroups": [ { "AutoScalingGroupName": "my-asg", "AutoScalingGroupARN": "arn", ... "CapacityRebalance": true } ] }
To disable Capacity Rebalancing

Use the update-auto-scaling-group command with the --no-capacity-rebalance option to disable Capacity Rebalancing.

aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \ --no-capacity-rebalance

For more information about Capacity Rebalancing, see Proactively manage Spot Instance lifecycle using the new Capacity Rebalancing feature for EC2 Auto Scaling on the Amazon Compute Blog.

For more information about the EC2 instance rebalance recommendations, see EC2 instance rebalance recommendations in the Amazon EC2 User Guide.

To learn more about lifecycle hooks, see the following resources.

Limitations

  • Amazon EC2 Auto Scaling can replace the instance that receives the rebalance notification only if the instance isn't protected from scale in. However, scale-in protection doesn't prevent termination from a Spot interruption. For more information, see Use instance scale-in protection to control instance termination.

  • Support for Capacity Rebalancing is available in all commercial Amazon Web Services Regions where Amazon EC2 Auto Scaling is available, except for the Middle East (UAE) Region.