Terraform 检索入站 NAT 规则端口

发布于 2025-01-17 20:37:24 字数 2090 浏览 1 评论 0原文

我正在使用 Terraform 在 Azure 上部署基础设施, 我正在使用 Linux 规模设置和负载均衡器的模块,并使用 azurerm_lb_nat_pool 来通过 SSH 访问虚拟机, 我现在需要检索 NAT 规则的端口以用于其他目的。

在我的一生中,我找不到检索它们的方法,浏览了所有 terraform 文档,但在任何数据源或属性引用下都找不到它。

这是我的 LB 代码:

resource "azurerm_lb" "front-load-balancer" {
  name                = "front-load-balancer"
  location            = var.def-location
  resource_group_name = var.rg-name
  sku                 = "Standard"

  
  frontend_ip_configuration {
    name                 = "frontend-IP-configuration"
    public_ip_address_id = var.public-ip-id
  }
}

resource "azurerm_lb_nat_pool" "lb-nat-pool" {
  resource_group_name            = var.rg-name
  loadbalancer_id                = azurerm_lb.front-load-balancer.id
  name                           = "lb-nat-pool"
  protocol                       = "Tcp"
  frontend_port_start            = var.frontend-port-start
  frontend_port_end              = var.frontend-port-end
  backend_port                   = 22
  frontend_ip_configuration_name = "frontend-IP-configuration"
}

任何帮助将不胜感激。

编辑: 我尝试在 azurerm_lb 前端 IP 配置上导出 inbound_nat_rules 导出,它提供了我目前不知道如何从中提取端口的资源列表::

output "frontend-ip-confguration-inbound-nat-rules" {
  value = azurerm_lb.front-load-balancer.frontend_ip_configuration[*].inbound_nat_rules
}

结果在这方面:

Changes to Outputs:
  + LB-frontend-IP-confguration-Inbound-nat-rules = [
      + [
          + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/weight-tracker-stage-rg/providers/Microsoft.Network/loadBalancers/front-load-balancer/inboundNatRules/lb-nat-pool.3",
          + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/weight-tracker-stage-rg/providers/Microsoft.Network/loadBalancers/front-load-balancer/inboundNatRules/lb-nat-pool.4",
          + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/weight-tracker-stage-rg/providers/Microsoft.Network/loadBalancers/front-load-balancer/inboundNatRules/lb-nat-pool.6",
        ],
    ]

I'm deploying infrastructure on Azure using Terraform,
I'm using modules for a linux scale set an a load balancer and using azurerm_lb_nat_pool in order to have SSH access to the VMs,
I have a need now to retrieve the ports of the NAT rules for other purposes.

For the life of me I cannot find a way to retrieve them, went through all the terraform documentation and cannot find it under any data source or attribute reference.

Here is my LB code:

resource "azurerm_lb" "front-load-balancer" {
  name                = "front-load-balancer"
  location            = var.def-location
  resource_group_name = var.rg-name
  sku                 = "Standard"

  
  frontend_ip_configuration {
    name                 = "frontend-IP-configuration"
    public_ip_address_id = var.public-ip-id
  }
}

resource "azurerm_lb_nat_pool" "lb-nat-pool" {
  resource_group_name            = var.rg-name
  loadbalancer_id                = azurerm_lb.front-load-balancer.id
  name                           = "lb-nat-pool"
  protocol                       = "Tcp"
  frontend_port_start            = var.frontend-port-start
  frontend_port_end              = var.frontend-port-end
  backend_port                   = 22
  frontend_ip_configuration_name = "frontend-IP-configuration"
}

Any assistance would be very appreciated.

EDIT:
I tried exporting the inbound_nat_rules export on the azurerm_lb frontend IP configuration, it gives a list of the resources which I do not currently know how to extract the ports from::

output "frontend-ip-confguration-inbound-nat-rules" {
  value = azurerm_lb.front-load-balancer.frontend_ip_configuration[*].inbound_nat_rules
}

Which results in this:

Changes to Outputs:
  + LB-frontend-IP-confguration-Inbound-nat-rules = [
      + [
          + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/weight-tracker-stage-rg/providers/Microsoft.Network/loadBalancers/front-load-balancer/inboundNatRules/lb-nat-pool.3",
          + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/weight-tracker-stage-rg/providers/Microsoft.Network/loadBalancers/front-load-balancer/inboundNatRules/lb-nat-pool.4",
          + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/weight-tracker-stage-rg/providers/Microsoft.Network/loadBalancers/front-load-balancer/inboundNatRules/lb-nat-pool.6",
        ],
    ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2025-01-24 20:37:24

我已经能够使用下面的命令恢复端口。您可以使用 azapi_resource 并调用 API 在 terraform 中执行此操作。

az network lb 入站 nat-rule list --lb-name "{}" --resource-group "{}" --query "[].{Port:frontendPort}"

[
  {
    "Port": 49153
  },
  {
    "Port": 49154
  }
]

I've been able to recover the ports using the command below. You'd be able to do this in terraform using azapi_resource and calling the API.

az network lb inbound-nat-rule list --lb-name "{}" --resource-group "{}" --query "[].{Port:frontendPort}"

[
  {
    "Port": 49153
  },
  {
    "Port": 49154
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文