Exercise - Use Azure Resource Manager tags and parameter files
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.
Note
This exercise is optional. If you want to complete this exercise, you'll need to create an Azure subscription before you begin. If you don't have an Azure account or you don't want to create one at this time, you can read through the instructions so you understand the information that's being presented.
Note
In this unit, you use Azure Cloud Shell as a terminal. You can access Cloud Shell through the Azure portal or the Cloud Shell sign-in. You don't have to install anything on your PC or laptop to use it.
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.
In Visual Studio Code, in the azuredeploy.json file, 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" } } },Use this parameter to tag your storage account resource. Change the
tags:attribute in the resource definition:"tags": "[parameters('resourceTags')]",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": "2025-01-01", "tags": "[parameters('resourceTags')]", "location": "[resourceGroup().location]", "kind": "StorageV2", "sku": { "name": "[parameters('storageSKU')]" } } ], "outputs": {} }Save the file.
Deploy the ARM template with updated tags
Deploy the updated ARM template to Azure. Be sure to use the same
storagePrefixthat 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
storagePrefixthat 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
In Azure, select your resource group name, then select the storage account you deployed.
Notice the Environment : Dev and Project : Tutorial tags:
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.
In Visual Studio Code, create another file. Call it azuredeploy.parameters.dev.json.
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
projectNameto 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" } } } }Be sure to replace
{unique-prefix}with your unique prefix.Save the file.
Deploy the template with the parameter file
In this section, you deploy the ARM template, specifying which parameter file to use.
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 $devParameterFileCheck Azure to ensure that the deployment was successful and that the tag value changed:
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.
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 $parameterFileCheck Azure to ensure that the deployment was successful and that the tag value changed:
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.