如何使用变量在 terraform GCP 上添加或删除 access_config
Posted
技术标签:
【中文标题】如何使用变量在 terraform GCP 上添加或删除 access_config【英文标题】:How to add or remove access_config on terraform GCP with variable 【发布时间】: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
谢谢你帮助我。
【问题讨论】:
【参考方案1】:您可以使用dynamic blocks 和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
【讨论】:
以上是关于如何使用变量在 terraform GCP 上添加或删除 access_config的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 terraform 将快照计划添加到 GCP boot_disk