简体   繁体   English

如何在 terraform 中使用 for_each double?

[英]how to using for_each double in terraform?

everyone.每个人。
While using for_each in terraform, duplication is coming out.在 terraform 中使用 for_each 时,出现重复。 In this case, how should I bypass it?在这种情况下,我应该如何绕过它?

The problematic points are 1), and 2).有问题的点是1)和2)。 data values must be obtained from each resource through for_each.数据值必须通过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"       
    }

Currently, we have prepared several domains for redirect, and we tried to grant certificates for each.目前,我们已经为重定向准备了几个域,并且我们尝试为每个域授予证书。

For example, when there is an endpoint domain ( www.azure.com ), domains for redirect: auz-ure.com, auz-ure.com, az-ops.shop, etc. For example, when there is an endpoint domain ( www.azure.com ), domains for redirect: auz-ure.com, auz-ure.com, az-ops.shop, etc.

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

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

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

For the terraform code, I referred to the document. terraform代码,我参考了文档。

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

As you're using the same indexes for your app service managed certificates as your custom hostname bindings, you can just iterate over the custom hostname bindings again:当您为应用服务托管证书使用与自定义主机名绑定相同的索引时,您可以再次迭代自定义主机名绑定:

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.value].id     
    certificate_id      = azurerm_app_service_managed_certificate._service_manage_cert[each.value].id      
    ssl_state           = "SniEnabled"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM