Using Empty/Optional Parameters in Azure ARM Template

Clackers picture Clackers · Oct 19, 2018 · Viewed 10.3k times · Source

I'm having issues setting up an ARM Template for Azure Web Apps in that I can't add ConnectionString parameters where it sets the values if the parameters are set, but leave blank (default) if the parameters aren't set.

Here is how it looks in the template.json file:

"connectionStrings": [
        {
          "name": "[parameters('connString').connName)]",
          "connectionString": "[parameters('connString').string]",
          "type": "[parameters('connString').connType]"
        }
      ],

And in the parameters.json file:

"connString": {
        "value": {
            "connName": "",
            "string": "",
            "connType": ""
        }
    },

When running the deployment with the above it fails on "Parameter name cannot be empty" I attempted to use an equals function to set the value as empty if the parameter is empty, but set the parameter if the parameter is filled out, however it doesn't like the empty value.

"name": "[if(equals(parameters('connString').connName,''),'',parameters('connString').connName)]"

Also attempted an empty function:

 "name": "[not(empty(parameters('connString').connName))]"

However this returns "False" if empty and "True" if the parameter is set (as designed)

The deployment works fine if I set dummy values as the parameters, is it possible to set a function or something similar so if the parameter is empty it uses whatever value is sent as if the connectionStrings section wasn't present in the template? These parameters are optional but it looks like because they're in the actual template.json file its expecting a value.

Cheers

EDIT Going to post what my end templates looked like in case someone else needs assistance.

Template File

Variables

"variables": {
"empty": []
},

Resources

"connectionStrings": "[if(empty(parameters('connString')), variables('empty'), array(parameters('connString')))]",

Parameter File

If setting a connection string

 "connString": {
        "value": [{
            "name": "test",
            "connectionString": "ufgndjkngsdgjkn",
            "type": "Custom"
        }]
    },

If not wanting to set a connection string

"connString": {
    "value": [
    ]
},

Answer

4c74356b41 picture 4c74356b41 · Oct 19, 2018

you should just do this:

parameters:

"connString": {
    "value": {}
},

variables:

"empty": [],

template:

"connectionStrings": "[if(empty(parameters('connString')), variables('empty'), array(parameters('connString')))]"