A quick tip for debugging Azure ARM template expressions

Azure Resource Manager (ARM) templates have a nice set of built-in functions that allow you to develop complex expressions. These expressions can help a static deployment template file become more scalable and flexible for different scenarios. This article is a quick rundown on my new favorite tip for debugging a template expression that you just can’t get working. 

Tip: Run a deployment with no resources and place your expression in the outputs section

The cool thing here is that you can actually initiate an ARM template deployment that has no resources in it- Azure doesn’t mind at all. In fact it runs through the normal template validation process and still produces output variables if you have items in that section. 

First you simply start with an empty deployment JSON file. Then add in the minimum number of parameters you need. Lastly, construct the expression you are debugging into an output parameter. The reason for it to be an output parameter is so that you can see the result of your expression printed back to the screen for you. 

This approach saves time on templates of all sizes by not actually deploying or verifying resources. You can quickly iterate through different testing values or syntax and get faster feedback on if your template expression is actually valid. Start with a small test, work your way up, and add more pieces until you have your completed/working expression.

Example: Simple concat() function expression test

Scenario: You want a template to deploy a storage account. The name of that storage account should be constructed using multiple parameters (such as a name prefix, some sort of regional suffix, etc.). This is an easy expression using the concat() function and a couple input parameters.

Note: There better ways to accomplish this same goal- but this way makes for a great test example.

In our example we build the expression and test it as an output parameter. Once you have it working as expected, you would then remove it from outputs and place it in your resources block under an actual deployment.

Sample deployment JSON file with the test expression

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "StorageAccountNamePrefix": {
            "type": "string"
        },
        "StorageAccountRegionCode": {
            "type": "string"
        }
    },
    "variables": {  },
    "functions": [  ],
    "resources": [  ],
    "outputs": {
        "MyExpressionResult": {
            "type": "string",
            "value": "[concat(parameters('StorageAccountNamePrefix'), parameters('StorageAccountRegionCode'))]"
        }
    }
}

Sample PowerShell to run this deployment

New-AzureRmResourceGroupDeployment -ResourceGroupName 'TestRG' -TemplateFile .\Desktop\deployment.json -StorageAccountNamePrefix 'mystorage' -StorageAccountRegionCode 'uk'

Resulting expression output on the screen

One thought on “A quick tip for debugging Azure ARM template expressions

  1. BenV January 29, 2019 / 9:48 am

    Wish you could run these test templates from the Azure Portal using the Template Deployment resource, but it won’t let you execute a template without resources.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s