image Check Azure CosmosDB “currency” from code and adapt the throughput image Microsoft Ignite – The tour in Amsterdam

Validate Azure ARM templates with PowerShell

If you are writing and using ARM templates as as a base of your infrastructure as code (IaC) principle, then the next few lines might be interesting to you. If you are new to IaC read this or if you are new to ARM templates read here.

We are using ARM templates with Azure DevOps to deploy resource groups of Virtual machines, databases and Application services. Templates tend to get large and even with a Visual Studio Intellisense which is good by the way, you can make mistakes. What we need is a tool that can validate the whole script before you commit/push to the source control. I was thinking, “is there a way to do this?” and indeed there is.

Microsoft documentation states that there is a tool (PowerShell) that can check your ARM templates for compatibility with Azure Stack in your region of choice. It also checks the JSON syntax and some other goodies (examples below). Let’s get started.

1. Download and install Azure Stack tools.

2. Unpack it to the folder of your choice and “cd” to the Azure Stack Tools folder.

3. Import Cloud capabilities module with the command below. The instructions in the documentation are not very clear, regarding the “<your location>” parameter. But I made some digging and that is of course Azure location (run Get-AzureRMLocation in PowerShell to get your location name). We use westeurope for a location parameter.

It takes some time 30 min or so, to generate capabilities JSON file, which will be then used by our next script Template Validator. This JSON file contains all the capabilities (all resources) in the selected region. It’s probably good to update it once in a while.

4. Then import Template validator module and finally run a validation of your ARM templates. You need to specify the -TemplatePath and -CapabilitiesPath parameters:

Because I set parameter -Verbose in the command above, I get a lot of feedback to the command line.

Verbose mode when running Test-AzureRMTemplate
Figure 1: Verbose mode when running Test-AzureRMTemplate.

Below are some examples of validation warnings and errors, that a tool can detect. Tool returns warnings like hardcoded URLs or wrong API version or errors like invalid JSON syntax or misspelled Azure resource type.

Warning message because of the hard coded value for Azure Storage URL.
Figure 2: Warning message because of the hard coded value for Azure Storage URL.
Invalid JSON syntax error message.
Figure 3: Invalid JSON syntax error message.
Misspelled Azure resource type error.
Figure 4: Misspelled Azure resource type error.
It successfully detects wrong API versioning values.
Figure 5: It successfully detects wrong API versioning values.

Not everything is validated at the moment (like Azure virtual machine type or offer) and this might be an improvement for the future.

There are some things I would like to try/see in the future regarding the ARM templates validation:

  1. A way to run validation easily inside Visual Studio or Visual Studio Code (if you know how to do it let me know).
  2. Integrate ARM template validation to Azure DevOps build pipeline.
  3. Add validation for Azure resource properties like virtual machine types, offers etc. I don’t see any reason why this could not be compiled through Cloud capabilities JSON file.

That’s all for this post. Let me know if you have any comment down below.

About Miha
I've always been fascinated by computers. I am happy that I can work in this creative field of software development, where I can solve problems. I am also an avid cyclist :) and I love to learn...a lot!
Related Posts
  • All
  • By Author
  • By Category
  • By Tag

Leave a Reply