azure Terraform for_each on a list

f45qwnt8  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(132)

我想做smth非常基本的这是我的变量文件

test = {
  t1 = {
    top = [
        {
            port = "12"
            name = "port12"
        },
        {
            port = "123"
            name = "port123"
        }
    ]
},
  t2 = {
    top = [
        {
            port = "12"
            name = "port12"
        },
        {
            port = "123"
            name = "port123"
        }

    ]
}

}

resource "azurerm_lb_nat_rule" "nat" {
  for_each = var.test 
  resource_group_name = var.rg.name
  loadbalancer_id     = elb.id
  name                = "ELB-XX"
  protocol                       = "Tcp"
  frontend_port                  = each.value.top.port
  backend_port                   = each.value.top.port
  frontend_ip_configuration_name = pip.id
}

我想要:

  • 名称如ELB-T1-port 12、ELB-T2-port 12、ELB-T1-port 123和ELB-T2-port 123
  • 检索frontend_port和backend_port的端口

我可以改变变量列表

csga3l58

csga3l581#

基于此配置,您可以创建azurerm_lb_nat_rule资源,如下所示:

resource "azurerm_lb_nat_rule" "nat" {
  for_each                    = var.test
  resource_group_name         = var.rg.name
  loadbalancer_id             = azurerm_lb.example.id
  protocol                    = "Tcp"
  frontend_ip_configuration   = azurerm_network_interface.example.ip_configuration[0].id
  frontend_port               = "ELB-${each.key}-port${each.value.top[0].port}"
  backend_port                = each.value.top[0].port
  name                        = "ELB-${each.key}-port${each.value.top[0].port}"
}

您可以阅读更多:The for_each Meta-Argument

相关问题