Using Empty/Optional Parameters in Azure ARM Template

14,673

you should just do this:

parameters:

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

variables:

"empty": [],

template:

"connectionStrings": "[if(empty(parameters('connString')), variables('empty'), array(parameters('connString')))]"
Share:
14,673
Clackers
Author by

Clackers

Updated on June 11, 2022

Comments

  • Clackers
    Clackers almost 2 years

    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": [
        ]
    },
    
  • Clackers
    Clackers over 5 years
    Perfect! Thanks a lot for this, worked perfectly. Note that I needed to remove the connectionString array in the template file and just put in your suggested line of code. I also needed to modify the parameter file so the connection string was expecting an Array instead of an Object. Thanks Again
  • Dirk Slabbert
    Dirk Slabbert about 5 years
    after more testing: also found some scenarios, where the template does not accept null as and array resource value. replacing json('null') with json('[]') above, seems to work in more cases.