🔀

Azure Container Apps (Environment) におけるルールベースのルーティング + カスタムドメイン

に公開

はじめに

この記事は、Azure Container Apps (Environment) にカスタムドメインを利用してルールベースのルーティングを設けてみた記録です。

https://zenn.dev/georgeosddev/articles/azure-container-apps-rule-based-routing

の続きとなります。

手順

まずはドキュメントに沿ってやってみます。

https://learn.microsoft.com/ja-jp/azure/container-apps/rule-based-routing-custom-domain

ちょっと複雑ですが先に

  1. カスタムドメインをEnvに対して設ける(DNSレコードの追加)
  2. ルーティングの追加
  3. 無料のマネージド証明書を追加

といった順番となるようです。

カスタムドメインの追加

Env にカスタム DNS サフィックス を設ける際は、*.<DNS_SUFFIX> で Aレコードを構成しましたが、今回はサブドメイン zenn-acaenv-routing.toshida-sandbox.org で指定してみます。

DNS Record

asuid の 値自体は共通のようです。

ルーティングの追加

あとから無料のマネージド証明書を追加するため、certificateId は指定せずに、bindingTypeAuto としておきます。

routing.yml
customDomains:
  - name: "zenn-aca-env-routing.toshida-sandbox.org"
    # certificateId: "<CERTIFICATE_ID>" //not needed with bindingType: "Auto"
    bindingType: "Auto"
rules:
  - description: App 1 rule
    routes:
      - match:
          prefix: /app1
        action:
          prefixRewrite: /
    targets:
      - containerApp: my-container-app-1
  - description: App 2 rule
    routes:
      - match:
          path: /app2
        action:
          prefixRewrite: /
      - match:
          path: /
    targets:
      - containerApp: my-container-app-2
az containerapp env http-route-config create \
  --http-route-config-name custom-domain-routes \
  --resource-group zenn-aca-without-vnet \
  --name aca-env-without-vnet \
  --yaml routing.yml \
  --query properties.fqdn

マネージド証明書の追加

CLI を用いてマネージド証明書を追加します。

az containerapp env certificate | Microsoft Learn

az containerapp env certificate create -g zenn-aca-without-vnet --name aca-env-without-vnet --hostname zenn-aca-env-routing.toshida-sandbox.org --validation-method CNAME

エラー

  • 先にルーティングを追加していないと以下のエラーが発生しました。

    (RequireCustomHostnameInEnvironment) Creating managed certificate requires hostname 'zenn-aca-env-routing.toshida-sandbox.org' added as a custom hostname to a container app or route in environment 'aca-env-without-vnet'
    
  • --validation-method CNAME は使えません。

    (CnameNotSupported) CNAME domain validation is not supported for routes, use HTTP or TXT.
    

そのため --validation-method TXT を用いてみます。
TXT レコードを登録するように応答が返却されます。

$ az containerapp env certificate create -g zenn-aca-without-vnet --name aca-env-without-vnet --hostname zenn-aca-env-routing.toshida-sandbox.org --validation-method TXT
This command is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
/ Running ..
Please copy the token below for TXT record and enter it with your domain provider:
_xmhgtac★★★★★

{
  "id": "/subscriptions/<サブスクリプションID>/resourceGroups/zenn-aca-without-vnet/providers/Microsoft.App/managedEnvironments/aca-env-without-vnet/managedCertificates/mc-zenn-aca-witho-zenn-aca-env-rou-0323",
  "location": "Japan East",
  "name": "mc-zenn-aca-witho-zenn-aca-env-rou-0323",
  "properties": {
    "domainControlValidation": "TXT",
    "provisioningState": "Pending",
    "subjectName": "zenn-aca-env-routing.toshida-sandbox.org",
    "validationMethod": "TXT",
    "validationToken": "_xmhgtac★★★★★"
  },
  "resourceGroup": "zenn-aca-without-vnet",
  "systemData": {
    "createdAt": "2026-01-15T05:58:29.5403331",
    <省略>
  },
  "type": "Microsoft.App/managedEnvironments/managedCertificates"
}

このとき証明書は Pending ステータスとなります。

cert-pending

指示通り TXT レコードを追加します。この TXT レコードはドメイン検証用の asuidとは異なります。

DNS Record2

TXT レコード追加後しばらくすると、検証に成功します。

cert-succeeded

結果

想定通りカスタムドメインを用いてルーティングが機能していることが確認できました。

app1-with-cert

app2-with-cert

考察など

できることはデフォルトドメインでのルールベース ルーティング と同じですが、証明書の追加方法が少しトリッキーですね。

また、Env にカスタム DNS サフィックス追加とは異なり、こちらは無料のマネージド証明書を利用することができます。

個別のアプリにカスタムドメインを設けないが、カスタムドメインを用いたいといった選択をした場合、カスタム DNS サフィックス か 今回の方法のどちらかを選択することになると思います。

個人的には、個別のアプリにカスタムドメインを設けるのがシンプルで良いかなとも思っています。

Discussion