Skip to content

detach_volume async

detach_volume(
    *,
    config_overrides: Optional[AsyncEC2ClientConfig] = None,
    device: Optional[String] = None,
    force: Optional[Boolean] = None,
    instance_id: Optional[InstanceIdForResolver] = None,
    volume_id: Optional[VolumeIdWithResolver] = None,
    dry_run: Optional[Boolean] = None,
) -> VolumeAttachment

Detaches an EBS volume from an instance. Make sure to unmount any file systems on the device within your operating system before detaching the volume. Failure to do so can result in the volume becoming stuck in the busy state while detaching. If this happens, detachment can be delayed indefinitely until you unmount the volume, force detachment, reboot the instance, or all three. If an EBS volume is the root device of an instance, it can't be detached while the instance is running. To detach the root volume, stop the instance first.

When a volume with an Amazon Web Services Marketplace product code is detached from an instance, the product code is no longer associated with the instance.

You can't detach or force detach volumes that are attached to Amazon Web Services-managed resources. Attempting to do this results in the UnsupportedOperationException exception.

For more information, see Detach an Amazon EBS volume in the Amazon EBS User Guide.

Parameters:

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

    The device name.

  • force (Optional[Boolean], default: None ) –

    Forces detachment if the previous detachment attempt did not occur cleanly (for example, logging into an instance, unmounting the volume, and detaching normally). This option can lead to data loss or a corrupted file system. Use this option only as a last resort to detach a volume from a failed instance. The instance won't have an opportunity to flush file system caches or file system metadata. If you use this option, you must perform file system check and repair procedures.

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

    The ID of the instance. If you are detaching a Multi-Attach enabled volume, you must specify an instance ID.

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

    The ID of the volume.

  • 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 detach a volume from an instance This example detaches the volume (vol-049df61146c4d7901) from the instance it is attached to.

>>> await client.detach_volume(volume_id='vol-1234567890abcdef0')
Source code in docs_src/capo_ec2/_services/async_ec2.py
async def detach_volume(
    self,
    *,
    config_overrides: Optional[AsyncEC2ClientConfig] = None,
    device: Optional["capo_ec2.types.string.String"] = None,
    force: Optional["capo_ec2.types.boolean.Boolean"] = None,
    instance_id: Optional[
        "capo_ec2.types.instance_id_for_resolver.InstanceIdForResolver"
    ] = None,
    volume_id: Optional[
        "capo_ec2.types.volume_id_with_resolver.VolumeIdWithResolver"
    ] = None,
    dry_run: Optional["capo_ec2.types.boolean.Boolean"] = None,
) -> "capo_ec2.types.volume_attachment.VolumeAttachment":
    """<p>Detaches an EBS volume from an instance. Make sure to unmount any file systems on the device within your operating system before detaching the volume. Failure to do so can result in the volume becoming stuck in the <code>busy</code> state while detaching. If this happens, detachment can be delayed indefinitely until you unmount the volume, force detachment, reboot the instance, or all three. If an EBS volume is the root device of an instance, it can't be detached while the instance is running. To detach the root volume, stop the instance first.</p> <p>When a volume with an Amazon Web Services Marketplace product code is detached from an instance, the product code is no longer associated with the instance.</p> <p>You can't detach or force detach volumes that are attached to Amazon Web Services-managed resources. Attempting to do this results in the <code>UnsupportedOperationException</code> exception.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/ebs/latest/userguide/ebs-detaching-volume.html">Detach an Amazon EBS volume</a> in the <i>Amazon EBS User Guide</i>.</p>

    Args:
        device: <p>The device name.</p>
        force: <p>Forces detachment if the previous detachment attempt did not occur cleanly (for example, logging into an instance, unmounting the volume, and detaching normally). This option can lead to data loss or a corrupted file system. Use this option only as a last resort to detach a volume from a failed instance. The instance won't have an opportunity to flush file system caches or file system metadata. If you use this option, you must perform file system check and repair procedures.</p>
        instance_id: <p>The ID of the instance. If you are detaching a Multi-Attach enabled volume, you must specify an instance ID.</p>
        volume_id: <p>The ID of the volume.</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 detach a volume from an instance
        This example detaches the volume (``vol-049df61146c4d7901``) from the instance it is attached to.

        >>> await client.detach_volume(volume_id='vol-1234567890abcdef0')
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_ec2.types.detach_volume_request.DetachVolumeRequest]",
    ) -> AsyncOperationResponse[
        "capo_ec2.types.volume_attachment.VolumeAttachment"
    ]:
        import capo_ec2._operations.amazon_ec2.detach_volume

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

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("DetachVolume")
    )
    input_: capo_ec2.types.detach_volume_request.DetachVolumeRequest = {}
    if device is not None:
        input_["device"] = device
    if force is not None:
        input_["force"] = force
    if instance_id is not None:
        input_["instance_id"] = instance_id
    if volume_id is not None:
        input_["volume_id"] = volume_id
    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