演習 - Azure Resource Manager 変数を使用して式を格納する

完了

この演習では、Azure ストレージ アカウント名の式を Azure Resource Manager (ARM) テンプレート変数に格納します。 次に、その変数を使用して、作成するストレージ アカウントの名前を指定します。

この演習では、 Azure Resource Manager Tools for Visual Studio Code を使用します。 この拡張機能を Visual Studio Code にインストールしてください。

変数を追加する

ストレージ アカウント名式をテンプレート内の 1 か所に格納する変数を追加します。

  1. Visual Studio Code の azuredeploy.json ファイルで、変数ブロック "variables":{} の中かっこの間にカーソルを置き、 Enter キーを押します。

  2. 中かっこ内に var を入力します。 関連するスニペットの一覧が表示されます。 arm-variable を選択します。

    Azure Resource Manager テンプレート変数のスニペットを示す Visual Studio Code のスクリーンショット。

  3. 変数セクションは次のコードのようになります。

    "variables": {"variable1": "value"},
    
  4. 変数の名前を uniqueStorageName に変更し、値 を "[toLower(concat(parameters('storagePrefix'),uniqueString(resourceGroup().id)))]" に変更します。 変数セクションは次のコードのようになります。

    "variables": {
        "uniqueStorageName": "[toLower(concat(parameters('storagePrefix'),uniqueString(resourceGroup().id)))]"
      },
    

    リテラル文字列ではなく、式で storagePrefix パラメーターを使用していることに注意してください。 それ以外の場合、この式は前のユニットで学習した式と同じです。

  5. resources セクションの変数を使用します。 name:属性とdisplayName属性の値を "[variables('uniqueStorageName')]" に変更します。

  6. ファイル全体は次の例のようになります。

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
           "storagePrefix": {
               "type": "string",
               "minLength": 3,
               "maxLength": 11
           },
            "storageSKU": {
                "type": "string",
                "defaultValue": "Standard_LRS",
                "allowedValues": [
                    "Standard_LRS",
                    "Standard_GRS",
                    "Standard_RAGRS",
                    "Standard_ZRS",
                    "Premium_LRS",
                    "Premium_ZRS",
                    "Standard_GZRS",
                    "Standard_RAGZRS"
                ]
            }
       },
        "functions": [],
        "variables": {
        "uniqueStorageName": "[toLower(concat(parameters('storagePrefix'),uniqueString(resourceGroup().id)))]"
      },
        "resources": [{
            "name": "[variables('uniqueStorageName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "tags": {
                "displayName": "[variables('uniqueStorageName')]"
            },
            "___location": "[resourceGroup().___location]",
            "kind": "StorageV2",
            "sku": {
             "name": "[parameters('storageSKU')]"
           }
        }],
        "outputs": {}
    }
    

必要に応じて、テンプレートをデプロイします

更新されたテンプレートにはデプロイしたリソースに変更がないため、このテンプレートをデプロイしても Azure 環境に変更は加えられません。

テンプレートをデプロイして成功したことを確認する場合は、次の Azure CLI コマンドを使用します。 必ず、前回のデプロイで使用したのと同じ storagePrefix パラメーター値を使用してください。

templateFile="azuredeploy.json"
today=$(date +"%d-%b-%Y")
DeploymentName="addVariable-"$today

az deployment group create \
  --name $DeploymentName \
  --template-file $templateFile \
  --parameters storagePrefix={your-Prefix}

テンプレートをデプロイして成功したことを確認する場合は、次の Azure PowerShell コマンドを使用します。 必ず、前回のデプロイで使用したのと同じ storagePrefix パラメーター値を使用してください。

$templateFile = "azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addVariable-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile `
  -storagePrefix {your-Prefix}