【问题标题】:How to add or remove access_config on terraform GCP with variable如何使用变量在 terraform GCP 上添加或删除 access_config
【发布时间】:2021-12-17 13:50:19
【问题描述】:

如何使用 GCP 在 terraform 上添加或删除块代码 access_config { }

我有变量:

external_ip = false

如果外部 IP 值为 false 代码:


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]
    
  }
}

如果 external_ip 的值为 true 代码:


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
    {
    }

  }
}

谢谢你帮助我。

【问题讨论】:

    标签: google-cloud-platform terraform terraform-provider-gcp


    【解决方案1】:

    您可以使用dynamic blocksfor_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
           }
        }
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 2020-05-02
      • 1970-01-01
      • 2019-04-01
      • 2016-12-03
      • 2020-02-24
      • 2021-05-18
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多