Skip to content

request_spot_instances async

request_spot_instances(
    *,
    config_overrides: Optional[AsyncEC2ClientConfig] = None,
    launch_specification: Optional[RequestSpotLaunchSpecification] = None,
    tag_specifications: Optional[TagSpecificationList] = None,
    instance_interruption_behavior: Optional[InstanceInterruptionBehavior] = None,
    dry_run: Optional[Boolean] = None,
    spot_price: Optional[String] = None,
    client_token: Optional[String] = None,
    instance_count: Optional[Integer] = None,
    type: Optional[SpotInstanceType] = None,
    valid_from: Optional[DateTime] = None,
    valid_until: Optional[DateTime] = None,
    launch_group: Optional[String] = None,
    availability_zone_group: Optional[String] = None,
    block_duration_minutes: Optional[Integer] = None,
) -> RequestSpotInstancesResult

Creates a Spot Instance request.

For more information, see Work with Spot Instance in the Amazon EC2 User Guide.

We strongly discourage using the RequestSpotInstances 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:

  • launch_specification (Optional[RequestSpotLaunchSpecification], default: None ) –

    The launch specification.

  • tag_specifications (Optional[TagSpecificationList], default: None ) –

    The key-value pair for tagging the Spot Instance request on creation. The value for ResourceType must be spot-instances-request, otherwise the Spot Instance request fails. To tag the Spot Instance request after it has been created, see CreateTags.

  • instance_interruption_behavior (Optional[InstanceInterruptionBehavior], default: None ) –

    The behavior when a Spot Instance is interrupted. The default is terminate.

  • 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_price (Optional[String], default: None ) –

    The maximum price per unit hour that you are willing to pay for a Spot Instance. We do not recommend using this parameter because it can lead to increased interruptions. If you do not specify this parameter, you will pay the current Spot price.

    If you specify a maximum price, your instances will be interrupted more frequently than if you do not specify this parameter.

  • client_token (Optional[String], default: None ) –

    Unique, case-sensitive identifier that you provide to ensure the idempotency of the request. For more information, see Ensuring idempotency in Amazon EC2 API requests in the Amazon EC2 User Guide.

  • instance_count (Optional[Integer], default: None ) –

    The maximum number of Spot Instances to launch.

    Default: 1

  • type (Optional[SpotInstanceType], default: None ) –

    The Spot Instance request type.

    Default: one-time

  • valid_from (Optional[DateTime], default: None ) –

    The start date of the request. If this is a one-time request, the request becomes active at this date and time and remains active until all instances launch, the request expires, or the request is canceled. If the request is persistent, the request becomes active at this date and time and remains active until it expires or is canceled.

    The specified start date and time cannot be equal to the current date and time. You must specify a start date and time that occurs after the current date and time.

  • valid_until (Optional[DateTime], default: None ) –

    The end date of the request, in UTC format (YYYY-MM-DDTHH:MM:SSZ).

    • For a persistent request, the request remains active until the ValidUntil date and time is reached. Otherwise, the request remains active until you cancel it.

    • For a one-time request, the request remains active until all instances launch, the request is canceled, or the ValidUntil date and time is reached. By default, the request is valid for 7 days from the date the request was created.

  • launch_group (Optional[String], default: None ) –

    The instance launch group. Launch groups are Spot Instances that launch together and terminate together.

    Default: Instances are launched and terminated individually

  • availability_zone_group (Optional[String], default: None ) –

    The user-specified name for a logical grouping of requests.

    When you specify an Availability Zone group in a Spot Instance request, all Spot Instances in the request are launched in the same Availability Zone. Instance proximity is maintained with this parameter, but the choice of Availability Zone is not. The group applies only to requests for Spot Instances of the same instance type. Any additional Spot Instance requests that are specified with the same Availability Zone group name are launched in that same Availability Zone, as long as at least one instance from the group is still active.

    If there is no active instance running in the Availability Zone group that you specify for a new Spot Instance request (all instances are terminated, the request is expired, or the maximum price you specified falls below current Spot price), then Amazon EC2 launches the instance in any Availability Zone where the constraint can be met. Consequently, the subsequent set of Spot Instances could be placed in a different zone from the original request, even if you specified the same Availability Zone group.

    Default: Instances are launched in any available Availability Zone.

  • block_duration_minutes (Optional[Integer], default: None ) –

    Deprecated.

Raises:

Examples:

To create a one-time Spot Instance request This example creates a one-time Spot Instance request for five instances in the specified Availability Zone. If your account supports EC2-VPC only, Amazon EC2 launches the instances in the default subnet of the specified Availability Zone.

>>> await client.request_spot_instances(spot_price='0.03', instance_count=5, type='one-time', launch_specification={'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'SecurityGroupIds': ['sg-1a2b3c4d'], 'InstanceType': 'm3.medium', 'Placement': {'AvailabilityZone': 'us-west-2a'}, 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}})
To create a one-time Spot Instance request
This example command creates a one-time Spot Instance request for five instances in the specified subnet. Amazon EC2 launches the instances in the specified subnet. If the VPC is a nondefault VPC, the instances do not receive a public IP address by default.
>>> await client.request_spot_instances(spot_price='0.050', instance_count=5, type='one-time', launch_specification={'ImageId': 'ami-1a2b3c4d', 'SecurityGroupIds': ['sg-1a2b3c4d'], 'InstanceType': 'm3.medium', 'SubnetId': 'subnet-1a2b3c4d', 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}})
Source code in docs_src/capo_ec2/_services/async_ec2.py
async def request_spot_instances(
    self,
    *,
    config_overrides: Optional[AsyncEC2ClientConfig] = None,
    launch_specification: Optional[
        "capo_ec2.types.request_spot_launch_specification.RequestSpotLaunchSpecification"
    ] = None,
    tag_specifications: Optional[
        "capo_ec2.types.tag_specification_list.TagSpecificationList"
    ] = None,
    instance_interruption_behavior: Optional[
        "capo_ec2.types.instance_interruption_behavior.InstanceInterruptionBehavior"
    ] = None,
    dry_run: Optional["capo_ec2.types.boolean.Boolean"] = None,
    spot_price: Optional["capo_ec2.types.string.String"] = None,
    client_token: Optional["capo_ec2.types.string.String"] = None,
    instance_count: Optional["capo_ec2.types.integer.Integer"] = None,
    type: Optional["capo_ec2.types.spot_instance_type.SpotInstanceType"] = None,
    valid_from: Optional["capo_ec2.types.date_time.DateTime"] = None,
    valid_until: Optional["capo_ec2.types.date_time.DateTime"] = None,
    launch_group: Optional["capo_ec2.types.string.String"] = None,
    availability_zone_group: Optional["capo_ec2.types.string.String"] = None,
    block_duration_minutes: Optional["capo_ec2.types.integer.Integer"] = None,
) -> "capo_ec2.types.request_spot_instances_result.RequestSpotInstancesResult":
    """<p>Creates a Spot Instance request.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html">Work with Spot Instance</a> in the <i>Amazon EC2 User Guide</i>.</p> <important> <p>We strongly discourage using the RequestSpotInstances 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:
        launch_specification: <p>The launch specification.</p>
        tag_specifications: <p>The key-value pair for tagging the Spot Instance request on creation. The value for <code>ResourceType</code> must be <code>spot-instances-request</code>, otherwise the Spot Instance request fails. To tag the Spot Instance request after it has been created, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateTags.html">CreateTags</a>. </p>
        instance_interruption_behavior: <p>The behavior when a Spot Instance is interrupted. The default is <code>terminate</code>.</p>
        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_price: <p>The maximum price per unit hour that you are willing to pay for a Spot Instance. We do not recommend using this parameter because it can lead to increased interruptions. If you do not specify this parameter, you will pay the current Spot price.</p> <important> <p>If you specify a maximum price, your instances will be interrupted more frequently than if you do not specify this parameter.</p> </important>
        client_token: <p>Unique, case-sensitive identifier that you provide to ensure the idempotency of the request. For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Run_Instance_Idempotency.html">Ensuring idempotency in Amazon EC2 API requests</a> in the <i>Amazon EC2 User Guide</i>.</p>
        instance_count: <p>The maximum number of Spot Instances to launch.</p> <p>Default: 1</p>
        type: <p>The Spot Instance request type.</p> <p>Default: <code>one-time</code> </p>
        valid_from: <p>The start date of the request. If this is a one-time request, the request becomes active at this date and time and remains active until all instances launch, the request expires, or the request is canceled. If the request is persistent, the request becomes active at this date and time and remains active until it expires or is canceled.</p> <p>The specified start date and time cannot be equal to the current date and time. You must specify a start date and time that occurs after the current date and time.</p>
        valid_until: <p>The end date of the request, in UTC format (<i>YYYY</i>-<i>MM</i>-<i>DD</i>T<i>HH</i>:<i>MM</i>:<i>SS</i>Z).</p> <ul> <li> <p>For a persistent request, the request remains active until the <code>ValidUntil</code> date and time is reached. Otherwise, the request remains active until you cancel it. </p> </li> <li> <p>For a one-time request, the request remains active until all instances launch, the request is canceled, or the <code>ValidUntil</code> date and time is reached. By default, the request is valid for 7 days from the date the request was created.</p> </li> </ul>
        launch_group: <p>The instance launch group. Launch groups are Spot Instances that launch together and terminate together.</p> <p>Default: Instances are launched and terminated individually</p>
        availability_zone_group: <p>The user-specified name for a logical grouping of requests.</p> <p>When you specify an Availability Zone group in a Spot Instance request, all Spot Instances in the request are launched in the same Availability Zone. Instance proximity is maintained with this parameter, but the choice of Availability Zone is not. The group applies only to requests for Spot Instances of the same instance type. Any additional Spot Instance requests that are specified with the same Availability Zone group name are launched in that same Availability Zone, as long as at least one instance from the group is still active.</p> <p>If there is no active instance running in the Availability Zone group that you specify for a new Spot Instance request (all instances are terminated, the request is expired, or the maximum price you specified falls below current Spot price), then Amazon EC2 launches the instance in any Availability Zone where the constraint can be met. Consequently, the subsequent set of Spot Instances could be placed in a different zone from the original request, even if you specified the same Availability Zone group.</p> <p>Default: Instances are launched in any available Availability Zone.</p>
        block_duration_minutes: <p>Deprecated.</p>

    Raises:
        capo_ec2.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To create a one-time Spot Instance request
        This example creates a one-time Spot Instance request for five instances in the specified Availability Zone. If your account supports EC2-VPC only, Amazon EC2 launches the instances in the default subnet of the specified Availability Zone.

        >>> await client.request_spot_instances(spot_price='0.03', instance_count=5, type='one-time', launch_specification={'ImageId': 'ami-1a2b3c4d', 'KeyName': 'my-key-pair', 'SecurityGroupIds': ['sg-1a2b3c4d'], 'InstanceType': 'm3.medium', 'Placement': {'AvailabilityZone': 'us-west-2a'}, 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}})
        To create a one-time Spot Instance request
        This example command creates a one-time Spot Instance request for five instances in the specified subnet. Amazon EC2 launches the instances in the specified subnet. If the VPC is a nondefault VPC, the instances do not receive a public IP address by default.

        >>> await client.request_spot_instances(spot_price='0.050', instance_count=5, type='one-time', launch_specification={'ImageId': 'ami-1a2b3c4d', 'SecurityGroupIds': ['sg-1a2b3c4d'], 'InstanceType': 'm3.medium', 'SubnetId': 'subnet-1a2b3c4d', 'IamInstanceProfile': {'Arn': 'arn:aws:iam::123456789012:instance-profile/my-iam-role'}})
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_ec2.types.request_spot_instances_request.RequestSpotInstancesRequest]",
    ) -> AsyncOperationResponse[
        "capo_ec2.types.request_spot_instances_result.RequestSpotInstancesResult"
    ]:
        import capo_ec2._operations.amazon_ec2.request_spot_instances

        (
            output,
            http_response,
        ) = await capo_ec2._operations.amazon_ec2.request_spot_instances.async_request_spot_instances(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_ec2.types.request_spot_instances_request.RequestSpotInstancesRequest",
    ) -> dict[str, Any]:
        import capo_ec2.types.request_spot_instances_request

        return capo_ec2.types.request_spot_instances_request.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_ec2.types.request_spot_instances_result.RequestSpotInstancesResult",
    ) -> dict[str, Any]:
        import capo_ec2.types.request_spot_instances_result

        return capo_ec2.types.request_spot_instances_result.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "RequestSpotInstances",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_ec2.types.request_spot_instances_request.RequestSpotInstancesRequest = {}
    if launch_specification is not None:
        input_["launch_specification"] = launch_specification
    if tag_specifications is not None:
        input_["tag_specifications"] = tag_specifications
    if instance_interruption_behavior is not None:
        input_["instance_interruption_behavior"] = instance_interruption_behavior
    if dry_run is not None:
        input_["dry_run"] = dry_run
    if spot_price is not None:
        input_["spot_price"] = spot_price
    if client_token is not None:
        input_["client_token"] = client_token
    if instance_count is not None:
        input_["instance_count"] = instance_count
    if type is not None:
        input_["type"] = type
    if valid_from is not None:
        input_["valid_from"] = valid_from
    if valid_until is not None:
        input_["valid_until"] = valid_until
    if launch_group is not None:
        input_["launch_group"] = launch_group
    if availability_zone_group is not None:
        input_["availability_zone_group"] = availability_zone_group
    if block_duration_minutes is not None:
        input_["block_duration_minutes"] = block_duration_minutes

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output