Exercise - Use Azure Resource Manager variables to store expressions

Completed

In this exercise, you store your Azure storage account name expression in an Azure Resource Manager (ARM) template variable. Then, you use that variable to specify the name of the storage account to create.

In this exercise, we use the Azure Resource Manager Tools for Visual Studio Code. Be sure to install this extension in Visual Studio Code.

Add a variable

Add a variable to store your storage account name expression in one place in the template.

  1. In Visual Studio Code, in the azuredeploy.json file, place your cursor between the braces in the variables block "variables":{} and press Enter.

  2. Type var inside the braces. You see a list of related snippets. Select arm-variable:

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

  3. Your variables section looks like this code:

    "variables": {"variable1": "value"},
    
  4. Change the name of the variable to uniqueStorageName, and change the value to "[toLower(concat(parameters('storagePrefix'),uniqueString(resourceGroup().id)))]". Your variables section looks like this code:

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

    Notice that you're using the storagePrefix parameter in the expression instead of a literal string. Otherwise, this expression is the same as the one you learned about in the previous unit.

  5. Use the variable in the resources section. Change the values of the name: and displayName attributes to "[variables('uniqueStorageName')]"

  6. The entire file looks like this example:

    {
        "$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": {}
    }
    

Optionally, deploy the template

The updated template doesn't have any changes to the resource you deployed, so deploying this template doesn't make any changes to your Azure environment.

If you want to deploy the template to see it succeed, use the following Azure CLI commands. Be sure to use the same storagePrefix parameter value that you used in the last deployment.

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

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

If you want to deploy the template to see it succeed, use the following Azure PowerShell commands. Be sure to use the same storagePrefix parameter value that you used in the last deployment.

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