DevOps | Scripts | Automation

ARM Templates

How to add Parameters in the ARM template?

In this article,
– We will add the Parameters for the Storage Account in the parameters section.
– We will apply parameters to the Resources
– How we can use the parameters in different ways.?

If you haven’t installed the extension for the ARM template in VS code yet, follow the below link to install and active the ARM template extension in VS Code.

https://theautomationcode.com/arm-template-extension-with-visual-studio-code/

To learn more about Azure Parameters, you can check the MS content below.

https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/parameters

We have the below sample ARM template structure.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "functions": [],
    "variables": {},
    "resources": [],
    "outputs": {}
}

We will now add the storage in the resources section. Once we start typing the resource name (here storage), it will automatically pop up the resource suggestions. Click on the arm-storage.

If not, you can use the ctrl + space button to pop up the resources in this section. Once you click on the arm-storage it will load the default schema.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "functions": [],
    "variables": {},
    "resources": [
       {
           "name": "storageaccount1",
           "type": "Microsoft.Storage/storageAccounts",
           "apiVersion": "2019-06-01",
           "tags": {
               "displayName": "storageaccount1"
           },
           "location": "[resourceGroup().location]",
           "kind": "StorageV2",
           "sku": {
               "name": "Premium_LRS",
               "tier": "Premium"
           }
       }
    ],
    "outputs": {}
}

Why to Add Parameters?

  • We will now add the parameters for the storage account. The benefit of adding the parameters is we can make the template reusable, like the PowerShell function, we can pass the different values each time for the resources in the resource group deployment.
  • We can also use the parameter file to add all the required values at the one place and provide this file as the input file.

Adding Parameters to the Parameter Section.

We will first pass the storage account names from the parameters section and use storageAccountName as the parameter.

We will start with minimal and provide the type and the default value for the storage account name. You can add the various types as shown in the popup box. We need a “string” type here.

To add the default value so if not passed in the command line, it takes the default value.

Using Parameters to the Resources section

To use the parameter outside its domain, we need to call it and it can be called using functions. Here we will use the parameter function. Inside the name, inside the double quote, you need to provide the square bracket “[]” and then need to type the Parameters() function, and inside you can provide the parameter name as shown below.

We have only one parameter, so it will show only that.

Deploying Template with Parameters.

Alright, now we have the Storage ARM template ready for deployment and we have also added the parameters.
The ARM template is deployed on the Azure Resource Group. So to deploy the ARM template we will create one empty resource group (you can also use the existing resource group).

New-AzResourceGroup -Name ARMTemplateTest -Location "centralus"

We have a now new resource group “ARMTemplateTest” in the “centralus” location. We will deploy the ARM template using the PowerShell command New-AzResourceGroupDeployment.

New-AzResourceGroupDeployment -Name StorageDeploy -ResourceGroupName ARMTemplateTest -TemplateFile C:\Temp\Test.json -Verbose

Output:

On the Azure portal site you can check,