Azure

Part 5 – Arm Template – Use Functions

Create Functions:

{
 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
 "contentVersion": "1.0.0.0",
 "parameters": {
   "storageName": {
     "type": "string",
     "minLength": 3,
     "maxLength": 24
   },
   "storageSKU": {
     "type": "string",
     "defaultValue": "Standard_LRS",
     "allowedValues": [
       "Standard_LRS",
       "Standard_GRS",
       "Standard_RAGRS",
       "Standard_ZRS",
       "Premium_LRS",
       "Premium_ZRS",
       "Standard_GZRS",
       "Standard_RAGZRS"
     ]
   }
 },
 "resources": [
   {
     "type": "Microsoft.Storage/storageAccounts",
     "apiVersion": "2019-04-01",
     "name": "[parameters('storageName')]",
     "location": "eastus",
     "sku": {
       "name": "[parameters('storageSKU')]"
     },
     "kind": "StorageV2",
     "properties": {
       "supportsHttpsTrafficOnly": true
     }
   }
 ]
}

We remember our previous steps for creating variables in templates. In that template we already used function “[parameters(‘storageName’)]”, we used parameter function. That syntax inside that bracket is called template expression. Resource Manager checks the syntax instead of  treating it as a literal value.

Functions add flexibility to your template by dynamically getting values during deployment. In this tutorial, you use a function to get the location of the resource group you’re using for deployment.

All arm template function add more benefits to get values during deployment. In this examples below, we use a function to get the location of the resource group we are using for deployment.

We used the East US Region in our previous template. sometime we need to deploy to a different Region. For this we will use Parameter “location” .

The following example highlights the changes to add a parameter called location. The parameter default value calls the resourceGroup function. This function returns an object with information about the resource group being used for deployment. One of the properties on the object is a location property. When you use the default value, the storage account location has the same location as the resource group. The resources inside a resource group don’t have to share the same location. You can also provide a different location when needed.

Copy the whole file and replace your template with its contents.

{
 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
 "contentVersion": "1.0.0.0",
 "parameters": {
   "storageName": {
     "type": "string",
     "minLength": 3,
     "maxLength": 24
   },
   "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]"
   }
 },
 "resources": [
   {
     "type": "Microsoft.Storage/storageAccounts",
     "apiVersion": "2019-04-01",
     "name": "[parameters('storageName')]",
     "location": "[parameters('location')]",
     "sku": {
       "name": "[parameters('storageSKU')]"
     },
     "kind": "StorageV2",
     "properties": {
       "supportsHttpsTrafficOnly": true
     }
   }
 ]
}

Deploy template

We created the storage account in East Us in our previous deployment, but our Resource Group was created in Central US.

In this example we will create our storage account in same region as the Resource Group. We will use default value for location to avoid parameter value. We will use new name for our storage account.

New-AzResourceGroupDeployment `
  -Name addlocationparameter `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -storageName "{new-unique-name}"
Show More

Related Articles

Back to top button