ストレージ アカウントへの App Service アプリの接続

このシナリオでは、Azure ストレージ アカウントと App Service アプリの作成方法について説明します。 作成後、アプリの設定を使用してストレージ アカウントをアプリにリンクします。

必要に応じて、Azure PowerShell ガイドの手順に従って Azure PowerShell をインストールし、Connect-AzAccount を実行して、Azure との接続を作成します。

サンプル スクリプト

注意

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


# Generates a Random Value
$Random=(New-Guid).ToString().Substring(0,8)

# Variables
$ResourceGroup="MyResourceGroup$Random"
$AppName="webappwithStorage$Random"
$StorageName="webappstorage$Random"
$Location="West US"

# Create a Resource Group
New-AzResourceGroup -Name $ResourceGroup -Location $Location

# Create an App Service Plan
New-AzAppservicePlan -Name WebAppwithStoragePlan -ResourceGroupName $ResourceGroup -Location $Location -Tier Basic

# Create a Web App in the App Service Plan
New-AzWebApp -Name $AppName -ResourceGroupName $ResourceGroup -Location $Location -AppServicePlan WebAppwithStoragePlan

# Create Storage Account
New-AzStorageAccount -Name $StorageName -ResourceGroupName $ResourceGroup -Location $Location -SkuName Standard_LRS

# Get Connection String for Storage Account
$StorageKey=(Get-AzStorageAccountKey -ResourceGroupName $ResourceGroup -Name $StorageName).Value[0]

# Assign Connection String to App Setting 
Set-AzWebApp -ConnectionStrings @{ MyStorageConnStr = @{ Type="Custom"; Value="DefaultEndpointsProtocol=https;AccountName=$StorageName;AccountKey=$StorageKey;" } } -Name $AppName -ResourceGroupName $ResourceGroup

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

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

Remove-AzResourceGroup -Name myResourceGroup -Force

スクリプトの説明

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

コマンド メモ
New-AzResourceGroup すべてのリソースを格納するリソース グループを作成します。
New-AzAppServicePlan App Service プランを作成します。
New-AzWebApp App Service アプリを作成します。
New-AzStorageAccount ストレージ アカウントを作成します。
Get-AzStorageAccountKey Azure ストレージ アカウントのアクセス キーを取得します。
Set-AzWebApp App Service アプリの構成を変更します。

次のステップ

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

その他の Azure App Service 用 Azure PowerShell サンプル スクリプトは、Azure PowerShell サンプルのページにあります。