PowerShell を使用してカスタム ドメインを Web アプリに割り当てる

このサンプル スクリプトでは、App Service で Web アプリを関連リソースと合わせて作成し、www.<yourdomain> を作成したアプリにマップします。

必要に応じて、Azure PowerShell ガイドの手順に従って Azure PowerShell をインストールし、Connect-AzAccount を実行して、Azure との接続を作成します。 また、ドメイン レジストラーの DNS 構成ページにアクセスできることも必要です。

サンプル スクリプト

注意

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

$fqdn="<Replace with your custom domain name>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"

# Create a resource group.
New-AzResourceGroup -Name $webappname -Location $location

# Create an App Service plan in Free tier.
New-AzAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName $webappname -Tier Free

# Create a web app.
$webapp = New-AzWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName $webappname

Write-Host "Sign in to your domain provider's website and configure the following records:"
Write-Host "A CNAME record that maps $fqdn to $webappname.azurewebsites.net"
Write-Host "A TXT record that maps asuid.$fqdn to the domain verification ID $($webapp.CustomDomainVerificationId)"
Read-Host "Press [Enter] key when ready ..."

# Before continuing, go to your DNS configuration UI for your custom domain and follow the 
# instructions at https://aka.ms/appservicecustomdns to configure a CNAME record for the 
# hostname "www" and point it your web app's default domain name.

# Upgrade App Service plan to Shared tier (minimum required by custom domains)
Set-AzAppServicePlan -Name $webappname -ResourceGroupName $webappname `
-Tier Shared

# Add a custom domain name to the web app. 
Set-AzWebApp -Name $webappname -ResourceGroupName $webappname `
-HostNames @($fqdn,"$webappname.azurewebsites.net")

デプロイのクリーンアップ

サンプル スクリプトの実行後、次のコマンドを使用すると、リソース グループ、Web アプリ、およびすべての関連リソースを削除できます。

Remove-AzResourceGroup -Name myResourceGroup -Force

スクリプトの説明

このスクリプトでは、次のコマンドを使用します。 表内の各コマンドは、それぞれのドキュメントにリンクされています。

コマンド メモ
New-AzResourceGroup すべてのリソースを格納するリソース グループを作成します。
New-AzAppServicePlan App Service プランを作成します。
New-AzWebApp Web アプリを作成します。
Set-AzAppServicePlan App Service プランを変更して価格レベルを変更します。
Set-AzWebApp Web アプリの構成を変更します。

次のステップ

Azure PowerShell モジュールの詳細については、Azure PowerShell のドキュメントを参照してください。

その他の Azure App Service Web Apps 用 Azure PowerShell サンプルは、Azure PowerShell サンプルのページにあります。