Terraform 如何将对象变量用作块?
我有以下地形文件:Terraform 如何将对象变量用作块?,terraform,Terraform,我有以下地形文件: variable azure_network_interface_ip_configuration { default = { name = "testconfiguration1" subnet_id = "1" #"${azurerm_subnet.test.id}" private_ip_addre
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_网络_接口”测试中:
48:ip_配置=var.azure_网络_接口_ip_配置
此处不需要名为“ip_配置”的参数。你是故意的吗
定义“ip_配置”类型的块
我只想使用一个变量作为块。我正在使用terraform 0.12.8。我知道我可以单独设置每个参数,但对我来说,只设置整个块要容易得多
更新人:
请参阅相关GitHub问题
不可能在一行中填充整个块。相反,您必须写出块并分别指定每个参数,以便从对象值到块的转换是显式的,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
}
这是一个地形设计折衷的例子,其目的是使配置对未来的读者更清晰,而代价是配置作者需要多做一些工作。在这种情况下,目标是明确设置哪些参数,而不是要求读者查找并理解var.azure\u network\u interface\u ip\u configuration
的声明,以便了解这一点