【问题标题】:Terraform Azure App Service - ip_restrictionsTerraform Azure 应用服务 - ip_restrictions
【发布时间】:2019-03-29 05:06:50
【问题描述】:

我正在尝试在我的 Azure App Service 应用程序中设置 IP 限制块

执行 Terraform 计划或应用时,我收到以下错误: 错误:azurerm_app_service.app-service-1: : 无效或未知密钥:ip_restriction

我在应用服务(Web 应用)资源的 Terraform 文档中使用了 ip_restriction

这是我正在使用的 AppService 部署代码:

resource "azurerm_app_service" "app-service-1" {
  name                    = "${var.app_service_1}"
  location                = "${data.azurerm_resource_group.core-rg.location}"
  resource_group_name     = "${data.azurerm_resource_group.core-rg.name}"
  app_service_plan_id     = "${data.azurerm_app_service_plan.app-service-plan-1.id}"
  https_only              = "True"
  enabled                 = "True"
  client_affinity_enabled = "True"

  site_config {
    always_on                 = "True"
    #default_documents        = ""
    dotnet_framework_version  = "v4.0"
    #http2_enabled            = ""
    #ip_restriction           = ""
    #java_version             = ""
    #java_container           = ""
    #java_container_version   = ""
    managed_pipeline_mode     = "Integrated"
    min_tls_version           = "1.2"
    #php_version              = ""
    #python_version           = ""
    remote_debugging_enabled  = "False"
    #remote_debugging_version = ""
    scm_type                  = "None"
    use_32_bit_worker_process = "False"
    websockets_enabled        = "True"
    #ftps_state               = ""
  }

  app_settings {
    "KeyVaultURI" = ""
    "WEBSITE_NODE_DEFAULT_VERSION" = "6.9.1"
  }

  ip_restriction {
   "ip_address"     = ""
   }

谢谢

【问题讨论】:

  • 我相信您需要为ip_address 分配一个值。当您将有效的 IP 地址放入其中时会发生什么?
  • 是的,我在那里有一个 IP,我为这篇文章删除了它。我从 Terraform Apply 收到的错误是声称“无效或未知的密钥:ip_restriction”,好像它不是 Terraform 中受支持的命令,尽管有 terraform 文档。有没有人成功使用 ip_restriction { "ip_address" = "1.2.3.4" }。文档说明子网掩码是可选的,但首先我们需要 Terraform 将 ip_restriction 识别为有效命令
  • 有人有什么想法吗?

标签: terraform azure-web-app-service ip-restrictions


【解决方案1】:

所以您遇到了语法错误。正如我在去年了解到的那样,文档可能会令人困惑。如果您阅读ip_restriction 上的部分,它会说需要一个或多个。这意味着它需要一个数组。

还有一段文档告诉您,它在数组中需要一个具有 ip_address 和 subnet_mask 属性的对象。那是here

因此,要解决您的问题,您需要以下 ip_restriction。

ip_restriction = [
    {
        ip_address = "10.0.0.0"
    }
]

希望这会有所帮助。

【讨论】:

  • 感谢您提供的信息。这确实是来自 Terraform 的令人困惑且几乎矛盾的信息!我相信这将解决问题。再次感谢!
  • 如果这回答了您的问题,请务必将其标记为答案,因为它会帮助其他人知道。
  • 我终于有机会现场试用了,它也不接受这种格式错误:azurerm_app_service.app-service-1:: invalid or unknown key: ip_restriction
【解决方案2】:

有兴趣的可以看看 Terraform 中使用 ipRestrictions 的方法

ip Restrictions 是 Site_Config 的一部分{}

使用方法见下文:

AppService.tf:

resource "azurerm_app_service" "app-service-1" {
  name                    = "${var.app_service_1}"
  location                = "${data.azurerm_resource_group.core-rg.location}"
  resource_group_name     = "${data.azurerm_resource_group.core-rg.name}"
  app_service_plan_id     = "${data.azurerm_app_service_plan.app-service-plan-1.id}"
  https_only              = "True"
  enabled                 = "True"
  client_affinity_enabled = "True"
  site_config {
    always_on                 = "True"
    #default_documents        = ""
    dotnet_framework_version  = "v4.0"
    #http2_enabled            = ""
    #ip_restriction           = ""
    #java_version             = ""
    #java_container           = ""
    #java_container_version   = ""
    managed_pipeline_mode     = "Integrated"
    min_tls_version           = "1.2"
    #php_version              = ""
    #python_version           = ""
    remote_debugging_enabled  = "False"
    #remote_debugging_version = ""
    scm_type                  = "None"
    use_32_bit_worker_process = "False"
    websockets_enabled        = "True"
    #ftps_state               = ""
    ip_restriction {
      ip_address  = "${var.ip_address_1}"
    }
    ip_restriction {
      ip_address  = "${var.ip_address_2}"
    }
    ip_restriction {
      ip_address  = "${var.ip_address_3}"
    }
  }
  app_settings {
    "KeyVaultURI" = ""
    "WEBSITE_NODE_DEFAULT_VERSION" = "6.9.1"
    }
  }

【讨论】:

  • ip_address 必须是 CIDR 表示法,您不能设置名称或设置 IP6 地址,这是一种耻辱
【解决方案3】:

不幸的是,@jamies 的答案不正确 IP_restriction 不是一个包含一个或多个的列表,而是一个可重复的块。

@gvazzana 是正确的格式。 但是,有一个陷阱..会导致您看到的错误。

在 Tf 中,我们习惯于以完整的 CIDR 格式键入 IP 地址,例如 10.23.97.201/23 或 192.68.50.0/24,本部分的 azure 门户甚至会这样显示它们。

但是对于这个特殊的街区,在 terraform 中,你必须把它们做老派。例如:

site_config {
  # For a single IP address
  ip_restriction {
      ip_address = "81.145.174.78"
      } 
  ip_restriction {
  # For an address range 
      ip_address = "10.240.101.0"
      subnet_mask = "255.255.255.0"
     }
}

如果您有很长的地址和范围列表,这当然会很痛苦。

现在 terraform 版本 0.12.0 已经发布,我们应该能够利用新的 dynamic 块样式和 cidrhostcidrmask 函数来简化事情。

例如:

dynamic "ip_restriction" {
for_each = var.ip_address_list
  content {
    ip_address  = cidrhost(ip_restriction.value,0)
    subnet_mask = cidrmask(ip_restriction.value)
  }
}

经过测试 Terraform v0.12.13

【讨论】:

  • 谢谢你,很棒的信息。从 v11 切换到 tf v12 会很困难。必须进行如此多的更改,并且我敢肯定会破坏代码!
  • 正是我需要的!除了我认为子网掩码的功能需要是cdrnetmask(...)。除非我像这样更改它,否则我无法让它工作:content { ip_address = cidrhost(ip_restriction.value,0) subnet_id = cidrnetmask(ip_restriction.value) } 我在the docs here 找到了这个。
  • @jamie 的回答实际上是有效的。这是documented and working alternative syntax for for repeated blocks。在这种情况下,它甚至更可取,因为使用动态块不会正确删除 ip_restriction 设置,因为它没有明确地将其设置为空块。
猜你喜欢
  • 2021-06-08
  • 2019-02-28
  • 1970-01-01
  • 2021-10-30
  • 2022-09-30
  • 2021-10-16
  • 2021-02-17
  • 2020-05-09
  • 2018-02-20
相关资源
最近更新 更多