Scripts and templates used in this post can be found at: https://github.com/devkimchi/Linked-ARM-Templates-Sample
We have a master template,
master-deployment.json, and it looks like:
Each nested template has a parameter called
environment that has the same value as the one in the master template. Each template also has a corresponding parameter file. Therefore, as above, we use both
parameters properties to handle both. However, life is not easy.
Oooops! We can’t use both
parameters at the same time. We have to use one or the other. This is by design at the time of this writing. Because the
environment parameter is a common denominator across all the nested templates, it is natural to think that the parameter can be passed from master to nested ones.
How can we work this out then? There are several workarounds.
- Add the common parameters to each parameter JSON file and upload it to Azure Storage Account programatically.
- Create a set of parameter files for all possible combinations.
- Use something to refer during the template deployment, whose value will be changing over time during the deployment but the reference point remains the same.
Update Parameters Programatically
The easiest way is the first option. It can be simply achieved by running a separate PowerShell script before the deployment. Let’s have a look.
This is merely to upload nested template files to Azure Storage Account. It excludes parameter files because they need to be updated before being uploaded. Let’s have a look at the following PowerShell script.
The core part of the script is:
- To read parameter file as a JSON object,
- To add the
environmentproperty to the JSON object, and
- To overwrite the parameter file.
Then this updated parameter files are uploaded to Azure Storage Account. Now our Storage Account has got all nested templates and their parameter files. Let’s run the master template again without the
Tada~! It all works fine!
So far, we’ve taken a look how to sort out the restriction that we can’t use both
parameters properties at the same time. We obviously need a help of another script to run linked templates with their parameters by updating it. It’s definitely not an ideal scenario but certainly it works. If this update is done by hand, it’s tedious, which should be avoided. However, in CI/CD pipelines, it wouldn’t be an issue because we can automate it.