gpt4 book ai didi

azure - 从 Terraform 中的 azurerm_windows_web_app 获取入站 IP 地址

转载 作者:行者123 更新时间:2023-12-03 06:52:09 25 4
gpt4 key购买 nike

我想创建允许“Azure Windows Web 应用程序”的 IP 的 azurerm_mssql_firewall_rule 资源女巫。

当我手动编写它时,效果很好。例如:

resource "azurerm_mssql_firewall_rule" "api_cloud" {
name = var.cloud_firewal_rule_name
server_id = azurerm_mssql_server.api.id
start_ip_address = "00.000.000.00"
end_ip_address = "00.000.000.00"
}

我想要获取像这样的 IP 地址 start_ip_address/end_ip_address = azurerm_windows_web_app.api.inbound_ip_address

但是 azurerm_windows_web_app 中没有入站选项,我只能访问出站地址 azurerm_windows_web_app.api.outbound_ip_addresses

有没有办法做这样的事情?

<小时/>

简而言之:如何使用 terraform 获取此 IP 地址? enter image description here

最佳答案

是的,有,但由于 Terraform 的工作方式,它有点复杂。我在示例中使用了 Linux 应用服务,但它对于 Windows 和 Linux 版本的工作方式应该相同。我们走吧:

因此,由于应用服务在共享基础架构上运行,因此它们具有相当大范围的可能出站 IP 地址,因此事情会变得更加复杂。因此它返回一个长度未知的列表。这让 Terraform 变得很烦人。例如,这就是您通常使用 for_each 迭代 Terraform 中的多个项目的方式。 :

resource "azurerm_mssql_firewall_rule" "example" {
for_each = toset(azurerm_linux_web_app.api_app.outbound_ip_address_list)
name = "FirewallRule"
server_id = azurerm_mssql_server.example.id
start_ip_address = each.key
end_ip_address = each.key
}

在此代码段中,您从应用服务获取出站 IP 地址列表 cast them to a set ,然后迭代它。但是,这仅在应用服务已存在的情况下才有效 - 如果您从空白开始,您将面临以下错误:

azurerm_linux_web_app.api_app.outbound_ip_address_list is a list ofstring, known only after apply

The "for_each" map includes keysderived from resource attributes that cannot be determined untilapply, and so Terraform cannot determine the full set of keys thatwill identify the instances of this resource.

When working withunknown values in for_each, it's better to define the map keysstatically in your configuration and place apply-time results only inthe map values.

Alternatively, you could use the -targetplanning option to first apply only the resources that the for_eachvalue depends on, and then apply a second time to fully converge.

幸运的是,Terraform 有一个非常有用的错误消息,它告诉我们如何解决该问题。使用-target parameter我们可以首先像这样创建应用服务

terraform apply -target=azurerm_linux_web_app.api_app

这应该只创建应用服务及其所需的依赖项。之后,我们就可以正常执行 Terraform,它应该按预期工作,没有任何错误。它不是很漂亮,但目前没有更好的方法来实现您想要的。

关于azure - 从 Terraform 中的 azurerm_windows_web_app 获取入站 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73489066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com