Can EC2 Auto Scaling Launch Into Capacity Reservation Groups?
Yes, Amazon EC2 Auto Scaling groups can be configured to launch instances exclusively into specific Capacity Reservation resource groups through their launch template settings and capacity distribution policies. By combining targeted On-Demand Capacity Reservations grouped via AWS Resource Groups with the appropriate Auto Scaling capacity reservation preference, organizations can guarantee that scaling activities only succeed when reserved compute capacity is available. If the specified Capacity Reservation group runs out of available instances, the Auto Scaling group can either wait for capacity to free up or fall back to standard On-Demand capacity, depending on how the launch template and Auto Scaling group parameters are defined.
Understanding On-Demand Capacity Reservations and Resource Groups
An On-Demand Capacity Reservation enables you to reserve compute capacity for your Amazon EC2 instances in a specific Availability Zone for any duration. To manage multiple reservations as a single logical unit, AWS allows you to group them using AWS Resource Groups.
When you create a Capacity Reservation resource group, you can pool reservations across different Availability Zones or instance types. Rather than targeting a single reservation ID, an EC2 launch template can reference the Amazon Resource Name (ARN) of the resource group.
There are two primary types of Capacity Reservation eligibility:
- Open: Any instance launched with matching attributes (instance type, platform, and Availability Zone) automatically runs in the reservation.
- Targeted: Only instances that explicitly specify the Capacity Reservation ID or belong to a launch configuration targeting a Capacity Reservation resource group can run in the reservation.
To ensure exclusive launching into your designated reservations, the reservations inside the resource group should be configured as Targeted, and the launch template must explicitly target that group.
How to Configure Launch Templates for Reservation Groups
Amazon EC2 Auto Scaling groups rely on launch templates to define instance configuration details, including Capacity Reservation targeting. Within the launch template, under the advanced details for Capacity Reservation, you can specify the target preference:
- Open: Launches instances into any open Capacity Reservation that has matching attributes.
- None: Prevents instances from using any Capacity Reservation, running them as standard On-Demand instances instead.
- Target by ID: Launches instances into a single specified Capacity Reservation.
- Target by group: Launches instances into any Capacity Reservation with matching attributes inside a selected Capacity Reservation resource group.
Enforcing Exclusive Launches vs. Fallback Behavior
When you configure a launch template to target a specific Capacity Reservation group, EC2 attempts to fulfill the launch request using the unused capacity in that group. However, whether the Auto Scaling group launches instances exclusively into that reserved capacity—failing to launch if the reservation is full—depends on both the launch template settings and the Auto Scaling group's CapacityReservationPreference setting.
In recent AWS EC2 Auto Scaling configurations, you can define how the group behaves when reserved capacity is exhausted:
- capacity-reservations-only: The Auto Scaling group launches instances strictly into the targeted Capacity Reservations or resource group. If no matching reserved capacity is available in the group, the launch fails and the Auto Scaling group keeps retrying until capacity becomes available.
- capacity-reservations-first: The Auto Scaling group attempts to launch instances into the targeted Capacity Reservation group first. If all reservations in the group are fully utilized, it falls back to launching standard On-Demand instances outside the reservation.
Mathematical Model of Capacity Fulfillment
When an Auto Scaling group scales out to a desired capacity \(D\), the number of instances launched into the targeted reservation group \(L_{res}\) versus standard On-Demand capacity \(L_{od}\) depends on the available reserved capacity \(C_{avail}\) across the matching Availability Zones.
Under the capacity-reservations-only policy, the number
of running instances \(R\) launched by
the scaling action is strictly bounded:
\[L_{res} = \min(D, C_{avail})\]
\[L_{od} = 0\]
If \(D > C_{avail}\), the unfulfilled deficit \(U = D - C_{avail}\) remains pending until additional capacity is added to the resource group or existing instances terminate.
Under the capacity-reservations-first policy, the
deficit overflows into standard On-Demand capacity:
\[L_{res} = \min(D, C_{avail})\]
\[L_{od} = \max(0, D - C_{avail})\]
Step-by-Step Configuration Using AWS CLI
To set up an Auto Scaling group that launches instances exclusively into a specific Capacity Reservation resource group, follow these steps.
Step 1: Create the Capacity Reservation Resource Group
Use the AWS Resource Groups CLI to create a group specifically for Capacity Reservations:
aws resource-groups create-group \
--name MyCapacityReservationGroup \
--configuration '{"Type":"AWS::EC2::CapacityReservationPool"}' \
'{"Type":"AWS::ResourceGroups::Generic","Parameters":[{"Name":"allowed-resource-types","Values":["AWS::EC2::CapacityReservation"]}]}'Step 2: Add Capacity Reservations to the Group
Add your existing targeted Capacity Reservations (by their ARNs) to the newly created resource group:
aws resource-groups group-resources \
--group MyCapacityReservationGroup \
--resource-arns arn:aws:ec2:us-east-1:123456789012:capacity-reservation/cr-0123456789abcdef0Step 3: Create the Launch Template Targeting the Group
Create an EC2 launch template that specifies the
CapacityReservationResourceGroupArn in the
CapacityReservationTarget block:
aws ec2 create-launch-template \
--launch-template-name MyExclusiveCRTemplate \
--version-description "Targets MyCapacityReservationGroup" \
--launch-template-data '{
"ImageId": "ami-0abcdef1234567890",
"InstanceType": "m5.large",
"CapacityReservationSpecification": {
"CapacityReservationTarget": {
"CapacityReservationResourceGroupArn": "arn:aws:resource-groups:us-east-1:123456789012:group/MyCapacityReservationGroup"
}
}
}'Step 4: Configure the Auto Scaling Group for Exclusive Reservation Usage
Create or update the Auto Scaling group using the launch template and
set the CapacityReservationSpecification (or
InstanceMaintenancePolicy / capacity distribution settings
depending on your SDK version) to strictly use Capacity
Reservations:
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name MyExclusiveCRAutoScalingGroup \
--launch-template LaunchTemplateName=MyExclusiveCRTemplate,Version='$Latest' \
--min-size 2 \
--max-size 10 \
--desired-capacity 4 \
--availability-zones us-east-1a us-east-1b \
--capacity-reservation-specification '{"CapacityReservationPreference": "capacity-reservations-only"}'Key Requirements and Limitations
When implementing this architecture, keep the following operational constraints in mind:
- Attribute Matching: The instance type, platform (OS), tenancy, and Availability Zone configured in the Auto Scaling group and launch template must exactly match the attributes of the Capacity Reservations inside the resource group.
- Mixed Instances Policy: If your Auto Scaling group uses a mixed instances policy with multiple instance types, you must ensure the Capacity Reservation resource group contains matching reservations for each instance type override, or specify separate launch template overrides per instance type.
- Availability Zone Alignment: If an Auto Scaling
group spans three Availability Zones (e.g.,
us-east-1a,us-east-1b,us-east-1c), but the Capacity Reservation group only has available capacity inus-east-1aandus-east-1b, scaling attempts inus-east-1cwill fail when usingcapacity-reservations-only, triggering Auto Scaling to rebalance or retry in the zones with available reserved capacity.