How to add or remove access_config on terraform GCP with variable

Viewed 114

How to add or remove block code access_config { } on terraform with GCP.

I have variable:

external_ip = false

if external IP is value false code:


resource "google_compute_instance_from_template" "default_name_index" {
  name  = "${length(var.instances[change_with_index].instance_backup_ip) == 1 ? var.instances[change_with_index].instance_backup_name : format("%s-%s", var.instances[change_with_index].instance_backup_name, count.index + 1)}"
  count = length(var.instances[change_with_index].instance_backup_ip)

  source_instance_template = "projects/${var.provider_project}/global/instanceTemplates/${replace(var.instances[change_with_index].instance_name, "-app-image", "")}-${var.release_version}"

  network_interface {
    network    = var.instances[change_with_index].instance_network
    subnetwork = var.instances[change_with_index].instance_subnetwork
    network_ip = var.instances[change_with_index].instance_backup_ip[count.index]
    
  }
}

if external_ip is value true code:


resource "google_compute_instance_from_template" "default_name_index" {
  name  = "${length(var.instances[change_with_index].instance_backup_ip) == 1 ? var.instances[change_with_index].instance_backup_name : format("%s-%s", var.instances[change_with_index].instance_backup_name, count.index + 1)}"
  count = length(var.instances[change_with_index].instance_backup_ip)

  source_instance_template = "projects/${var.provider_project}/global/instanceTemplates/${replace(var.instances[change_with_index].instance_name, "-app-image", "")}-${var.release_version}"

  network_interface {
    network    = var.instances[change_with_index].instance_network
    subnetwork = var.instances[change_with_index].instance_subnetwork
    network_ip = var.instances[change_with_index].instance_backup_ip[count.index]
    
    #access_config will add in here
    access_config
    {
    }

  }
}

thank u for helping me.

1 Answers

You can do this with dynamic blocks and for_each:

resource "google_compute_instance_from_template" "default_name_index" {
  name  = "${length(var.instances[change_with_index].instance_backup_ip) == 1 ? var.instances[change_with_index].instance_backup_name : format("%s-%s", var.instances[change_with_index].instance_backup_name, count.index + 1)}"
  count = length(var.instances[change_with_index].instance_backup_ip)

  source_instance_template = "projects/${var.provider_project}/global/instanceTemplates/${replace(var.instances[change_with_index].instance_name, "-app-image", "")}-${var.release_version}"

  network_interface {
    network    = var.instances[change_with_index].instance_network
    subnetwork = var.instances[change_with_index].instance_subnetwork
    network_ip = var.instances[change_with_index].instance_backup_ip[count.index]
    
    #access_config will add in here
    dynamic "access_config"    
    {
       for_each = external_ip == false ? [] : [1]
       content {
          // the normal content of access_config
       }
    }

  }
}
Related