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

完了

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

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

変数を追加する

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

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

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

    Screenshot of Visual Studio Code that shows the snippets for Azure Resource Manager template variables.

  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}