terraform - 如何将对象变量用作块?
问题描述
我有以下地形文件:
variable azure_network_interface_ip_configuration {
default = {
name = "testconfiguration1"
subnet_id = "1" #"${azurerm_subnet.test.id}"
private_ip_address_allocation = "Static"
private_ip_address = "10.0.2.5"
public_ip_address_id = "1" #"${azurerm_public_ip.test.id}"
}
type = object({ name=string, subnet_id=string, private_ip_address_allocation=string, private_ip_address=string, public_ip_address_id=string })
}
resource "azurerm_resource_group" "test" {
name = "experiment"
location = "westeurope"
}
resource "azurerm_virtual_network" "test" {
name = "test-network"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "acctsub"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_public_ip" "test" {
name = "test-pip"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
allocation_method = "Dynamic"
idle_timeout_in_minutes = 30
tags = {
environment = "test"
}
}
resource "azurerm_network_interface" "test" {
name = "test-nic"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
ip_configuration = var.azure_network_interface_ip_configuration
}
但是当我尝试验证它时,我得到了以下输出。
错误:不支持的参数
在 main.tf 第 48 行,在资源“azurerm_network_interface”“test”中:48:ip_configuration = var.azure_network_interface_ip_configuration
这里不需要名为“ip_configuration”的参数。你的意思是定义一个“ip_configuration”类型的块吗?
我只想将变量用作块。我正在使用 terraform 0.12.8。我知道我可以单独设置每个参数,但对我来说,只设置完整的块会容易得多。
解决方案
不可能仅在一行中填充整个块。相反,您必须写出块并分别分配每个参数,以便从对象值到块的转换是明确的,并且 Terraform 可以验证各个参数:
ip_configuration {
name = var.azure_network_interface_ip_configuration.name
subnet_id = var.azure_network_interface_ip_configuration.subnet_id
private_ip_address_allocation = var.azure_network_interface_ip_configuration.private_ip_address_allocation
private_ip_address = var.azure_network_interface_ip_configuration.private_ip_address
public_ip_address_id = var.azure_network_interface_ip_configuration.public_ip_address_id
}
这是一个 Terraform 设计权衡的示例,旨在使配置对未来的读者更清晰,但代价是配置作者需要做更多的工作。在这种情况下,目标是明确设置哪些参数,而不是要求读者找到并理解声明var.azure_network_interface_ip_configuration
以了解这一点。
推荐阅读
- python - for i,j in a,b: TypeError: 'NoneType' object is not iterable
- javascript - if 和 else 条件
- google-api - 公共 Youtube/Google API 测试账号/频道
- python - 如何使用 scipy 的插值程序用 NaN 插值数据?
- python - 用交互项绘制逻辑回归的结果(Python Scikit-Learn)
- php - 无法使用 PHP 和 Laravel 存储上传的图像
- python - TfidfVectorizer 赋予停用词高权重
- regex - htaccess URL 重写不适用于连字符
- jquery - 未捕获的 RangeError:jQuery 触发函数中超出了最大调用堆栈大小
- python - 具有大参数列表的python中的多处理