Mike26
Mike26

Reputation: 119

How to set types of parameters in Azure ARM templates when using KeyVault references?

I have an issue with parameters types when I reference them as KeyVault secrets. I need to include Int, Bool or Object types not just a String.

Here is my example, main file:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "userLogin": {
            "type": "string"
        },
        "userPassword": {
            "type": "securestring"
        },
        "numberOfCores": {
            "type": "int",
            "defaultValue": 2
        },
        "subnetsConfig": {
            "type": "object"
        },
...
}

and parameters file:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "userLogin": {
            "reference": {
                "keyVault": {
                    "id": "MY KV RESOURCE ID"
                },
                "secretName": "MY SECRET NAME1"
            }
        },
        "userPassword": {
            "reference": {
                "keyVault": {
                    "id": "MY KV RESOURCE ID"
                },
                "secretName": "MY SECRET NAME2"
            }
        },
        "numberOfCores": {
            "reference": {
                "keyVault": {
                    "id": "MY KV RESOURCE ID"
                },
                "secretName": "MY SECRET NAME3"
            }
        },
        "subnetsConfig": {
            "reference": {
                "keyVault": {
                    "id": "MY KV RESOURCE ID"
                },
                "secretName": "MY SECRET NAME4"
            }
        },
...
}

I got that error:

Code=InvalidTemplate; Message=Deployment template validation failed: 'Template parameter JToken type is not valid. Expected 'Integer'. Actual 'String'

I cannot find a way how to make it work. Is that supported in that case?

Upvotes: 1

Views: 906

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40869

In terms of int you can convert it

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stringToConvert": {
      "type": "string",
      "defaultValue": "4"
    }
  },
  "resources": [
  ],
  "outputs": {
    "intResult": {
      "type": "int",
      "value": "[int(parameters('stringToConvert'))]"
    }
  }
}

and the same with bool

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "trueString": {
      "type": "bool",
      "value": "[bool('true')]",
    },
    "falseString": {
      "type": "bool",
      "value": "[bool('false')]"
    },
    "trueInt": {
      "type": "bool",
      "value": "[bool(1)]"
    },
    "falseInt": {
      "type": "bool",
      "value": "[bool(0)]"
    }
  }
}

and you can also create an object

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
  ],
  "outputs": {
    "newObject": {
      "type": "object",
      "value": "[createObject('intProp', 1, 'stringProp', 'abc', 'boolProp', true(), 'arrayProp', createArray('a', 'b', 'c'), 'objectProp', createObject('key1', 'value1'))]"
    }
  }
}

Upvotes: 1

Related Questions