Skip to content

attach_volume async

attach_volume(
    *,
    config_overrides: Optional[AsyncEC2ClientConfig] = None,
    device: Optional[String] = None,
    instance_id: Optional[InstanceId] = None,
    volume_id: Optional[VolumeId] = None,
    ebs_card_index: Optional[BoxedInteger] = None,
    dry_run: Optional[Boolean] = None,
) -> VolumeAttachment

Attaches an Amazon EBS volume to a running or stopped instance, and exposes it to the instance with the specified device name.

The maximum number of Amazon EBS volumes that you can attach to an instance depends on the instance type. If you exceed the volume attachment limit for an instance type, the attachment request fails with the AttachmentLimitExceeded error. For more information, see Instance volume limits.

After you attach an EBS volume, you must make it available for use. For more information, see Make an EBS volume available for use.

If a volume has an Amazon Web Services Marketplace product code:

  • The volume can be attached only to a stopped instance.

  • Amazon Web Services Marketplace product codes are copied from the volume to the instance.

  • You must be subscribed to the product.

  • The instance type and operating system of the instance must support the product. For example, you can't detach a volume from a Windows instance and attach it to a Linux instance.

For more information, see Attach an Amazon EBS volume to an instance in the Amazon EBS User Guide.

Parameters:

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

    The device name (for example, /dev/sdh or xvdh).

  • instance_id (Optional[InstanceId], default: None ) –

    The ID of the instance.

  • volume_id (Optional[VolumeId], default: None ) –

    The ID of the EBS volume. The volume and instance must be within the same Availability Zone.

  • ebs_card_index (Optional[BoxedInteger], default: None ) –

    The index of the EBS card. Some instance types support multiple EBS cards. The default EBS card index is 0.

  • 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.

Raises:

Examples:

To attach a volume to an instance This example attaches a volume (vol-1234567890abcdef0) to an instance (i-01474ef662b89480) as /dev/sdf.

>>> await client.attach_volume(volume_id='vol-1234567890abcdef0', instance_id='i-01474ef662b89480', device='/dev/sdf')
Source code in docs_src/capo_ec2/_services/async_ec2.py
async def attach_volume(
    self,
    *,
    config_overrides: Optional[AsyncEC2ClientConfig] = None,
    device: Optional["capo_ec2.types.string.String"] = None,
    instance_id: Optional["capo_ec2.types.instance_id.InstanceId"] = None,
    volume_id: Optional["capo_ec2.types.volume_id.VolumeId"] = None,
    ebs_card_index: Optional["capo_ec2.types.boxed_integer.BoxedInteger"] = None,
    dry_run: Optional["capo_ec2.types.boolean.Boolean"] = None,
) -> "capo_ec2.types.volume_attachment.VolumeAttachment":
    """<p>Attaches an Amazon EBS volume to a <code>running</code> or <code>stopped</code> instance, and exposes it to the instance with the specified device name.</p> <note> <p>The maximum number of Amazon EBS volumes that you can attach to an instance depends on the instance type. If you exceed the volume attachment limit for an instance type, the attachment request fails with the <code>AttachmentLimitExceeded</code> error. For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/volume_limits.html">Instance volume limits</a>.</p> </note> <p>After you attach an EBS volume, you must make it available for use. For more information, see <a href="https://docs.aws.amazon.com/ebs/latest/userguide/ebs-using-volumes.html">Make an EBS volume available for use</a>.</p> <p>If a volume has an Amazon Web Services Marketplace product code:</p> <ul> <li> <p>The volume can be attached only to a stopped instance.</p> </li> <li> <p>Amazon Web Services Marketplace product codes are copied from the volume to the instance.</p> </li> <li> <p>You must be subscribed to the product.</p> </li> <li> <p>The instance type and operating system of the instance must support the product. For example, you can't detach a volume from a Windows instance and attach it to a Linux instance.</p> </li> </ul> <p>For more information, see <a href="https://docs.aws.amazon.com/ebs/latest/userguide/ebs-attaching-volume.html">Attach an Amazon EBS volume to an instance</a> in the <i>Amazon EBS User Guide</i>.</p>

    Args:
        device: <p>The device name (for example, <code>/dev/sdh</code> or <code>xvdh</code>).</p>
        instance_id: <p>The ID of the instance.</p>
        volume_id: <p>The ID of the EBS volume. The volume and instance must be within the same Availability Zone.</p>
        ebs_card_index: <p>The index of the EBS card. Some instance types support multiple EBS cards. The default EBS card index is 0.</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>

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

    Examples:
        To attach a volume to an instance
        This example attaches a volume (``vol-1234567890abcdef0``) to an instance (``i-01474ef662b89480``) as ``/dev/sdf``.

        >>> await client.attach_volume(volume_id='vol-1234567890abcdef0', instance_id='i-01474ef662b89480', device='/dev/sdf')
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_ec2.types.attach_volume_request.AttachVolumeRequest]",
    ) -> AsyncOperationResponse[
        "capo_ec2.types.volume_attachment.VolumeAttachment"
    ]:
        import capo_ec2._operations.amazon_ec2.attach_volume

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

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("AttachVolume")
    )
    input_: capo_ec2.types.attach_volume_request.AttachVolumeRequest = {}
    if device is not None:
        input_["device"] = device
    if instance_id is not None:
        input_["instance_id"] = instance_id
    if volume_id is not None:
        input_["volume_id"] = volume_id
    if ebs_card_index is not None:
        input_["ebs_card_index"] = ebs_card_index
    if dry_run is not None:
        input_["dry_run"] = dry_run

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