openstack_compute_volume_attach_v2

Attaches a Block Storage Volume to an Instance using the OpenStack Compute (Nova) v2 API.

Example Usage

Basic attachment of a single volume to a single instance

resource "openstack_blockstorage_volume_v3" "volume_1" {
  name = "volume_1"
  size = 1
}

resource "openstack_compute_instance_v2" "instance_1" {
  name            = "instance_1"
  security_groups = ["default"]
}

resource "openstack_compute_volume_attach_v2" "va_1" {
  instance_id = openstack_compute_instance_v2.instance_1.id
  volume_id   = openstack_blockstorage_volume_v3.volume_1.id
}

Attaching multiple volumes to a single instance

resource "openstack_blockstorage_volume_v3" "volumes" {
  count = 2
  name  = format("vol-%02d", count.index + 1)
  size  = 1
}

resource "openstack_compute_instance_v2" "instance_1" {
  name            = "instance_1"
  security_groups = ["default"]
}

resource "openstack_compute_volume_attach_v2" "attachments" {
  count       = 2
  instance_id = openstack_compute_instance_v2.instance_1.id
  volume_id   = openstack_blockstorage_volume_v3.volumes[count.index].id
}

output "volume_devices" {
  value = openstack_compute_volume_attach_v2.attachments.*.device
}

Note that the above example will not guarantee that the volumes are attached in a deterministic manner. The volumes will be attached in a seemingly random order.

If you want to ensure that the volumes are attached in a given order, create explicit dependencies between the volumes, such as:

resource "openstack_blockstorage_volume_v3" "volumes" {
  count = 2
  name  = format("vol-%02d", count.index + 1)
  size  = 1
}

resource "openstack_compute_instance_v2" "instance_1" {
  name            = "instance_1"
  security_groups = ["default"]
}

resource "openstack_compute_volume_attach_v2" "attach_1" {
  instance_id = openstack_compute_instance_v2.instance_1.id
  volume_id   = openstack_blockstorage_volume_v3.volumes.0.id
}

resource "openstack_compute_volume_attach_v2" "attach_2" {
  instance_id = openstack_compute_instance_v2.instance_1.id
  volume_id   = openstack_blockstorage_volume_v3.volumes.1.id

  depends_on = ["openstack_compute_volume_attach_v2.attach_1"]
}

output "volume_devices" {
  value = "${openstack_compute_volume_attach_v2.attachments.*.device}"
}

Using Multiattach-enabled volumes

Multiattach Volumes are dependent upon your OpenStack cloud and not all clouds support multiattach.

resource "openstack_blockstorage_volume_v3" "volume_1" {
  name        = "volume_1"
  size        = 1
  multiattach = true
}

resource "openstack_compute_instance_v2" "instance_1" {
  name            = "instance_1"
  security_groups = ["default"]
}

resource "openstack_compute_instance_v2" "instance_2" {
  name            = "instance_2"
  security_groups = ["default"]
}

resource "openstack_compute_volume_attach_v2" "va_1" {
  instance_id = openstack_compute_instance_v2.instance_1.id
  volume_id   = openstack_blockstorage_volume_v3.volume_1.id
  multiattach = true
}

resource "openstack_compute_volume_attach_v2" "va_2" {
  instance_id = openstack_compute_instance_v2.instance_2.id
  volume_id   = openstack_blockstorage_volume_v3.volume_1.id
  multiattach = true

  depends_on = ["openstack_compute_volume_attach_v2.va_1"]
}

It is recommended to use depends_on for the attach resources to enforce the volume attachments to happen one at a time.

Argument Reference

The following arguments are supported:

The vendor_options block supports:

Attributes Reference

The following attributes are exported:

Import

Volume Attachments can be imported using the Instance ID and Volume ID separated by a slash, e.g.

$ terraform import openstack_compute_volume_attach_v2.va_1 89c60255-9bd6-460c-822a-e2b959ede9d2/45670584-225f-46c3-b33e-6707b589b666