如何将对象变量用作块?

Car*_*ten 6 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
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试验证它时,我得到以下输出。

错误:不支持的参数

在 main.tf 第 48 行,资源“azurerm_network_interface”“test”:48:ip_configuration = var.azure_network_interface_ip_configuration

这里不需要名为“ip_configuration”的参数。您的意思是定义一个“ip_configuration”类型的块吗?

我只是想使用一个变量作为一个块。我正在使用 terraform 0.12.8。我知道我可以单独设置每个参数,但对我来说,设置完整的块会容易得多。

由Yurik更新:请参阅相关的 GitHub 问题25668

Mar*_*ins 10

不可能仅用一行填充整个块。相反,您必须写出块并单独分配每个参数,以便从对象值到块的转换是显式的,并且 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
  }
Run Code Online (Sandbox Code Playgroud)

这是 Terraform 设计权衡的一个示例,旨在使配置对未来的读者更清晰,但代价是需要配置作者做更多的工作。在这种情况下,目标是明确要设置哪些参数,而不是要求读者查找并理解 的声明var.azure_network_interface_ip_configuration来了解这一点。

  • 我想知道这是一个有意识的设计选择还是他们只是无意中偶然发现的。 (4认同)