Azure CLI を使用して Azure Content Delivery Network のプロファイルとエンドポイントを作成する

Azure portal の代わりに、これらのサンプル Azure CLI スクリプトを使用して、次のコンテンツ配信ネットワーク操作を管理できます。

  • コンテンツ配信ネットワークのプロファイルを作成します。
  • コンテンツ配信ネットワークのエンドポイントを作成します。
  • コンテンツ配信ネットワークの配信元グループを作成し、それを既定のグループにします。
  • コンテンツ配信ネットワークの配信元を作成します。
  • カスタム ドメインを作成し、HTTPS を有効にする。

前提条件

  • Azure Cloud Shell で Bash 環境を使用します。 詳細については、「Azure Cloud Shell の Bash のクイックスタート」を参照してください。

  • CLI リファレンス コマンドをローカルで実行する場合、Azure CLI をインストールします。 Windows または macOS で実行している場合は、Docker コンテナーで Azure CLI を実行することを検討してください。 詳細については、「Docker コンテナーで Azure CLI を実行する方法」を参照してください。

    • ローカル インストールを使用する場合は、az login コマンドを使用して Azure CLI にサインインします。 認証プロセスを完了するには、ターミナルに表示される手順に従います。 その他のサインイン オプションについては、Azure CLI でのサインインに関するページを参照してください。

    • 初回使用時にインストールを求められたら、Azure CLI 拡張機能をインストールします。 拡張機能の詳細については、Azure CLI で拡張機能を使用する方法に関するページを参照してください。

    • az version を実行し、インストールされているバージョンおよび依存ライブラリを検索します。 最新バージョンにアップグレードするには、az upgrade を実行します。

サンプルのスクリプト

コンテンツ配信ネットワーク プロファイルのリソース グループがまだない場合は、コマンド az group create を実行してこれを作成します。

# Create a resource group to use for the content delivery network.
az group create --name MyResourceGroup --location eastus

次の Azure CLI スクリプトでは、コンテンツ配信ネットワーク プロファイルとコンテンツ配信ネットワーク エンドポイントが作成されます。

# Create a content delivery network profile.
az cdn profile create --resource-group MyResourceGroup --name MyCDNProfile --sku Standard_Microsoft

# Create a content delivery network endpoint.
az cdn endpoint create --resource-group MyResourceGroup --name MyCDNEndpoint --profile-name MyCDNProfile --origin www.contoso.com

次の Azure CLI スクリプトでは、コンテンツ配信ネットワークの配信元グループが作成され、エンドポイントの既定の配信元グループが設定され、新しい配信元が作成されます。

# Create an origin group.
az cdn origin-group create --resource-group MyResourceGroup --endpoint-name MyCDNEndpoint --profile-name MyCDNProfile --name MyOriginGroup --origins origin-0

# Make the origin group the default group of an endpoint.
az cdn endpoint update --resource-group MyResourceGroup --name MyCDNEndpoint --profile-name MyCDNProfile --default-origin-group MyOriginGroup

# Create another origin for an endpoint.
az cdn origin create --resource-group MyResourceGroup --endpoint-name MyCDNEndpoint --profile-name MyCDNProfile --name origin-1 --host-name example.contoso.com

次の Azure CLI スクリプトでは、コンテンツ配信ネットワーク カスタム ドメインが作成され、HTTPS が有効になります。 カスタム ドメインを Azure Content Delivery Network エンドポイントに関連付けるためには、最初にコンテンツ配信ネットワーク エンドポイントを指す正規名 (CNAME) レコードを Azure DNS またはご利用の DNS プロバイダーで作成する必要があります。 詳細については、「CNAME DNS レコードを作成する」を参照してください。

# Associate a custom domain with an endpoint.
az cdn custom-domain create --resource-group MyResourceGroup --endpoint-name MyCDNEndpoint --profile-name MyCDNProfile --name MyCustomDomain --hostname www.example.com

# Enable HTTPS on the custom domain.
az cdn custom-domain enable-https --resource-group MyResourceGroup --endpoint-name MyCDNEndpoint --profile-name MyCDNProfile --name MyCustomDomain

リソースをクリーンアップする

サンプル スクリプトの実行が完了したら、次のコマンドを使用して、リソース グループとそれに関連付けられているすべてのリソースを削除します。

# Delete the resource group.
az group delete --name MyResourceGroup

この記事で使用されている Azure CLI コマンド