Skip to content

Resource: awsVolumeAttachment

Provides an AWS EBS Volume Attachment as a top level resource, to attach and detach volumes from AWS Instances.

\~> NOTE on EBS block devices: If you use ebsBlockDevice on an awsInstance, Terraform will assume management over the full set of non-root EBS block devices for the instance, and treats additional block devices as drift. For this reason, ebsBlockDevice cannot be mixed with external awsEbsVolume + awsVolumeAttachment resources for a given instance.

Example Usage

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const awsEbsVolumeExample = new aws.ebsVolume.EbsVolume(this, "example", {
  availabilityZone: "us-west-2a",
  size: 1,
});
const awsInstanceWeb = new aws.instance.Instance(this, "web", {
  ami: "ami-21f78e11",
  availabilityZone: "us-west-2a",
  instanceType: "t2.micro",
  tags: {
    Name: "HelloWorld",
  },
});
new aws.volumeAttachment.VolumeAttachment(this, "ebs_att", {
  deviceName: "/dev/sdh",
  instanceId: awsInstanceWeb.id,
  volumeId: awsEbsVolumeExample.id,
});

Argument Reference

The following arguments are supported:

  • deviceName - (Required) The device name to expose to the instance (for example, /dev/sdh or xvdh). See Device Naming on Linux Instances and Device Naming on Windows Instances for more information.
  • instanceId - (Required) ID of the Instance to attach to
  • volumeId - (Required) ID of the Volume to be attached
  • forceDetach - (Optional, Boolean) Set to true if you want to force the volume to detach. Useful if previous attempts failed, but use this option only as a last resort, as this can result in data loss. See Detaching an Amazon EBS Volume from an Instance for more information.
  • skipDestroy - (Optional, Boolean) Set this to true if you do not wish to detach the volume from the instance to which it is attached at destroy time, and instead just remove the attachment from Terraform state. This is useful when destroying an instance which has volumes created by some other means attached.
  • stopInstanceBeforeDetaching - (Optional, Boolean) Set this to true to ensure that the target instance is stopped before trying to detach the volume. Stops the instance, if it is not already stopped.

Attributes Reference

In addition to all arguments above, the following attributes are exported:

  • deviceName - The device name exposed to the instance
  • instanceId - ID of the Instance
  • volumeId - ID of the Volume

Import

EBS Volume Attachments can be imported using DEVICE_NAME:VOLUME_ID:INSTANCE_ID, e.g.,

$ terraform import aws_volume_attachment.example /dev/sdh:vol-049df61146c4d7901:i-12345678