Reserve capacity in specific Availability Zones with Capacity Reservations
Amazon EC2 On-Demand Capacity Reservations help you reserve compute capacity in specific
Availability Zones. To start using Capacity Reservations, you create a Capacity
Reservation in a specific Availability Zone. Then, you can launch instances into the
reserved capacity, view its capacity utilization in real time, and increase or decrease
its capacity as needed.
Capacity Reservations are configured as either open
or
targeted
. If the Capacity Reservation is open
, all new and
existing instances that have matching attributes automatically run in the capacity of
the Capacity Reservation. If the Capacity Reservation is targeted
,
instances must specifically target it to run in the reserved capacity.
This topic shows how to create an Auto Scaling group that launches On-Demand Instances into
targeted
Capacity Reservations. This gives you more control over when
to use specific Capacity Reservations.
The basic steps are:
-
Create Capacity Reservations in multiple Availability Zones that have the
same instance type, platform, and instance count.
-
Group Capacity Reservations using AWS Resource Groups.
-
Create an Auto Scaling group with a launch template that targets the resource
group, using the same Availability Zones as the Capacity
Reservations.
Step 1: Create the Capacity
Reservations
The first step is to create a Capacity Reservation in each Availability Zone where
your Auto Scaling group will be deployed.
You can only create targeted
reservations when you first create
the Capacity Reservations.
- Console
-
To create your Capacity Reservations
Open the Amazon EC2 console at
https://console.aws.amazon.com/ec2/.
-
Choose Capacity Reservations, and then
choose Create Capacity Reservation.
-
On the Create a Capacity Reservation
page, pay attention to following settings in the
Instance details section. The instance
type, platform, and Availability Zone of the instances that you
launch must match the instance type, platform, and Availability
Zone that you specify here or the Capacity Reservation is not
applied.
-
For Instance type, choose the
type of instance to launch into the reserved
capacity.
-
For Platform, choose the
operating system for your instances.
-
For Availability Zone, choose the
first Availability Zone that you want to reserve
capacity in.
-
For Total capacity, choose the
number of instances you need. Calculate the total number
of instances you need for your Auto Scaling group divided by the
number of Availability Zones you plan to use.
-
Under Capacity Reservation details, for
Capacity Reservation ends, choose one
of the following options:
-
For Instance eligibility, choose
Targeted: Only instances that target the Capacity
Reservation.
-
(Optional) For Tags, specify any tags to
associate with the Capacity Reservation.
-
Choose Create.
-
Note the ID of the newly created Capacity Reservation. You
need it to set up the Capacity Reservation group.
Repeat this procedure for each Availability Zone you want to enable
for your Auto Scaling group, changing only the value of the
Availability Zone option.
- AWS CLI
-
To create your Capacity Reservations
Use the following create-capacity-reservation command to create the
Capacity Reservations. Replace the sample values for
--availability-zone
, --instance-type
,
--instance-platform
, and
--instance-count
.
aws ec2 create-capacity-reservation \
--availability-zone us-east-1a
\
--instance-type c5.xlarge
\
--instance-platform Linux/UNIX
\
--instance-count 3
\
--instance-match-criteria targeted
Example of resulting Capacity Reservation ID
{
"CapacityReservation": {
"CapacityReservationId": "cr-1234567890abcdef1",
"OwnerId": "123456789012",
"CapacityReservationArn": "arn:aws:ec2:us-east-1:123456789012:capacity-reservation/cr-1234567890abcdef1",
"InstanceType": "c5.xlarge",
"InstancePlatform": "Linux/UNIX",
"AvailabilityZone": "us-east-1a",
"Tenancy": "default",
"TotalInstanceCount": 3,
"AvailableInstanceCount": 3,
"EbsOptimized": false,
"EphemeralStorage": false,
"State": "active",
"StartDate": "2023-07-26T21:36:14+00:00",
"EndDateType": "unlimited",
"InstanceMatchCriteria": "targeted",
"CreateDate": "2023-07-26T21:36:14+00:00"
}
}
Note the ID of the newly created Capacity Reservation. You need it to
set up the Capacity Reservation group.
Repeat this command for each Availability Zone you want to enable for
your Auto Scaling group, changing only the value of the
--availability-zone
option.
Step 2: Create a Capacity
Reservation group
When you finish creating the Capacity Reservations, you can group them together
using the AWS Resource Groups service. AWS Resource Groups supports several
different types of groups for different uses. Amazon EC2 uses a special-purpose group,
known as a service-linked resource group, to target a group of Capacity
Reservations. To interact with this service-linked resource group, you can use the
AWS CLI or an SDK but not the console. For more information about service-linked
resource groups, see Service configurations for resource
groups in the AWS Resource Groups User
Guide.
To create a Capacity Reservation group using the AWS CLI
Use the create-group command to create a resource group that can contain
only Capacity Reservations. In this example, the resource group is named
my-cr-group
.
aws resource-groups create-group \
--name my-cr-group
\
--configuration '{"Type":"AWS::EC2::CapacityReservationPool"}' '{"Type":"AWS::ResourceGroups::Generic", "Parameters": [{"Name": "allowed-resource-types", "Values": ["AWS::EC2::CapacityReservation"]}]}'
The following is an example response.
{
"Group": {
"GroupArn": "arn:aws:resource-groups:us-east-1:123456789012:group/my-cr-group",
"Name": "my-cr-group"
},
"GroupConfiguration": {
"Configuration": [
{
"Type": "AWS::EC2::CapacityReservationPool"
},
{
"Type": "AWS::ResourceGroups::Generic",
"Parameters": [
{
"Name": "allowed-resource-types",
"Values": [
"AWS::EC2::CapacityReservation"
]
}
]
}
],
"Status": "UPDATE_COMPLETE"
}
}
Note the ARN of the resource group. You need it to set up the launch template for
your Auto Scaling group.
To associate your Capacity Reservations to the newly created group using the
AWS CLI
Use the following group-resources command to associate the Capacity Reservations to
the newly created Capacity Reservation group. For the
--resource-arns
option, specify the Capacity Reservations using
their ARNs. Construct the ARNs using the relevant Region, your account ID, and
the reservation IDs you noted earlier. In this example, the reservations with
IDs cr-1234567890abcdef1
and
cr-54321abcdef567890
will be
grouped together in the group named
my-cr-group
.
aws resource-groups group-resources \
--group my-cr-group
\
--resource-arns \
arn:aws:ec2:region
:account-id
:capacity-reservation/cr-1234567890abcdef1
\
arn:aws:ec2:region
:account-id
:capacity-reservation/cr-54321abcdef567890
The following is an example response.
{
"Succeeded": [
"arn:aws:ec2:us-east-1:123456789012:capacity-reservation/cr-1234567890abcdef1",
"arn:aws:ec2:us-east-1:123456789012:capacity-reservation/cr-54321abcdef567890"
],
"Failed": [],
"Pending": []
}
For information about modifying or deleting the resource group, see the AWS Resource
Groups API Reference.
Step 3: Create a launch
template
- Console
-
To create a launch template
Open the Amazon EC2 console at
https://console.aws.amazon.com/ec2/.
-
On the navigation pane, under Instances,
choose Launch Templates.
-
Choose Create launch template. Enter a
name and provide a description for the initial version of the
launch template.
-
Under Auto Scaling guidance, select the
check box.
-
Create the launch template. Choose an AMI and instance type
that matches the Capacity Reservations you are planning to use,
and optionally, a key pair, one or more security groups, and any
additional EBS volumes or instance store volumes for your
instances.
-
Expand Advanced details, and do the
following:
-
For Capacity Reservation, choose
Target by group.
-
For Capacity reservation - Target by
group, choose the Capacity Reservations
group that you created in the previous section, and then
choose Save.
-
Choose Create launch template.
-
On the confirmation page, choose Create Auto Scaling
group.
- AWS CLI
-
To create a launch template
Use the following create-launch-template command to create a launch
template that specifies that the Capacity Reservation targets a
specific resource group. Replace the sample value for
--launch-template-name
. Replace
c5.xlarge
with the
instance type that you used in the Capacity Reservation and
ami-0123456789EXAMPLE
with the ID of the AMI that you want to use. Replace
arn:aws:resource-groups:region:account-id:group/my-cr-group
with the ARN of the resource group that you created in the beginning
of the previous section.
aws ec2 create-launch-template \
--launch-template-name my-launch-template
\
--launch-template-data \
'{"InstanceType": "c5.xlarge
",
"ImageId": "ami-0123456789EXAMPLE
",
"CapacityReservationSpecification":
{"CapacityReservationTarget":
{ "CapacityReservationResourceGroupArn": "arn:aws:resource-groups:region
:account-id
:group/my-cr-group
" }
}
}'
The following is an example response.
{
"LaunchTemplate": {
"LaunchTemplateId": "lt-0dd77bd41dEXAMPLE",
"LaunchTemplateName": "my-launch-template",
"CreateTime": "2023-07-26T21:42:48+00:00",
"CreatedBy": "arn:aws:iam::123456789012:user/Bob",
"DefaultVersionNumber": 1,
"LatestVersionNumber": 1
}
}
Step 4: Create an Auto Scaling
group
- Console
-
Create your Auto Scaling group as you usually do, but when you choose your VPC
subnets, choose a subnet from each Availability Zone that matches the
targeted
Capacity Reservations you created. Then, when
your Auto Scaling group launches an On-Demand Instance in one of these
Availability Zones, the instance will be run in the reserved capacity
for that Availability Zone. If the resource group runs out of Capacity
Reservations before your desired capacity is fulfilled, we launch
anything beyond the reserved capacity as regular On-Demand
capacity.
To create a simple Auto Scaling group
Open the Amazon EC2 console at
https://console.aws.amazon.com/ec2/, and choose Auto Scaling Groups from the navigation pane.
-
On the navigation bar at the top of the screen, choose the
same AWS Region that you used when you created the launch
template.
-
Choose Create an Auto Scaling group.
-
On the Choose launch template or
configuration page, for Auto Scaling group
name, enter a name for your Auto Scaling group.
-
For Launch template, choose an existing
launch template.
-
For Launch template version, choose
whether the Auto Scaling group uses the default, the latest, or a
specific version of the launch template when scaling out.
-
On the Choose instance launch
options page, skip the Instance type
requirements section to use the EC2 instance type
that is specified in the launch template.
-
Under Network, for
VPC, choose a VPC. The Auto Scaling group must
be created in the same VPC as the security group you specified
in your launch template. If you didn't specify a security group
in your launch template, you can choose any VPC that has subnets
in the same Availability Zones as your Capacity
Reservations.
-
For Availability Zones and
subnets, choose subnets from each
Availability Zone that you want to include, based on which
Availability Zones your Capacity Reservations are in.
-
Choose Next twice.
-
On the Configure group size and scaling
policies page, for Desired
capacity, enter the initial number of instances
to launch. When you change this number to a value outside of the
minimum or maximum capacity limits, you must update the values
of Minimum capacity or Maximum
capacity. For more information, see Set scaling limits for your Auto Scaling group.
-
Choose Skip to review.
-
On the Review page, choose
Create Auto Scaling group.
- AWS CLI
-
To create a simple Auto Scaling group
Use the following create-auto-scaling-group command and specify the name
and version of your launch template as the value for the
--launch-template
option. Replace the sample values
for --auto-scaling-group-name
, --min-size
,
--max-size
, and --vpc-zone-identifier
.
For the --availability-zones
option, specify the
Availability Zones that you created Capacity Reservations for. For
example, if your Capacity Reservations specify the
us-east-1a
and us-east-1b
Availability
Zones, then you must create your Auto Scaling group in the same zones. Then,
when your Auto Scaling group launches an On-Demand Instance in one of these
Availability Zones, the instance will be run in the reserved capacity
for that Availability Zone. If the resource group runs out of Capacity
Reservations before your desired capacity is fulfilled, we launch
anything beyond the reserved capacity as regular On-Demand
capacity.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name my-asg
\
--launch-template LaunchTemplateName=my-launch-template
,Version='1
' \
--min-size 6
\
--max-size 6
\
--vpc-zone-identifier "subnet-5f46ec3b
,subnet-0ecac448
" \
--availability-zones us-east-1a
us-east-1b
For an example implementation, see the AWS CloudFormation template in the following AWS
samples GitHub repository: https://github.com/aws-samples/aws-auto-scaling-backed-by-on-demand-capacity-reservations/.
The following related topics can be helpful as you learn about Capacity
Reservations.