-
How to Use for_each with a List of Objects in Terraform
I have a requirement to provision a set of Google Cloud Platform (GCP) compute instances, and I’m using Terraform for this purpose. My Terraform configuration includes a variable, let’s call it “vms,” which is a list of objects. Each object represents a compute instance with various properties such as hostname, CPU, RAM, and more.
Here’s an example of what the “vms” variable looks like:
{
"gcp_zone": "us-central1-a",
"image_name": "centos-cloud/centos-7",
"vms": [
{
"hostname": "test1-srfe",
"cpu": 1,
"ram": 4,
"hdd": 15,
"log_drive": 300,
"template": "Template-New",
"service_types": [
"sql",
"db01",
"db02"
]
},
{
"hostname": "test1-second",
"cpu": 1,
"ram": 4,
"hdd": 15,
"template": "APPs-Template",
"service_types": [
"configs"
]
}
]
}
My goal is to use the
for_each
meta-argument in Terraform to iterate through this list of objects and create compute instances for each object, applying the specified properties.-
This discussion was modified 2 months, 2 weeks ago by
Joshua Falken.
-
This discussion was modified 2 months, 2 weeks ago by
Log in to reply.