# Openstack: Create multiple instances with additional disks - for\_each list(object)

**URL:** <https://discuss.hashicorp.com/t/openstack-create-multiple-instances-with-additional-disks-for-each-list-object/60110>\
**Category:** Terraform\
**Created:** [November 16, 2023, 4:23pm UTC](https://discuss.hashicorp.com/t/openstack-create-multiple-instances-with-additional-disks-for-each-list-object/60110 "2023-11-16T16:23:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EddyMaestroDev](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/eddymaestrodev/32/38833_2.png) [@EddyMaestroDev](https://discuss.hashicorp.com/u/EddyMaestroDev)\
**Post date:** [November 16, 2023, 4:23pm UTC](https://discuss.hashicorp.com/t/openstack-create-multiple-instances-with-additional-disks-for-each-list-object/60110/1 "2023-11-16T16:23:38Z")

</div>

Hey,

I’m currently trying to create several instances under Openstack with Terraform. Afterwards I want to attach more of them. But I’m not getting anywhere with the last point.  
Does anyone have an idea how I can solve it?

Here is my code:

```auto
variable "instances" {
  description = "The Instances to be deployed"
  type = map(object({
    name = string
    image = string
    flavor = string
    volume_size = number
    security_groups = list(string)
    network = string
    keypair_name = string
    floating_ip_pool = string
    tags = list(string)
    additional_disks = list(object({
      disk = string
      volume_size = number
    }))
  }))
}

instances = {
  "Websrv01" = {
    name = "websrv01"
    image = "Ubuntu 22.04"
    flavor = "SCS-2V-4-20s"
    volume_size = 20
    security_groups = ["default"]
    network = "test-intern"
    keypair_name = "ssh-pub"
    floating_ip_pool = "public"
    tags = ["general", "webserver"]
    additional_disks = []
  },
  "dbsrv01" = {
    name = "dbsrv01"
    image = "Ubuntu 22.04"
    flavor = "SCS-2V-4-20s"
    volume_size = 20
    security_groups = ["default"]
    network = "test-intern"
    keypair_name = "ssh-pub"
    floating_ip_pool = "public"
    tags = ["general", "dbserver"]
    additional_disks = [
      { disk = "db_data", volume_size = 30 },
      { disk = "db_log", volume_size = 10 }
    ]
  }
}

resource "openstack_compute_instance_v2" "instances" {
  for_each = var.instances

  name = each.value.name
  flavor_id = data.openstack_compute_flavor_v2.flavor[each.key].id
  key_pair = each.value.keypair_name
  security_groups = each.value.security_groups

  ## Boot Disk
  block_device {
    uuid = data.openstack_images_image_v2.image[each.key].id
    source_type = "image"
    volume_size = each.value.volume_size
    boot_index = 0
    destination_type = "volume"
    delete_on_termination = true
  }
  /*
  lifecycle {
    prevent_destroy = true
  }*/

  network {
    name = each.value.network
  }

  tags = each.value.tags
}

## Get floating IP if needed
resource "openstack_networking_floatingip_v2" "floating_ip" {
  for_each = { for k, v in var.instances : k => v if v.floating_ip_pool != "" }

  pool = each.value.floating_ip_pool
}

## Associate floating ip to Instances
resource "openstack_compute_floatingip_associate_v2" "associate" {
  for_each = openstack_networking_floatingip_v2.floating_ip

  instance_id = openstack_compute_instance_v2.instances[each.key].id
  floating_ip = each.value.address
}

## Additional Disks
resource "openstack_blockstorage_volume_v3" "additional_disks" {
  for_each = {
    for instance, disks in var.instances : instance => disks.additional_disks
    if length(disks.additional_disks) > 0
  }

  name = "${each.key}-${each.value[0].disk}"
  size = each.value[0].volume_size
  enable_online_resize = true
}

## Attach additional disks to instances
resource "openstack_compute_volume_attach_v2" "attach_additional_disks" {
  for_each = {
    for instance, disks in var.instances : instance => disks.additional_disks
    if length(disks.additional_disks) > 0
  }

  instance_id = openstack_compute_instance_v2.instances[each.key].id
  volume_id = openstack_blockstorage_volume_v3.additional_disks[each.key].id
}

```

Only the first hard disk is taken into account here.  
Ich i tried a to loop truth the list with [\*], but it fails.

regards  
Eddi

---

<div class="post-metadata">

**Author:** ![EddyMaestroDev](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/eddymaestrodev/32/38833_2.png) [@EddyMaestroDev](https://discuss.hashicorp.com/u/EddyMaestroDev)\
**Post date:** [November 16, 2023, 9:38pm UTC](https://discuss.hashicorp.com/t/openstack-create-multiple-instances-with-additional-disks-for-each-list-object/60110/2 "2023-11-16T21:38:12Z")

</div>

with the code I can roll out many systems.

```auto
variable "instances" {
  description = "The Instances to be deployed"
  type = map(object({
    name = string
    image = string
    flavor = string
    volume_size = number
    security_groups = list(string)
    network = string
    keypair_name = string
    floating_ip_pool = string
    tags = list(string)
  }))
}

instances = {
  "Websrv01" = {
    name = "websrv01"
    image = "Ubuntu 22.04"
    flavor = "SCS-2V-4-20s"
    volume_size = 20
    security_groups = ["default"]
    network = "test-intern"
    keypair_name = "ssh-pub"
    floating_ip_pool = "public"
    tags = ["general", "webserver"]
  },
  "Websrv02" = {
    name = "websrv02"
    image = "Ubuntu 22.04"
    flavor = "SCS-2V-4-20s"
    volume_size = 20
    security_groups = ["default"]
    network = "test-intern"
    keypair_name = "ssh-pub"
    floating_ip_pool = "public"
    tags = ["general", "webserver"]
  },
  "dbsrv01" = {
    name = "dbsrv01"
    image = "Ubuntu 22.04"
    flavor = "SCS-2V-4-20s"
    volume_size = 20
    security_groups = ["default"]
    network = "test-intern"
    keypair_name = "ssh-pub"
    floating_ip_pool = "public"
    tags = ["general", "dbserver"]
  }
}

resource "openstack_compute_instance_v2" "instances" {
  for_each = var.instances

  name = each.value.name
  flavor_id = data.openstack_compute_flavor_v2.flavor[each.key].id
  key_pair = each.value.keypair_name
  security_groups = each.value.security_groups

  ## Boot Disk
  block_device {
    uuid = data.openstack_images_image_v2.image[each.key].id
    source_type = "image"
    volume_size = each.value.volume_size
    boot_index = 0
    destination_type = "volume"
    delete_on_termination = true
  }
  /*
  lifecycle {
    prevent_destroy = true
  }*/

  network {
    name = each.value.network
  }

  tags = each.value.tags
}

## Get floating IP if needed
resource "openstack_networking_floatingip_v2" "floating_ip" {
  for_each = { for k, v in var.instances : k => v if v.floating_ip_pool != "" }

  pool = each.value.floating_ip_pool
}

## Associate floating ip to Instances
resource "openstack_compute_floatingip_associate_v2" "associate" {
  for_each = openstack_networking_floatingip_v2.floating_ip

  instance_id = openstack_compute_instance_v2.instances[each.key].id
  floating_ip = each.value.address
}

```

And that works perfectly so far. Now I would like to be able to attach further hard disks with individual sizes. a dynamic entry with block\_device is not possible. Since no subsequent changes are possible.

How could the variable be usefully supplemented to create the corresponding sections with openstack\_blockstorage\_volume\_v3 and openstack\_compute\_volume\_attach\_v2?

---

<div class="post-metadata">

**Author:** ![EddyMaestroDev](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/eddymaestrodev/32/38833_2.png) [@EddyMaestroDev](https://discuss.hashicorp.com/u/EddyMaestroDev)\
**Post date:** [November 18, 2023, 3:09pm UTC](https://discuss.hashicorp.com/t/openstack-create-multiple-instances-with-additional-disks-for-each-list-object/60110/3 "2023-11-18T15:09:01Z")

</div>

A solution has been found:

> <https://stackoverflow.com/questions/77497279/openstack-terraform-multiple-instances-with-additional-disks-for-each-listob/77507435#77507435>
