terraform - Terraform:如果子网不存在,则将 interface 设置为 null
问题描述
我正在创建一个在 GCP 中创建多网卡 VM 的自定义模块。在部署时,可能会出现 VM 不需要使用多个接口的情况。
如果子网不存在,是否可以将 network_interface 设置为空值?如果可能,我想避免为每个接口计数创建多个模块。
resource "google_compute_instance" "vm" {
name = "${var.vm_name}"
machine_type = "${var.machine_type}"
zone = "${var.zone}"
min_cpu_platform = "${var.cpu_platform}"
network_interface {
subnetwork = "${google_compute_subnetwork.subnetwork1.name}"
}
network_interface {
subnetwork = "${google_compute_subnetwork.subnetwork2.name}"
}
network_interface {
// PSEUDO CODE
subnetwork = if (subnetwork3 == true) {
"${google_compute_subnetwork.subnetwork3.name}"
else
"do nothing or set null"
}
}
解决方案
你可以结合locals
acount
吗?
例子
locals {
interface_num = "${var.is_subnetwork_3 ? 0 : 3}"
}
resource "google_whatever" "name" {
count = "${local.interface_num}"
// config
}
推荐阅读
- python-3.x - 没有名为“pyautogui”的模块
- java - 我可以在方法 java 类中使用用户输入吗?
- botframework - 无法在 botbnt 框架作曲家中使用先前定义的属性设置属性
- linux - Linux 内核 v.5。linux/wait.h WAIT_QUEUE_HEAD
- php - 如何使用 foreach 循环创建多维数组
- laravel - 带有 SASS 的 Tailwindcss 2.0 - 使用高级选择器
- sql - SQLite:测试学生是否跳过了一个学年
- php - PHP通过键值访问数组项
- microsoft-graph-api - Microsoft Graph 的问题 - DriveItem 添加权限
- sql - 检索列名的子字符串,然后将它们应用于相应的列数据?SQL