0

I'm trying to make my parameter file a bit smarter but for the life of me can't figure out how to do so. I have a parameters.json file with 2 params: env & commonTags. env takes a string from my DevOps pipeline, and I need this parameter to fill a value in the commonTags-object parameter. See code snippet below:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
  "env": {
  },
  "location": {
    "value": "westeurope"
  },
  "commonTags": {
    "value": {
        "contact":"dr@balloon.com",
        "costcenter":       "99999",
        "env":              "[parameters('env')]",
        "criticality":      "[parameters('env') == 'prd' ? 'high', 'low']"
    }     
}
}

}

The only other option I see is to set the env-specific parameters in the template file. Either by merging with the existing parameters or by setting the value of commonTags in the template file entirely. But I'd rather keep my template files free of parameter values and have these all located in a central parameter file.

Can anybody point me in the right direction? I can't seem to find anything online.

Many thanks!

Sam
  • 99
  • 2
  • 7

1 Answers1

2

You cannot nest parameters as you are trying to do here.

Instead, you can make use of conditional directly in your main arm template coupled with inline parameters. Let's take a simple Storage Account as an example.

azuredeploy.json

    {
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string"
        },
        "env": {
            "type": "string"
        },
        "costcenter": {
            "type": "string"
        },
        "contact": {
            "type": "string"
        },
        "storageAccountType": {
            "type": "string",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_ZRS",
                "Premium_LRS"
            ],
            "metadata": {
                "description": "Storage Account type"
            }
        }
    },
    "variables": {
        "storageAccountName": "[concat('store', uniquestring(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "name": "[variables('storageAccountName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[parameters('storageAccountType')]"
            },
            "tags": {
                "contact": "[parameters('contact')]",
                "costcenter": "[parameters('costcenter')]",
                "env": "[parameters('env')]",
                "criticality": "[if(equals(parameters('env'),'prd'),'high','low')]"
            },
            "kind": "StorageV2"
        }
    ]
}

azuredeploy.parameters.json

    {
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageAccountType": {
            "value": "Standard_LRS"
        },
        "costcenter": {
            "value": "99999"
        },
        "contact": {
            "value": "dr@balloon.com"
        },
        "location": {
            "value": "westeurope"
        }
    }
}

Note that the tag names remain static - there are ways to pass them inline instead/as well - see this answer.

From these, you can then use Az CLI or Powershell to deploy your template, the parameters kept in a single place and the dynamically provide the remaining one like env.

Example using Powershell :

New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $rgName -TemplateFile .\azuredeploy.json -TemplateParameterFile .\azuredeploy.parameters.json -env $env

Jul_DW
  • 1,036
  • 6
  • 20
  • Hi Jul_DW. Thanks for sharing your detailed answer. As I suspected it's not possible to create this logic in the parameter file. I'm not very fond of the parameters in ARM templates. If I want to use a single parameter file it forces me to add these parameters to all my main template files. I've now created a separate file called globalVars and I'm misusing outputs as parameters and calling these as modules in my template files. – Sam Nov 16 '21 at 09:02