Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to for_each through a list(objects) in Terraform 0.12

I need to deploy a list of GCP compute instances. How do I loop for_each through the "vms" in a list of objects like this:

    "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"         ]       }     ]     } 
like image 500
TheShadow2707 Avatar asked Oct 28 '19 16:10

TheShadow2707


People also ask

What is For_each in Terraform?

for_each is a meta-argument defined by the Terraform language. It can be used with modules and with every resource type. The for_each meta-argument accepts a map or a set of strings, and creates an instance for each item in that map or set.

Can we use For_each and count together in Terraform?

What is count in Terraform? When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage several of the same resources, you can use either count or for_each , which removes the need to write a separate block of code for each one.

How for each loop works in Terraform?

The for_each argument will iterate over a data structure to configure resources or modules with each item in turn. It works best when the duplicate resources need to be configured differently but share the same lifecycle. Tip: Terraform 0.13+ supports the for_each argument on both resource and module blocks.

How do you use count in Terraform?

How to use Terraform Count. We use count to deploy multiple resources. The count meta-argument generates numerous resources of a module. You use the count argument within the block and assign a whole number (could by variable or expression), and Terraform generates the number of resources.


1 Answers

Seem's like I found what to do. If you pass not the maps of maps but the list of maps you can use such code

resource "google_compute_instance" "node" {     for_each = {for vm in var.vms:  vm.hostname => vm}      name         = "${each.value.hostname}"     machine_type = "custom-${each.value.cpu}-${each.value.ram*1024}"     zone         = "${var.gcp_zone}"      boot_disk {         initialize_params {         image = "${var.image_name}"         size = "${each.value.hdd}"         }     }      network_interface {         network = "${var.network}"     }      metadata = {         env_id = "${var.env_id}"         service_types = "${join(",",each.value.service_types)}"   } } 

It will create actual number of instance and when you remove for example middle one of three(if you create three:)), terraform will remove what we asked.

like image 178
TheShadow2707 Avatar answered Sep 23 '22 17:09

TheShadow2707