【问题标题】:how to using for_each double in terraform?如何在 terraform 中使用 for_each double?
【发布时间】:2021-11-25 02:19:22
【问题描述】:

每个人。
在 terraform 中使用 for_each 时,会出现重复。 这种情况下应该怎么绕过呢?

问题点是 1) 和 2)。 数据值必须通过for_each从每个资源中获取。

// custom hostname binding
resource "azurerm_app_service_custom_hostname_binding" "service_host_bind" {
     
  for_each                     =  azurerm_dns_cname_record.cname_target
      
  hostname                  = trim(each.value.fqdn, ".")
      
  app_service_name     = azurerm_app_service._service.name
      
  resource_group_name = azurerm_resource_group._rg.name
      
  depends_on          = [azurerm_dns_txt_record._txt_target]

  lifecycle {
        ignore_changes = [ssl_state, thumbprint]
      }    }

// app service managed certificate
resource "azurerm_app_service_managed_certificate" "_service_manage_cert" {
      
      for_each                    = azurerm_app_service_custom_hostname_binding._service_host_bind
      
      custom_hostname_binding_id  = each.value.id     
    }

// app service certificate binding
resource "azurerm_app_service_certificate_binding" "xtrm_service_certi_bind" {
      
      1) hostname_binding_id = azurerm_app_service_custom_hostname_binding._service_host_bind.id
      // ## how to for_each??
      2) certificate_id               = azurerm_app_service_managed_certificate._service_manage_cert.id
        // ## how to for_each??
      ssl_state           = "SniEnabled"       
    }

目前,我们已经为重定向准备了几个域,并且我们尝试为每个域授予证书。

例如,当有端点域(www.azure.com)时,重定向的域:auz-ure.com、auz-ure.com、az-ops.shop 等。

(azure-redirect.net -> www.azure.com

auz-ure.com -> www.azure.com

az-ops.shop -> www.azure.com)

关于 terraform 代码,我参考了文档。

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/app_service_managed_certificate

【问题讨论】:

    标签: azure terraform azure-app-service-plans


    【解决方案1】:

    由于您对应用服务托管证书使用与自定义主机名绑定相同的索引,因此您可以再次迭代自定义主机名绑定:

    resource "azurerm_app_service_certificate_binding" "xtrm_service_certi_bind" {
      
        for_each            = azurerm_app_service_custom_hostname_binding.service_host_bind
        hostname_binding_id = azurerm_app_service_custom_hostname_binding.service_host_bind[each.key].id     
        certificate_id      = azurerm_app_service_managed_certificate._service_manage_cert[each.key].id      
        ssl_state           = "SniEnabled"
    }
    

    【讨论】:

    • 感谢您重播 :) 我尝试了您的建议,但它显示此错误“给定的键未识别此集合值中的元素:需要字符串。”
    • 您可能需要使用 each.key 而不是 each.value。我会更新答案。
    • 你好,我试试这个代码和成功的 terraform 计划 XD 是的,每个.key 都通过了工作,谢谢! ''' for_each = azurerm_app_service_custom_hostname_binding.service_host_bind hostname_binding_id = azurerm_app_service_custom_hostname_binding.service_host_bind[each.key].id certificate_id = azurerm_app_service_managed_certificate.service_manage_cert[each.key].id '''
    猜你喜欢
    • 2021-04-14
    • 2021-04-09
    • 2021-07-09
    • 2020-12-19
    • 2022-07-21
    • 2020-12-09
    • 2020-11-01
    • 2023-02-25
    • 1970-01-01
    相关资源
    最近更新 更多