fbpx

Learn Build5Nines Forum

Find answers, ask questions, and connect with our
global community of IT professionals.

Forums HashiCorp Terraform How to Use for_each with a List of Objects in Terraform

  • How to Use for_each with a List of Objects in Terraform

    Posted by Joshua Falken on September 22, 2023 at 11:33 am

    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 7 months, 1 week ago by  Joshua Falken.
    Joshua Falken replied 7 months, 1 week ago 1 Member · 1 Reply
  • 1 Reply
  • Joshua Falken

    Member
    September 22, 2023 at 11:33 am

    Solution for Using for_each with a List of Objects in Terraform

    To achieve your goal of deploying Google Cloud Platform (GCP) compute instances using Terraform and iterating through the “vms” variable, you can utilize the for_each meta-argument along with resource blocks. Here’s a step-by-step guide on how to do this:

    1. Declare the “vms” Variable: In your Terraform configuration, declare the “vms” variable as shown in your example. Make sure to have this variable defined with your list of objects.

    Create a Resource Block: Use the resource block to define the GCP compute instances. You will create a single resource block for each compute instance you want to provision.

    resource "google_compute_instance" "example" {

    for_each = { for vm in var.vms : vm.hostname => vm }

    name = each.value.hostname

    machine_type = "n1-standard-${each.value.cpu}"

    zone = var.gcp_zone

    # Define other properties like boot disk, network, etc.

    }

    In this example, we use the <code style=”color: var(–bb-body-text-color);”>for_each argument to loop through the “vms” variable. Each instance is identified by its “hostname,” which serves as the unique key in the map.

    1. Set Instance Properties: Inside the resource block, set the properties of each compute instance. You can access the values from the current iteration using each.value.

    2. Define Other Resources: If you have additional resources associated with each compute instance, such as a network configuration, define them within the same Terraform configuration file.

    3. Apply Terraform Configuration: Run terraform init and terraform apply to apply your Terraform configuration. Terraform will create compute instances for each object in the “vms” variable.

    With this approach, Terraform will create GCP compute instances for each object in your “vms” list, applying the specified properties to each instance. This allows you to easily manage and scale your infrastructure based on the objects in your list.

    • This reply was modified 7 months, 1 week ago by  Joshua Falken.
    • This reply was modified 7 months, 1 week ago by  Joshua Falken.

Log in to reply.