Part 4 – Arm Template – Use Variable

Create Variable:

We can use variable similar to parameters to add values to each resource properties. The difference between them is variable use function like “concatenation” to combine multiple variables or parameters.

Below examples shows the changes being added as variables into template which creates unique storage name.

{
 "$schema": "https://schema.management.azure.com/schemas/2015-01-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"
     ]
   },
   "location": {
     "type": "string",
     "defaultValue": "[resourceGroup().location]"
   }
 },
 "variables": {
   "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
 },
 "resources": [
   {
     "type": "Microsoft.Storage/storageAccounts",
     "apiVersion": "2019-04-01",
     "name": "[variables('uniqueStorageName')]",
     "location": "[parameters('location')]",
     "sku": {
       "name": "[parameters('storageSKU')]"
     },
     "kind": "StorageV2",
     "properties": {
       "supportsHttpsTrafficOnly": true
     }
   }
 ]
}

uniqueStorageName: is a variable being created which uses four functions to  construct a string value.

uniqueString:  this function creates 13 character has value.

Concat: This functions construct or combine multiple parameter/variables together.

you can get more details about functions from this link:

https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-deployment

Deploy template

New-AzResourceGroupDeployment `
  -Name addnamevariable `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -storagePrefix "store" `
  -storageSKU Standard_LRS
Exit mobile version