【发布时间】:2021-12-18 18:00:33
【问题描述】:
如何使 ip_configuration 可选以打开以下内容:
resource "azurerm_firewall" "example" {
name = "testfirewall"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "configuration"
subnet_id = azurerm_subnet.example.id
public_ip_address_id = azurerm_public_ip.example.id
}
}
到一些可以选择接受值的东西:
variable "ip_configuration" {
type = object({
name = string // Specifies the name of the IP Configuration.
subnet_id = string // Reference to the subnet associated with the IP Configuration.
public_ip_address_id = string // The ID of the Public IP Address associated with the firewall.
})
description = "(Optional) An ip_configuration block as documented"
default = null
}
我正在查看动态块、查找和尝试表达式,但似乎没有任何效果。任何人都可以帮忙吗?我花了几天时间试图弄明白
编辑: 也许有一种更简洁的方法可以做到这一点,但我发现了一些可行的方法。如果有人可以对此进行改进,那就太好了,但感谢那些回答的人。
subnet_id 应该只出现在第一个 ip_configuration 中,这就是我决定在密钥上使用编号系统的原因。
resource "azurerm_firewall" "example" {
name = "testfirewall"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
dynamic "ip_configuration" {
for_each = var.ip_configuration
iterator = ip
content {
name = ip.value["name"]
subnet_id = ip.key == "0" ? ip.value["subnet_id"] : null
public_ip_address_id = ip.value["public_ip_address_id"]
}
}
}
variable "ip_configuration" {
type = map
description = <<-EOT
(Optional) An ip_configuration block is nested maps with 0, 1, 2, 3 as the name of the map as documented below:
name = string // Specifies the name of the IP Configuration.
public_ip_address_id = string // The ID of the Public IP Address associated with the firewall.
subnet_id = string // Reference to the subnet associated with the IP Configuration. The Subnet used for the Firewall must have the name AzureFirewallSubnet and the subnet mask must be at least a /26.
NOTE: Only the first map (with a key of 0) should have a subnet_id property.
EXAMPLE LAYOUT:
{
"0" = {
name = "first_pip_configuration"
public_ip_address_id = azurerm_public_ip.firstpip.id
subnet_id = azurerm_subnet.example.id
},
"1" = {
name = "second_pip_configuration"
public_ip_address_id = azurerm_public_ip.secondpip.id
},
"2" = {
name = "third_pip_configuration"
public_ip_address_id = azurerm_public_ip.thirdpip.id
}
}
EOT
default = {}
}
【问题讨论】:
-
动态块绝对会为您做到这一点,所以请更新问题,尝试使用动态块以及为什么它不适合您。
-
“可选”到底是什么意思?您是要包含
ip_configuration还是根据某些变量不包含它? -
我在我的问题中添加了更多细节,在玩了几个小时后,我终于找到了可行的方法。谢谢你帮我看这个。如果您有任何进一步的改进,我会全力以赴:)
-
请更新此帖子,并提供解释您如何解决问题的答案。这将允许找到您的帖子并遇到相同问题的其他用户发现潜在的解决方案。您提到您找到了解决方案,请分享!
-
谢谢。刚刚有机会更新这个@MrPotatoHead。
标签: dynamic terraform conditional-statements