😀

Azure Storage Account の Blob コンテナーにカスタムドメインでアクセスする検証をしてみた

に公開

パブリッククラウド側であらかじめ用意されているドメイン、例えば Azure App Service の *.azurewebsites.net のまま運用しているケースをよく見かけます。カスタムドメインや SSL/TLS サーバー証明書を別途用意しなくて良い反面、別のクラウドやオンプレに変更したり、Azure App Service の手間に Azure Front Door を追加する構成に変更する場合など、運用中の FQDN は変更先では使えないので、何かと面倒な事になります。

今回は、Azure Storage Account の Blob コンテナーの FQDN を、Azure が用意しているものではなく、カスタムドメインでアクセスできるようにする検証を行ってみました。

検証用の Blob コンテナーを作成

# 環境変数をセットします
prefix=mnrblob

# リソースグループを作成します
az group create \
  --name ${prefix}-rg \
  --location japaneast

# Storage Account を作成します
az storage account create \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --sku Standard_LRS

# Blob コンテナーを作成します
az storage container create \
  --account-name ${prefix} \
  --name ${prefix} \
  --public-access blob

# 検証用のテキストファイルを作成します
LANG=C date > date.txt

# Blob コンテナーに検証用のテキストファイルをアップロードします
az storage blob upload \
  --account-name ${prefix} \
  --container-name ${prefix} \
  --file date.txt \
  --name date.txt

# Azure が用意している FQDN を使って検証用テキストファイルにアクセスします
curl https://${prefix}.blob.core.windows.net/${prefix}/date.txt

# 検証用テキストファイルを削除します
rm date.txt

Storage Account にカスタムドメインを設定

# Azure DNS に Storage Account 用のカスタムドメインを登録します
az network dns record-set cname set-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name ${prefix} \
  --ttl 300 \
  --cname ${prefix}.blob.core.windows.net

# Azure DNS にドメイン所有者確認用のカスタムドメインを登録します
az network dns record-set cname set-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name awverify.${prefix} \
  --ttl 300 \
  --cname awverify.${prefix}.blob.core.windows.net

# Storage Account にカスタムドメインを登録します
az storage account update \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --custom-domain ${prefix}.mnrsdev.com \
  --use-subdomain false

# サーバー証明書を無視するオプションをつけてアクセスできるか確認します
curl -k https://${prefix}.mnrsdev.com/${prefix}/date.txt

# Storage Account 単体ではサーバー証明書を登録できないので一旦削除します
az storage account update \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --custom-domain ""

# Azure DNS に登録したカスタムドメインを削除します
az network dns record-set cname remove-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name ${prefix} \
  --cname ${prefix}.blob.core.windows.net

# Azure DNS に登録したカスタムドメインを削除します
az network dns record-set cname remove-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name awverify.${prefix} \
  --cname awverify.${prefix}.blob.core.windows.net

Azure CDN にカスタムドメインとマネージドサーバー証明書を登録

# Azure CDN を作成します
az cdn profile create \
  --name ${prefix}-cdn \
  --resource-group ${prefix}-rg \
  --sku Standard_Microsoft

# Azure CDN に Storage Account を登録します
az cdn endpoint create \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --profile-name ${prefix}-cdn \
  --no-http \
  --enable-compression \
  --origin ${prefix}.blob.core.windows.net \
  --origin-host-header ${prefix}.blob.core.windows.net

# サーバー証明書を無視するオプションをつけてアクセスできるか確認します
curl -k https://${prefix}.azureedge.net/${prefix}/date.txt

# Azure DNS に Azure CDN 用のカスタムドメインを登録します
az network dns record-set cname set-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name ${prefix}cdn \
  --ttl 300 \
  --cname ${prefix}.azureedge.net

# Azure DNS にドメイン所有者確認用のカスタムドメインを登録します
az network dns record-set cname set-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name cdnverify.${prefix}cdn \
  --ttl 300 \
  --cname cdnverify.${prefix}.azureedge.net

# Azure CDN にカスタムドメインを登録します
az cdn custom-domain create \
  --resource-group ${prefix}-rg \
  --endpoint-name ${prefix} \
  --profile-name ${prefix}-cdn \
  --name ${prefix}cdn \
  --hostname ${prefix}cdn.mnrsdev.com

# サーバー証明書を無視するオプションをつけてアクセスできるか確認します
curl -k https://${prefix}cdn.mnrsdev.com/${prefix}/date.txt

# Azure CDN のマネージドサーバー証明書を有効にします
az cdn custom-domain enable-https \
  --resource-group ${prefix}-rg \
  --endpoint-name ${prefix} \
  --profile-name ${prefix}-cdn \
  --name ${prefix}cdn

# Azure CDN のマネージドサーバー証明書が有効になるのを気長に待ちます
# 何度かこのコマンドを実行し "Enabling" が "Enabled" になれば完了です
az cdn custom-domain show \
  --resource-group ${prefix}-rg \
  --endpoint-name ${prefix} \
  --profile-name ${prefix}-cdn \
  --name ${prefix}cdn \
  --query customHttpsProvisioningState

# カスタムドメインのサーバー証明書が有効な状態でアクセスできるか確認します
curl https://${prefix}cdn.mnrsdev.com/${prefix}/date.txt

参考

https://learn.microsoft.com/ja-jp/azure/storage/blobs/storage-custom-domain-name?tabs=azure-cli

https://learn.microsoft.com/ja-jp/azure/cdn/scripts/cli/cdn-azure-cli-create-endpoint

Discussion