New capacity-optimized allocation strategy for EC2 Auto Scaling and EC2 Fleet.
resource "aws_autoscaling_group" "example" {
mixed_instances_policy {
instances_distribution {
spot_allocation_strategy = "capacity-optimized"
}
}
}
resource "aws_ec2_fleet" "example" {
spot_option {
allocation_strategy = "capacityOptimized"
}
}
resource "aws_spot_fleet_request" "example" {
allocation_strategy = "capacityOptimized"
}
In the interim, I've tried capacity-optimized in ASG and seems to be working well (no changes required).
From what I've checked so far there are differences in the implementation. EC2 Fleet, Spot Fleet and Auto Scaling Groups take slightly different approach.
Auto Scaling Groups, just pass the allocation strategy string through and does not validate the schema. For anyone applying it to an existing ASG config, bear in mind that spot_instance_pools is not compatible with capacity-optimized (link to documentation), meaning you will need to remove the spot_instance_pools section to make capacity-optimized to work.
For EC2 fleet, as I expected after checking the code, I got a validation error. Changes will be required to support it.
Error: expected spot_options.0.allocation_strategy to be one of [diversified lowestPrice], got capacityOptimized
on ec2_fleet.tf line 22, in resource "aws_ec2_fleet" "aws_fleet_example":
22: resource "aws_ec2_fleet" "aws_fleet_example" {
I haven't had a chance yet to test Spot Fleet, but my understanding is that there is no validation, just a pass-through the allocation strategy, so for as long as the instance_pools_to_use_count is not provided, my guess it it should work.
For other people finding this. The instances_distribution block for my Autoscaling Group looks like this now:
instances_distribution {
on_demand_percentage_above_base_capacity = 0
spot_instance_pools = 0
spot_allocation_strategy = "capacity-optimized"
}
Most helpful comment
For other people finding this. The instances_distribution block for my Autoscaling Group looks like this now: