如何通过 bicep 将自定义域添加到 Azure 容器应用程序?

Tho*_* F. 5 azure azure-dns azure-bicep azure-container-apps

我有一个二头肌模板,可以创建多个容器应用程序。他们应在我的私有 DNS 区域中有一个自定义子域。但是,我遇到的问题是,这会导致循环依赖:要将 CName 和验证 TXT 记录添加到 DNS 区域,我需要容器应用程序的 FQDN 和验证。另一方面,容器应用程序的部署会验证这些记录是否存在,如果不存在则失败。

我当前使用的解决方法涉及一个布尔模板参数,该参数在第一次运行时必须为 false:

param register_custom_domains bool = false

resource container_app 'Microsoft.App/containerapps@2022-03-01' = {
  // ...
  properties: {
    configuration: {
      ingress: {
        customDomains: register_custom_domains ? [
          {
            name: 'name'
            certificateId: certifcate.id
            bindingType: 'SniEnabled'
          }
        ] : null 
      }
    }
  }
}

Run Code Online (Sandbox Code Playgroud)

但这自然需要部署两次。有没有更好的办法?

Tho*_* F. 3

有一种一次性解决方案。它基于这样一个事实:容器应用程序环境和各个容器的验证字符串是相同的。由于容器将获得一个由容器名称和容器环境defaultDomain组成的默认域名,因此可以在容器之前创建CNAME和TXT记录。

my_container以下是注册名为的容器应用程序的分步说明subdomain.my-domain.com

  1. 使用SSL证书创建容器应用程序环境:
resource container_environment 'Microsoft.App/managedEnvironments@2022-06-01-preview' = {
  name: 'cae'
  // ...

  resource ssl_certificate 'certificates@2022-03-01' = {
    name: 'my-certificate'
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 在 DNS 区域中创建 CNAME 和 TXT 记录。就我而言,由于 DNS 区域位于另一个资源组中,因此我必须使用一个模块,但这里省略了。
resource dns_zone 'Microsoft.Network/dnsZones@2018-05-01' existing = {
  name: 'my-domain.com'

  resource cname 'CNAME@2018-05-01' = {
    name: 'subdomain'
    properties: {
      TTL: 3600
      CNAMERecord: {
        cname: '${container_name}.${container_environment.properties.defaultDomain}'
      }
    }
  }
  resource verification 'TXT@2018-05-01' = {
    name: 'asuid.subdomain'
    properties: {
      TTL: 3600
      TXTRecords: [
        {
          value: [container_environment.properties.customDomainConfiguration.customDomainVerificationId]
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 创建具有自定义域的容器。这里指定依赖项很重要,因为二头肌编译器无法知道是否需要域记录。
resource container_app 'Microsoft.App/containerapps@2022-03-01' = {
  dependsOn: [extend_dns_zone_module]
  name: 'my_container'
  properties: {
    // ...
    configuration: {
      // ...
      ingress: {
        // ...
        customDomains: [
          {
            name: 'subdomain.my-domain.com'
            certificateId: container_environment::ssl_certificate.id
            bindingType: 'SniEnabled'
          } 
        ]
      }
   }
}
Run Code Online (Sandbox Code Playgroud)