spin multiple instances of different images using terraform on openstack

1k Views Asked by At

I've been trying to spin up multiple instances of different images using terraform but haven't had luck so far. I could spin multiple instances of a single image with count. While spinning up different images with for_each, I couldn't get the right configuration.

I've the below variable .tf configuration file.

variable "images" {
  type = map
  default = {
    "rhel-8-factory-os-ready" = {
       "az_zone" = "eu-fra-1ah"
       "ins_count" = 2
     }
    "rhel-7-factory-os-ready" = {
       "az_zone" = "eu-fra-1ai"
       "ins_count" = 2
    }
  }
}

resource "openstack_compute_instance_v2" "instance" {
   for_each = var.images
   flavor_id = var.flavor
   image_name = each.key
   name = "${var.image_name}-${lower(random_id.random-instance.hex)}"
   availability_zone = each.value.az_zone
   security_groups = var.security_group
   key_pair = "fop-mgt-key"
   network {
     name = var.network_name
   }
}

resource "random_id" "random-instance" {
  byte_length = 4
}

resource "random_id" "random-number" {
  byte_length = 8
}

resource "openstack_networking_floatingip_v2" "fip" {
  count = length(var.images)
  pool = var.floatingip_pool
}

resource "openstack_compute_floatingip_associate_v2" "fip" {

  depends_on = [openstack_compute_instance_v2.instance]

  count = length(var.images)
  floating_ip = openstack_networking_floatingip_v2.fip[count.index].address
  instance_id = openstack_compute_instance_v2.instance.*.id[count.index]
  fixed_ip    = openstack_compute_instance_v2.instance.*.network.0.fixed_ip_v4[count.index]
}

terraform {
  backend "artifactory" {}
}

data "terraform_remote_state" "foo" {
  backend = "artifactory"
  config = {
    repo = "${var.repo}"
    subpath = "${var.subpath}"
  }
}

I see the below error message ::

Error: Unsupported attribute

on tf-backend-fra/main.tf line 49, in resource "openstack_compute_floatingip_associate_v2" "fip": 49: instance_id = openstack_compute_instance_v2.instance.*.id[count.index]

This object does not have an attribute named "id".

I'm not sure how to get it done with for_each. Terraform version is 0.12.17. Your help is much appreciated.

Thanks, Harsha

1

There are 1 best solutions below

0
harshavmb On

Somehow managed to solve with lookup and element like below.

resource "openstack_compute_instance_v2" "instance" {
   for_each = var.images
   flavor_id = each.value.flavor
   image_id = lookup(data.openstack_images_image_v2.os-image, each.key).id
   name = "${var.image_name}-${lower(random_id.random-instance.hex)}"
   availability_zone = each.value.az_zone
   security_groups = var.security_group
   key_pair = "fop-mgt-key"
   network {
     name = var.network_name
   }
}

resource "openstack_compute_floatingip_associate_v2" "fip" {
  for_each = var.images
  floating_ip = element(openstack_networking_floatingip_v2.fip, index(tolist(keys(var.images)), each.key)).address
  instance_id = lookup(openstack_compute_instance_v2.instance, each.key).id
  fixed_ip    = lookup(openstack_compute_instance_v2.instance, each.key).network.0.fixed_ip_v4
}