简体   繁体   English

Terraform Azurerm - 输出公共 IP 地址用作变量

[英]Terraform Azurerm - Output public IP address to be used as a variable

right now I am creating a Terraform script to setup an application gateway, aks, nsg etc...现在我正在创建一个 Terraform 脚本来设置应用程序网关、aks、nsg 等...

I am in need to setup an inbound rule within my nsg to allow all traffic from my application gateway ip address to my back end vnet.我需要在我的 nsg 中设置一个入站规则,以允许从我的应用程序网关 IP 地址到我的后端 vnet 的所有流量。

The public ip address is created when the application gateway is configured.配置应用程序网关时会创建公共 ip 地址。 Is there a way to output this IP address and use it as a variable for a inbound rule on my nsg?有没有办法输出这个 IP 地址并将它用作我的 nsg 上的入站规则的变量?

I assume the name of the IP address is predictable (in your case you're defining it as a static IP).我假设 IP 地址的名称是可预测的(在您的情况下,您将其定义为静态 IP)。 Terraform output sometimes doesn't get the IP address because it takes longer to actually get provisioned. Terraform 输出有时无法获取 IP 地址,因为实际配置需要更长的时间。 Terraform public IP address block allows exporting of the IP address using the ip_address attribute. Terraform 公共 IP 地址块允许使用ip_address属性导出 IP 地址

Static IP静态IP

If using a static IP address you can actually just do:如果使用静态 IP 地址,您实际上可以这样做:

destination_address_prefix = azurerm_public_ip.myagw_pip.ip_address

Dynamic IP动态IP

If you're using a dynamic IP address you could just use a data source to get the IP address and then parse it to your NSG rule:如果您使用的是动态 IP 地址,则可以使用数据源获取 IP 地址,然后将其解析为您的 NSG 规则:

data "azurerm_public_ip" "agw_pip" {
  name                = azurerm_public_ip.myagw_pip.name
  resource_group_name = azurerm_public_ip.myagw_pip.resource_group_name
}

I'm referencing the values of name and resource_group_name from the resource object just so Terraform sets an implicit dependency between them.我正在从资源对象中引用nameresource_group_name的值,以便 Terraform 在它们之间设置隐式依赖关系。 That way it will query the IP address after it gets created.这样它就会在创建后查询 IP 地址。

After that you need to configure your NSG to something like:之后,您需要将 NSG 配置为:

destination_address_prefix = data.azurerm_public_ip.pip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM