我试图在terraform中使用从azure函数应用导出的功能来获取可能的出站IP地址,以便添加到防火墙的白名单中。
返回的参数是一串逗号分隔的ips。
我试过在terraform中使用split函数,但它并没有给出一个列表,而是给出了一个不能作为列表使用的接口。我试过用本地作用域在它周围加方括号,但还是一样。
我补充一下,这是terraform 11而不是12。
resource "azurerm_key_vault" "keyvault" {
name = "${var.project_name}-${var.environment}-kv"
location = "${azurerm_resource_group.environment.location}"
resource_group_name = "${azurerm_resource_group.environment.name}"
enabled_for_disk_encryption = true
tenant_id = "${var.tenant_id}"
sku_name = "standard"
network_acls {
bypass = "AzureServices"
default_action = "Deny"
ip_rules = "${split(",", azurerm_function_app.function.possible_outbound_ip_addresses)}"
}
tags = {
asset-code = "${var.storage_tags["asset_code"]}"
module-code = "${var.storage_tags["module_code"]}"
environment = "${var.environment}"
instance-code = "${var.storage_tags["instance_code"]}"
source = "terraform"
}
}
这时会出现 "ip_rules must be a list "的错误。
谢谢你
我认为你在这里看到的是一个典型的Terraform 0.11设计缺陷:当一个值在计划时是未知的(因为只有在应用时才会决定),Terraform 0.11不能正确跟踪它的类型信息。
因为 possible_outbound_ip_addresses
是规划时的未知值,结果是 split
与该字符串也是未知值。因为Terraform没有跟踪该结果的类型信息,所以提供者SDK代码会拒绝该未知值,因为它不是一个列表。
为了解决这个问题,在Terraform 0.11中,需要在初始运行时使用 -target
参数,这样Terraform就可以先集中精力创建函数(从而分配它的出站IP地址),一旦已知该字符串,再单独处理该字符串的处理。
terraform apply -target=azurerm_function_app.function
terraform apply # to complete the rest of the work that -target excluded
Terraform 0.12通过跟踪已知值和未知值的类型信息来解决这个限制,所以在Terraform 0.12中的 split
函数会看到你给了它一个未知的字符串,并接受它为正确的输入,然后它将返回一个未知的 字符串 来作为结果的占位符,结果将在应用阶段最终确定。
如果 var.string 是 1.2.3.4,5.6.7.8
-
split(',', var.string)[0]
还你 1.2.3.4
作为一个字符串。你的问题在没有例子的情况下是很难的。