Exercise - Use Azure Resource Manager variables to store expressions
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.
In Visual Studio Code, in the azuredeploy.json file, place your cursor between the braces in the variables block
"variables":{}
and press Enter.Type var inside the braces. You see a list of related snippets. Select arm-variable:
Your variables section looks like this code:
"variables": {"variable1": "value"},
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.Use the variable in the resources section. Change the values of the
name:
anddisplayName
attributes to "[variables('uniqueStorageName')]"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}