Skip to content

feat(runners): support OnDemandOptions.AllocationStrategy in EC2 Fleet#5077

Open
piotrj wants to merge 5 commits intogithub-aws-runners:mainfrom
ClipboardHealth:feat/on-demand-allocation-strategy
Open

feat(runners): support OnDemandOptions.AllocationStrategy in EC2 Fleet#5077
piotrj wants to merge 5 commits intogithub-aws-runners:mainfrom
ClipboardHealth:feat/on-demand-allocation-strategy

Conversation

@piotrj
Copy link

@piotrj piotrj commented Mar 24, 2026

Summary

Currently, createInstances always sets SpotOptions.AllocationStrategy regardless of the fleet's targetCapacityType. For on-demand fleets, AWS silently ignores SpotOptions and defaults to lowest-price, making the instance_allocation_strategy variable and instance_types ordering meaningless for on-demand users who want prioritized.

This PR fixes that by conditionally setting SpotOptions or OnDemandOptions based on targetCapacityType, so the allocation strategy is correctly applied for both spot and on-demand fleets.

Why instance_type_priorities?

The prioritized allocation strategy doesn't simply use the array order of launch template overrides — it relies on an explicit Priority field on each override entry. Without setting Priority, all overrides have equal (lowest) priority and prioritized effectively behaves like lowest-price.

We introduced the optional instance_type_priorities variable (map(number), e.g. { "m5.large" = 1, "c5.large" = 2 }) as a non-breaking addition to give users explicit control over the priority assigned to each instance type. When not provided, priorities automatically fall back to the index position in instance_types, so existing configurations continue to work unchanged — the first instance type in the list gets the highest priority (0), the second gets 1, and so on. This makes prioritized work correctly out of the box while still allowing fine-grained control when needed.

Note: The Priority field is only set on fleet overrides when instance_allocation_strategy is "prioritized". For all other allocation strategies, overrides remain unchanged — no Priority is added. This ensures that existing setups using strategies like capacity-optimized or lowest-price are completely unaffected by this change.

What changed

Conditional SpotOptions / OnDemandOptions

  • The createInstances function now checks targetCapacityType and sets SpotOptions (with MaxTotalPrice and AllocationStrategy) for spot fleets, or OnDemandOptions (with AllocationStrategy) for on-demand fleets.
  • "prioritized" is added to the instance_allocation_strategy validation list in all Terraform variable definitions.

Failover strategy mapping

  • When falling back from spot to on-demand, spot-only allocation strategies (e.g. capacity-optimized) are automatically mapped to lowest-price to avoid passing invalid values to OnDemandOptions.

Override priorities

  • generateFleetOverrides sets Priority on each override only when the allocation strategy is prioritized. It uses instance_type_priorities if provided, or the index in instance_types otherwise.
  • The new instance_type_priorities variable is threaded through the full chain: root Terraform variables → runners module → Lambda environment variables (INSTANCE_TYPE_PRIORITIES as JSON) → ec2instanceCriteriagenerateFleetOverrides.

@piotrj piotrj requested review from a team as code owners March 24, 2026 16:02
@piotrj piotrj force-pushed the feat/on-demand-allocation-strategy branch 3 times, most recently from 35e8f73 to 25c0704 Compare March 24, 2026 18:10
piotrj added 5 commits March 24, 2026 19:56
Previously, createInstances always set SpotOptions.AllocationStrategy
even for on-demand fleets, making instance_allocation_strategy and
instance_types ordering meaningless for on-demand users wanting
`prioritized`. Now the fleet request conditionally sets SpotOptions
or OnDemandOptions based on targetCapacityType, and the spot-to-
on-demand failover path maps spot-only strategies to `lowest-price`.
Add optional `instance_type_priorities` variable (map of instance type
to priority number) to control EC2 Fleet override priorities. When not
set, priorities default to the index position in `instance_types`,
preserving the user's ordering. This makes the `prioritized` allocation
strategy work correctly for both spot and on-demand fleets.
Move the prioritized strategy check into generateFleetOverrides itself
rather than having the caller decide what to pass, making the logic
more explicit and self-contained.
@piotrj piotrj force-pushed the feat/on-demand-allocation-strategy branch from 25c0704 to 73c147f Compare March 24, 2026 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant