Creates a Spot Fleet request.
The Spot Fleet request specifies the total target capacity and the On-Demand target capacity. Amazon EC2 calculates the difference between the total capacity and On-Demand capacity, and launches the difference as Spot capacity.
You can submit a single request that includes multiple launch specifications that vary by instance type, AMI, Availability Zone, or subnet.
By default, the Spot Fleet requests Spot Instances in the Spot Instance pool where the price per unit is the lowest. Each launch specification can include its own instance weighting that reflects the value of the instance type to your application workload.
Alternatively, you can specify that the Spot Fleet distribute the target capacity across the Spot pools included in its launch specifications. By ensuring that the Spot Instances in your Spot Fleet are in different Spot pools, you can improve the availability of your fleet.
You can specify tags for the Spot Fleet request and instances launched by the fleet. You cannot tag other resource types in a Spot Fleet request because only the spot-fleet-request and instance resource types are supported.
For more information, see Spot Fleet requests in the Amazon EC2 User Guide.
We strongly discourage using the RequestSpotFleet API because it is a legacy API with no planned investment. For options for requesting Spot Instances, see Which is the best Spot request method to use? in the Amazon EC2 User Guide.
Parameters:
-
dry_run
(Optional[Boolean], default:
None
)
–
Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.
-
spot_fleet_request_config
(Optional[SpotFleetRequestConfigData], default:
None
)
–
The configuration for the Spot Fleet request.
Raises:
Examples:
To request a Spot fleet in the subnet with the lowest price
This example creates a Spot fleet request with two launch specifications that differ only by subnet. The Spot fleet launches the instances in the specified subnet with the lowest price. If the instances are launched in a default VPC, they receive a public IP address by default. If the instances are launched in a nondefault VPC, they do not receive a public IP address by default. Note that you can't specify different subnets from the same Availability Zone in a Spot fleet request.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.04', 'TargetCapacity': 2, 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'SecurityGroups': [{'GroupId': 'sg-1a2b3c4d'}], 'InstanceType': 'm3.medium', 'SubnetId': 'subnet-1a2b3c4d, subnet-3c4d5e6f', 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}}]})
To request a Spot fleet in the Availability Zone with the lowest price
This example creates a Spot fleet request with two launch specifications that differ only by Availability Zone. The Spot fleet launches the instances in the specified Availability Zone with the lowest price. If your account supports EC2-VPC only, Amazon EC2 launches the Spot instances in the default subnet of the Availability Zone.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.04', 'TargetCapacity': 2, 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'SecurityGroups': [{'GroupId': 'sg-1a2b3c4d'}], 'InstanceType': 'm3.medium', 'Placement': {'AvailabilityZone': 'us-west-2a, us-west-2b'}, 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}}]})
To launch Spot instances in a subnet and assign them public IP addresses
This example assigns public addresses to instances launched in a nondefault VPC. Note that when you specify a network interface, you must include the subnet ID and security group ID using the network interface.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.04', 'TargetCapacity': 2, 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'InstanceType': 'm3.medium', 'NetworkInterfaces': [{'DeviceIndex': 0, 'SubnetId': 'subnet-1a2b3c4d', 'Groups': ['sg-1a2b3c4d'], 'AssociatePublicIpAddress': True}], 'IamInstanceProfile': {'Arn': 'arn:aws:iam::880185128111:instance-profile/my-iam-role'}}]})
To request a Spot fleet using the diversified allocation strategy
This example creates a Spot fleet request that launches 30 instances using the diversified allocation strategy. The launch specifications differ by instance type. The Spot fleet distributes the instances across the launch specifications such that there are 10 instances of each type.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.70', 'TargetCapacity': 30, 'AllocationStrategy': 'diversified', 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'InstanceType': 'c4.2xlarge', 'SubnetId': 'subnet-1a2b3c4d'}, {'ImageId': 'ami-1a2b3c4d', 'InstanceType': 'm3.2xlarge', 'SubnetId': 'subnet-1a2b3c4d'}, {'ImageId': 'ami-1a2b3c4d', 'InstanceType': 'r3.2xlarge', 'SubnetId': 'subnet-1a2b3c4d'}]})
Source code in docs_src/capo_ec2/_services/ec2.py
| def request_spot_fleet(
self,
*,
config_overrides: Optional[EC2ClientConfig] = None,
dry_run: Optional["capo_ec2.types.boolean.Boolean"] = None,
spot_fleet_request_config: Optional[
"capo_ec2.types.spot_fleet_request_config_data.SpotFleetRequestConfigData"
] = None,
) -> "capo_ec2.types.request_spot_fleet_response.RequestSpotFleetResponse":
"""<p>Creates a Spot Fleet request.</p> <p>The Spot Fleet request specifies the total target capacity and the On-Demand target capacity. Amazon EC2 calculates the difference between the total capacity and On-Demand capacity, and launches the difference as Spot capacity.</p> <p>You can submit a single request that includes multiple launch specifications that vary by instance type, AMI, Availability Zone, or subnet.</p> <p>By default, the Spot Fleet requests Spot Instances in the Spot Instance pool where the price per unit is the lowest. Each launch specification can include its own instance weighting that reflects the value of the instance type to your application workload.</p> <p>Alternatively, you can specify that the Spot Fleet distribute the target capacity across the Spot pools included in its launch specifications. By ensuring that the Spot Instances in your Spot Fleet are in different Spot pools, you can improve the availability of your fleet.</p> <p>You can specify tags for the Spot Fleet request and instances launched by the fleet. You cannot tag other resource types in a Spot Fleet request because only the <code>spot-fleet-request</code> and <code>instance</code> resource types are supported.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet-requests.html">Spot Fleet requests</a> in the <i>Amazon EC2 User Guide</i>.</p> <important> <p>We strongly discourage using the RequestSpotFleet API because it is a legacy API with no planned investment. For options for requesting Spot Instances, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-best-practices.html#which-spot-request-method-to-use">Which is the best Spot request method to use?</a> in the <i>Amazon EC2 User Guide</i>.</p> </important>
Args:
dry_run: <p>Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is <code>DryRunOperation</code>. Otherwise, it is <code>UnauthorizedOperation</code>.</p>
spot_fleet_request_config: <p>The configuration for the Spot Fleet request.</p>
Raises:
capo_ec2.errors.UnknownServiceError: The service returned an error code this client does not model.
Examples:
To request a Spot fleet in the subnet with the lowest price
This example creates a Spot fleet request with two launch specifications that differ only by subnet. The Spot fleet launches the instances in the specified subnet with the lowest price. If the instances are launched in a default VPC, they receive a public IP address by default. If the instances are launched in a nondefault VPC, they do not receive a public IP address by default. Note that you can't specify different subnets from the same Availability Zone in a Spot fleet request.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.04', 'TargetCapacity': 2, 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'SecurityGroups': [{'GroupId': 'sg-1a2b3c4d'}], 'InstanceType': 'm3.medium', 'SubnetId': 'subnet-1a2b3c4d, subnet-3c4d5e6f', 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}}]})
To request a Spot fleet in the Availability Zone with the lowest price
This example creates a Spot fleet request with two launch specifications that differ only by Availability Zone. The Spot fleet launches the instances in the specified Availability Zone with the lowest price. If your account supports EC2-VPC only, Amazon EC2 launches the Spot instances in the default subnet of the Availability Zone.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.04', 'TargetCapacity': 2, 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'SecurityGroups': [{'GroupId': 'sg-1a2b3c4d'}], 'InstanceType': 'm3.medium', 'Placement': {'AvailabilityZone': 'us-west-2a, us-west-2b'}, 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}}]})
To launch Spot instances in a subnet and assign them public IP addresses
This example assigns public addresses to instances launched in a nondefault VPC. Note that when you specify a network interface, you must include the subnet ID and security group ID using the network interface.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.04', 'TargetCapacity': 2, 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'InstanceType': 'm3.medium', 'NetworkInterfaces': [{'DeviceIndex': 0, 'SubnetId': 'subnet-1a2b3c4d', 'Groups': ['sg-1a2b3c4d'], 'AssociatePublicIpAddress': True}], 'IamInstanceProfile': {'Arn': 'arn:aws:iam::880185128111:instance-profile/my-iam-role'}}]})
To request a Spot fleet using the diversified allocation strategy
This example creates a Spot fleet request that launches 30 instances using the diversified allocation strategy. The launch specifications differ by instance type. The Spot fleet distributes the instances across the launch specifications such that there are 10 instances of each type.
>>> client.request_spot_fleet(spot_fleet_request_config={'SpotPrice': '0.70', 'TargetCapacity': 30, 'AllocationStrategy': 'diversified', 'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role', 'LaunchSpecifications': [{'ImageId': 'ami-1a2b3c4d', 'InstanceType': 'c4.2xlarge', 'SubnetId': 'subnet-1a2b3c4d'}, {'ImageId': 'ami-1a2b3c4d', 'InstanceType': 'm3.2xlarge', 'SubnetId': 'subnet-1a2b3c4d'}, {'ImageId': 'ami-1a2b3c4d', 'InstanceType': 'r3.2xlarge', 'SubnetId': 'subnet-1a2b3c4d'}]})
"""
def _handler(
req: "OperationRequest[capo_ec2.types.request_spot_fleet_request.RequestSpotFleetRequest]",
) -> OperationResponse[
"capo_ec2.types.request_spot_fleet_response.RequestSpotFleetResponse"
]:
import capo_ec2._operations.amazon_ec2.request_spot_fleet
output, http_response = (
capo_ec2._operations.amazon_ec2.request_spot_fleet.request_spot_fleet(
req.options, req.input
)
)
return OperationResponse(output=output, response=http_response)
def _redact_input(
input_: "capo_ec2.types.request_spot_fleet_request.RequestSpotFleetRequest",
) -> dict[str, Any]:
import capo_ec2.types.request_spot_fleet_request
return capo_ec2.types.request_spot_fleet_request.redact_sensitive(input_)
interceptors_, options_ = self.operation_options(
config_overrides,
OperationLogging("RequestSpotFleet", redact_input=_redact_input),
)
input_: capo_ec2.types.request_spot_fleet_request.RequestSpotFleetRequest = {}
if dry_run is not None:
input_["dry_run"] = dry_run
if spot_fleet_request_config is not None:
input_["spot_fleet_request_config"] = spot_fleet_request_config
response = execute_pipeline(
OperationRequest(input=input_, options=options_),
handler=_handler,
interceptors=list(interceptors_),
)
response.response.close()
return response.output
|