Exercise - Use Azure Resource Manager tags and parameter files

Completed

In this exercise, you add tags to help organize and track your Microsoft Azure resources. You also use an Azure Resource Manager (ARM) template parameter file to allow for different parameter configurations for each deployment.

This exercise uses the Azure Resource Manager Tools for Visual Studio Code. Be sure to install this extension in Visual Studio Code.

Create a tag to track the resource deployment environment and project

First, you create a parameter to use as a resource tag in your template.

  1. In Visual Studio Code, in the azuredeploy.json file, place your cursor after the closing brace for the storageSKU parameter. Add a comma and press Enter.

  2. Type par. You see a list of related snippets.

  3. Select arm-param. Remember, this action adds a generic parameter to the template. It looks like this code:

    "parameter1": {
        "type": "string",
        "metadata": {
            "description": "description"
        }
    
  4. Change parameter1 to resourceTags and change the value of "type": to object. Remember that parameters can have string, secureString, int, bool, object, secureObject, and array data types. A link to example syntax for these parameter types is in the summary of this module.

  5. Add an attribute called defaultValue: and set the value to {"Environment": "Dev", "Project": "Tutorial"}.

    The parameter block should look like this code:

    "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"
            ]
        },
        "resourceTags": {
        "type": "object",
        "defaultValue": {
            "Environment": "Dev",
            "Project": "Tutorial"
            }
        }
    },
    
  6. Use this parameter to tag your storage account resource. Change the tags: attribute in the resource definition:

    "tags": "[parameters('resourceTags')]",
    
  7. Your file should look like this file:

    {
        "$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"
                ]
            },
            "resourceTags": {
            "type": "object",
            "defaultValue": {
                "Environment": "Dev",
                "Project": "Tutorial"
            }
        }
       },
        "functions": [],
        "variables": {
        "uniqueStorageName": "[toLower(concat(parameters('storagePrefix'),uniqueString(resourceGroup().id)))]"
      },
        "resources": [{
            "name": "[variables('uniqueStorageName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "tags": "[parameters('resourceTags')]",
            "location": "[resourceGroup().location]",
            "kind": "StorageV2",
            "sku": {
             "name": "[parameters('storageSKU')]"
           }
        }],
        "outputs": {}
    }
    
  8. Save the file.

Deploy the ARM template with updated tags

  • Deploy the updated ARM template to Azure. Be sure to use the same storagePrefix that you used before.

    templateFile="azuredeploy.json"
    today=$(date +"%d-%b-%Y")
    DeploymentName="updateTags-"$today
    
    az deployment group create \
        --name $DeploymentName \
        --template-file $templateFile \
        --parameters storagePrefix={your-Prefix} storageSKU=Standard_LRS
    
  • Deploy the updated ARM template to Azure. Be sure to use the same storagePrefix that you used before.

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

Verify that the new tags are in the deployment

  1. In Azure, select the [sandbox resource group name] resource group, then select the storage account you deployed.

  2. Notice the Environment : Dev and Project : Tutorial tags:

    Screenshot of the Azure portal that shows the selected tags in the Storage account Overview page.

Use a parameter file

There are currently three parameters to fill in each time you deploy this template. Each user of the template can create a file to hold their parameter values. Here, you create a parameter file to use with your template.

  1. In Visual Studio Code, create another file. Call it azuredeploy.parameters.dev.json.

  2. In this file, you add the values for the template parameters that you want to have input into the template for the development environment. Change a tag value to see that the deployment makes a change. For example, you could change projectName to Learn:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "storagePrefix": {
          "value": "{unique-prefix}"
        },
        "storageSKU": {
          "value": "Standard_LRS"
        },
        "resourceTags": {
          "value": {
            "Environment": "Dev",
            "Project": "Learn"
          }
        }
      }
    }
    
  3. Be sure to replace {unique-prefix} with your unique prefix.

  4. Save the file.

Deploy the template with the parameter file

In this section, you deploy the ARM template, specifying which parameter file to use.

  1. In the Visual Studio Code terminal, run these Azure CLI commands:

    templateFile="azuredeploy.json"
    devParameterFile="azuredeploy.parameters.dev.json"
    today=$(date +"%d-%b-%Y")
    DeploymentName="addParameterFile-"$today
    
    az deployment group create \
      --name $DeploymentName \
      --template-file $templateFile \
      --parameters $devParameterFile
    
  2. Check Azure to ensure that the deployment was successful and that the tag value changed:

    Screenshot of the Azure portal that shows the updated tag values in the Storage account Overview page.

  3. As a challenge, create a parameter file for the production environment. Change the parameter file path when you run the command to deploy to the production environment.

  1. In the Visual Studio Code terminal, run these Azure PowerShell commands:

    $templateFile = "azuredeploy.json"
    $parameterFile="azuredeploy.parameters.dev.json"
    $today=Get-Date -Format "MM-dd-yyyy"
    $deploymentName="addParameterFile-"+"$today"
    New-AzResourceGroupDeployment `
      -Name $deploymentName `
      -TemplateFile $templateFile `
      -TemplateParameterFile $parameterFile
    
  2. Check Azure to ensure that the deployment was successful and that the tag value changed:

    Screenshot of the Azure portal that shows the updated tag values in the Storage account Overview page.

  3. As a challenge, create a parameter file for the production environment. Change the parameter file path when you run the command to deploy to the production environment.