【问题标题】:Dynamic IP Restriction to add ip addresses, vnet and service tag terraform azure动态 IP 限制以添加 IP 地址、vnet 和服务标签 terraform azure
【发布时间】:2021-11-16 15:23:16
【问题描述】:

到目前为止,在前面主题的帮助下,我能够使用以下代码部署具有 IP 限制的应用服务:

变量

locals {
ip_address_list2 = [            {     
                  ip_add : "20.20.20.3/32",
                  subnet_id = null,
                  service_tag               = null,
                  prior : "140",
                  name = "test1"
            },
           {     
                  ip_add : "10.10.10.2/32",
                  subnet_id = null,
                  service_tag               = null,
                  prior : "141",
                  name = "test2"
            },
            {
                 ip_add : "0.0.0.0/0",
                 subnet_id = null,
                 service_tag               = "AppService"
                 prior : "142",
                 name = "Service_Tag"
            }]}

应用服务:

  site_config {
  dynamic "ip_restriction" {
for_each = local.ip_address_list2
  content {
    ip_address  = ip_restriction.value["ip_add"]
    action                    = "Allow"
    priority                  = ip_restriction.value["prior"]
    virtual_network_subnet_id = ip_restriction.value["subnet_id"]
    service_tag = ip_restriction.value["service_tag"]
    name = ip_restriction.value["name"]
  }}}

但如果我为子网添加以下变量,我会收到错误:

        {
             ip_add : "0.0.0.0/0",
             subnet_id = azurerm_subnet.subnet.id,
             service_tag               = null
             prior : "143",
             name = "VirtualNetwork"
        }

Screenshot Error

错误:创建应用服务“hook-service”(资源组 “RG-DEV-TEST”):web.AppsClient#CreateOrUpdate:发送失败 请求:StatusCode=0 -- 原始错误:Code="BadRequest" Message="IpSecurityRestriction 无效。只有 IpAddress 或 必须指定 VnetSubnetResourceId 属性。” Details=[{"Message":"IpSecurityRestriction 无效。只有 IpAddress 或 VnetSubnetResourceId 属性必须是 指定。"},{"Code":"BadRequest"},{"ErrorEntity":{"Code":"BadRequest","ExtendedCode":"51021","Message":"IpSecurityRestriction 是无效的。只有 IpAddress 或 VnetSubnetResourceId 属性必须是 指定。","MessageTemplate":"{0} 无效。 {1}","Parameters":["IpSecurityRestriction","仅 IpAddress 或 必须指定 VnetSubnetResourceId 属性。"]}}] │ │ 与 azurerm_app_service.hook-service,│在 main.tf 第 474 行,在资源中 “azurerm_app_service” “hook-service”:│ 474:资源 “azurerm_app_service” “钩子服务” {

注意:仅使用 terraform plan 即可完成验证而不会出错。只有在 terraform apply

之后才会观察到错误

谢谢

【问题讨论】:

  • 您好@Igor,根据错误消息,它清楚地表明您可以使用 ip_address 或虚拟网络,同时设置 ip_restrictions 不能同时使用。所以如果你添加 ip_address 那么 vnet,subnet 将默认为空,如果提供了虚拟网络,则 ip_address 将变为空。
  • 感谢您回复我的问题。我怎样才能避免或结合这种配置?我知道它只在门户上点击几下就可以创建这个 ip_restriction 但我想让它自动创建

标签: azure-web-app-service terraform-provider-azure azure-rm


【解决方案1】:

你应该使用类似下面的东西:

locals {
 ip_address_list = [            {     
                  ip_add : "20.20.20.3/32",
                  prior : "140",
                  name = "test1"
            },
           {     
                  ip_add : "10.10.10.2/32",
                  prior : "141",
                  name = "test2"
            }
 ]
ip_address_list2=[            {
                 subnet = "${data.azurerm_subnet.name.id}" ,
                 name = "test3"
                 prior ="143"
            }]
            }

然后你可以使用:

  site_config {
    dynamic "ip_restriction"{
      for_each=local.ip_address_list
      content{
        ip_address  = ip_restriction.value["ip_add"]
    action                    = "Allow"
    priority                  = ip_restriction.value["prior"]
    name = ip_restriction.value["name"]
      }
    }
dynamic "ip_restriction"  {
  for_each = local.ip_address_list2
  content{
    name = ip_restriction.value["name"]
    virtual_network_subnet_id = ip_restriction.value["subnet"]
    priority = ip_restriction.value["prior"]
  }
}
}

输出:

注意:对于 IP_address 限制,您必须提供 ip_add 、 Prior 和 name。但是对于子网限制,您必须只提供子网 ID、优先级和名称。提供 service_tag 会报同样的错误。

【讨论】:

  • 这太棒了:我在 ip_address_list (仅添加 ip 地址) ip_address_list2 (仅添加 Service_Tag ) ip_address_list3 (添加虚拟子网,服务端点测试如下:并且正在运行)并且所有必需的限制都在门户上可用。
  • 很高兴能帮上忙!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
相关资源
最近更新 更多