Search code examples
continuous-integrationazure-resource-managerazure-rm-template

environment agnostic ARM template parameter file


I have a (simplified) ARM template for an Storage Account deployment:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters":{
    "env_storage_name": {
      "type": "string"
    }
  },
  "resources":[
    {
        "type": "Microsoft.Storage/storageAccounts",
        "name":"[parameters('env_storage_name')]",
        "apiVersion": "2019-06-01"
    }
  ]
}

This template uses the associated parameter file:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "env_storage_name": {
            "value": "<env>-storage_name"
        }
    }
}

My goal is to deploy this template using Powershell by:

az deployment group create --name "someDeploymentName" --resource-group "myRG" --template-file "pathToTemplate.json" --parameters "pathToParameters.json"

I have googled quite a bit, but so far I havent found a way to dynamically replace the env substring in my parameter with e.g. "dev", "tst", "acc", "prd".What I want to do is effectively use the same Powershell command an "pipe in" the environment. I have tried to play around with variables, but so far I haven't managed to get it done.

Is that possible? Can anyone help me out?


Solution

  • The solution that solves my problem is, to use variables to concatinate my relevant resource names etc from parameters. It took me a while understand the possibilities that ARM gives you here.

    I am now using this template:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "subscription": {
            "type": "string"
          },
            "env": {
            "type": "string"
          }
        },
        "variables": {
            "storage_name":  "[concat(parameters('subscription'),'-',parameters('env'),'-storage_account_name')]"
        },
        "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "name":"[variables('storage_name')]",
              "apiVersion": "2019-06-01"
          }
        ]
    }
    

    and feed it with this parameter file:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
                "subscription": {
                "value": "mySub"
            },
                "env": {
                "value": "dev"
            }
        }
    }
    

    So the only thing that I have to do is change the "env" parameter in the parameter file.

    This does NOT give me the possibility to push a button and all four environments are created sequentially, but that is not part of the requirements atm. I still have to manually change "dev" to "tst" for example and rerun my PowerShell script, but that is acceptable for me.