Iterate over a list of json objects in terraforms 0.12.23

5.8k Views Asked by At

I have two arrays of objects in a json file that I'm passing as a tfvar file over the cli command. One is a list of flavors and another is a list of images. What I wanna do is create a VM for each flavor and image, combining them.

Here is an example of the image array:

{
  "image_list" : [
    {
      "ID": "e7d95104-4d15-4087-ab32-4a33f580d664",
      "Name": "CentOS-7-x86_64",
      "Status": "active"
    },
    {
      "ID": "d266a91a-9bfb-4b32-b0de-f9940adc4017",
      "Name": "Debian-8-x86_64",
      "Status": "active"
    },
    {
      "ID": "912967ae-4ee8-4aab-ad6d-b97aeeac3526",
      "Name": "Debian-9-x86_64",
      "Status": "active"
    }
  ]
}

And here is an example of the flavor list:

{
  "flavor_list": [
    {
      "ID": "00000000-0000-000A-0001-000000001024",
      "Name": "T1-1vCPU-1GB-RAM",
      "RAM": 1024,
      "Disk": 50,
      "Ephemeral": 0,
      "VCPUs": 1,
      "Is_Public": true
    },
    {
      "ID": "00000000-0000-000A-0002-000000002048",
      "Name": "T1-2vCPU-2GB-RAM",
      "RAM": 2048,
      "Disk": 50,
      "Ephemeral": 0,
      "VCPUs": 2,
      "Is_Public": true
    },
    {
      "ID": "01829284-4e5e-4bf8-a753-525dc59e8476",
      "Name": "M1-4vCPU-32GB-RAM",
      "RAM": 32768,
      "Disk": 50,
      "Ephemeral": 0,
      "VCPUs": 4,
      "Is_Public": true
    }
  ]
}

I have followed the instructions here https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/ but it's not working and the error messages are not helping either.

Here is my tf file:

# Configure the OpenStack Provider
provider "openstack" {
# I have ommited the content here
}

variable "image_list" {
  type = list(object({
    ID      = string
    Name    = string
    Status  = string
  }))
  default = [
    {
      ID      = "912967ae-4ee8-4aab-ad6d-b97aeeac3526",
      Name    = "Debian-9-x86_64",
      Status  = "active"
    }
  ]
}

variable "flavor_list" {
  type = list(object({
    ID        = string
    Name      = string
    RAM       = number
    Disk      = number
    Ephemeral = number
    VCPUs     = number
    Is_Public = bool
  }))
}

# Create a web server
resource "openstack_compute_instance_v2" "Odin_test_VMs" {
  for_each = var.flavor_list

  dynamic "flavor" {
    for_each = var.flavor_list

    content {
      name = flavor_list.value.Name
    }
  }

  dynamic "image" {
    for_each = var.image_list

    content {
      id = image.value.ID
    }
  }

  name = "VM_${flavor.value.name}"
  flavor_name = flavor.value.name
  image_id    = image.value.id

  network {
    name = "1234362-network"
    uuid = "cb7be82b-e521-4b21-b82c-fb084abe8d3b"
  }
}

Edit

Even after the mjpgpy3 suggestions, I'm having the same error messages:

Error: Reference to undeclared resource

  on odin_test.tf line 61, in resource "openstack_compute_instance_v2" "Odin_test_VMs":
  61:   name = "VM_${flavor.value.name}"

A managed resource "flavor" "value" has not been declared in the root module.


Error: Reference to undeclared resource

  on odin_test.tf line 62, in resource "openstack_compute_instance_v2" "Odin_test_VMs":
  62:   flavor_name = flavor.value.name

A managed resource "flavor" "value" has not been declared in the root module.


Error: Reference to undeclared resource

  on odin_test.tf line 63, in resource "openstack_compute_instance_v2" "Odin_test_VMs":
  63:   image_id    = image.value.id

A managed resource "image" "value" has not been declared in the root module.
1

There are 1 best solutions below

1
mjgpy3 On

A couple of errors that I can see by glancing over your terraform.

All variables, when used, must be accessed with var.. For example use var.flavor_list rather than just flavor_list.

dynamic blocks require a nested content block which is where the attributes are actually assigned. The for_each just specifies which collection you're iterating over.

So, instead of usages like this

  dynamic "image" {
    for_each = [for i in image_list: {
      id = i.ID
    }]
  }

You probably need something like the following (note the var change is included)

  dynamic "image" {
    for_each = var.image_list

    content {
      id = image.value.ID
    }

  }

See the terraform docs for more info on how dynamic blocks work.