Jevgenij Nekrasov
Jevgenij Nekrasov

Reputation: 2760

ARM template optional parameters

I have pretty simple ARM template json file with the parameter:

"StorageName": {
  "type": "string",
  "defaultValue": ""
},

and the resource:

{
      "name": "[parameters('StorageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "location": "[resourceGroup().location]",
      "apiVersion": "2018-02-01",
      "condition": "[greater(length(parameters('StorageName')), 0)]",
      "sku": {
        "name": "[parameters('StorageType')]"
      },
      "dependsOn": [],
      "tags": {
        "displayName": "..storage"
      },
      "properties": {
        "accountType": "[parameters('StorageType')]",
        "supportsHttpsTrafficOnly": true,
        "encryption": {
          "services": {
            "blob": {
              "enabled": true
            },
            "file": {
              "enabled": true
            }
          },
          "keySource": "Microsoft.Storage"
        }
      },
      "kind": "[parameters('StorageKind')]"
    }

The StorageName has default empty string value, which is valid value based on microsoft documentation - The default value can be an empty string.

I use condition feature to create storage if only the name provided

"condition": "[greater(length(parameters('StorageName')), 0)]",

But still I received an error in vso console, when I run this ARM template:

2018-08-13T08:46:56.1816398Z The detected encoding for file 'D:\a\r1\a\...\drop\azuredeploy.json' is 'utf-8'
2018-08-13T08:46:56.1949411Z The detected encoding for file 'D:\a\r1\a\...\drop\azuredeploy.parameters.staging.json' is 'utf-8'
2018-08-13T08:46:56.1950518Z Starting Deployment.
2018-08-13T08:46:57.1434733Z There were errors in your deployment. Error code: InvalidTemplate.
2018-08-13T08:46:57.1435535Z ##[error]Deployment template validation failed: 'The template resource '' at line '1' and column '1358' is not valid. The name property cannot be null or empty. Please see https://aka.ms/arm-template/#resources for usage details.'.
2018-08-13T08:46:57.1436356Z ##[error]Task failed while creating or updating the template deployment

Any suggestions how to make parameter optional?

Upvotes: 2

Views: 9318

Answers (1)

4c74356b41
4c74356b41

Reputation: 72151

you cannot use an empty string as a name for a resource. so logical solution, change it from empty string to 'false' (or any other value) and do something like:

"[not(equals(parameters('name'), 'false'))] # << use the same value here

Alternatively you can do something like this:

"[not(empty(parameters('name')))]"

and use this condition to deploy or not deploy nested deployment, that will deploy storage account. this will work because you can have another name for nested deployment, but it wont start since the condition evaluates to false.

this is the general approach to do something if\when parameter\object is not empty.

Upvotes: 7

Related Questions