Search code examples
azure-rm-template

Parameter of Array type in ARM Template


I have an ARM Template with a parameter of array type as follows:

enter image description here

How do I set values in 2nd and 3rd position of parameter array? (for instance, "b", "c" in this example)


Solution

  • Method 1: Directly using the array index -

    You can use the values of Array parameter by using this expression:

    For 1st value: "[parameters('parameter1')[0]]"

    For 2nd value: "[parameters('parameter1')[1]]"

    For 3rd value: "[parameters('parameter1')[2]]"

    You can test this sample template for fetching the values from array and displaying in the output:

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "parameter1": {
                "defaultValue": [
                    "value1",
                    "value2",
                    "value3"
                ],
                "type": "Array"
            }
        },
        "resources": [],
        "outputs": {
            "firstValue": {
                "type": "String",
                "value": "[parameters('parameter1')[0]]"
            },
            "secondValue": {
                "type": "String",
                "value": "[parameters('parameter1')[1]]"
            },
            "thirdValue": {
                "type": "String",
                "value": "[parameters('parameter1')[2]]"
            }
        }
    }
    

    Update:

    Method 2: If you want to use copyIndex. Use the below expression -

    "[parameters('parameter1')[copyIndex()]]"
    

    Please check the below example:

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "parameter1": {
                "defaultValue": [
                    "strvalue1",
                    "strvalue2"
                ],
                "type": "Array"
            }
        },
        "resources": [
            {
                "type": "Microsoft.Storage/storageAccounts",
                "apiVersion": "2019-04-01",
                "name": "[parameters('parameter1')[copyIndex()]]",
                "location": "[resourceGroup().location]",
                "sku": {
                    "name": "Standard_LRS"
                },
                "kind": "Storage",
                "properties": {},
                "copy": {
                    "name": "storagecopy",
                    "count": "[length(parameters('parameter1'))]"
                }
            }
        ]
    }