Find out what EC2 Spot Instances are currently available #7021
Replies: 1 comment
|
There is no EC2 API that reliably answers “does this Spot pool have capacity right now?” without making a launch request. A Spot pool is an instance type in one Availability Zone, and its capacity can change between a describe call and the launch. For this case, send the choices in one EC2 Fleet request instead of creating sequential Spot Instance Requests. AWS recommends an List<FleetLaunchTemplateOverridesRequest> overrides = spotInstanceTypes.stream()
.map(type -> FleetLaunchTemplateOverridesRequest.builder()
.instanceType(type)
.subnetId(subnetId)
.build())
.toList();
FleetLaunchTemplateConfigRequest config = FleetLaunchTemplateConfigRequest.builder()
.launchTemplateSpecification(t -> t
.launchTemplateId(launchTemplateId)
.version("$Latest"))
.overrides(overrides)
.build();
CreateFleetRequest request = CreateFleetRequest.builder()
.type(FleetType.INSTANT)
.targetCapacitySpecification(t -> t
.totalTargetCapacity(1)
.defaultTargetCapacityType(DefaultTargetCapacityType.SPOT))
.spotOptions(o -> o
.allocationStrategy(SpotAllocationStrategy.PRICE_CAPACITY_OPTIMIZED))
.launchTemplateConfigs(config)
.build();
CreateFleetResponse response = ec2.createFleet(request);For better availability, add overrides for the allowed subnets/AZs as well as several compatible instance types. The response is synchronous for an instant fleet and contains both the launched instances and per-pool errors, so there is no request-status polling loop. AWS documents this exact use case here: Configure an EC2 Fleet of type instant. It specifically recommends I compiled the request above against AWS SDK for Java 2.41.24. The launch template still needs the AMI, security groups, IAM profile, and other settings required by your workload. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello,
I am working on trying to launch EC2 spot instances automatically from Java, I have a list of instance types and I am trying to launch a spot instance among those.
Currently it's possible that the spot instance type that I choose to try and launch is currently not available, so my request could either get CLOSED, FAILED or have a status containing "capacity-not-available". The way that I am currently doing this is looping over my list of instances and I'm checking for those statuses manually and if one of those happens then I try for the next instance type.
I want to know if there is a better way of doing this last part, is there a way for me to find out which spot instance type has capacity available at the moment without trying to create it first? Preferably not using any APIs that are severely rate limited.
A sample similar to what I am doing is below:
And a section of the monitoring method:
All reactions