Andrew Wiebe
Andrew Wiebe

Reputation: 221

Put Azure Key Vault value in parameter array

I am trying to deploy a App service webapp via ARM template and need to put a secret from a key vault into an app setting (env variable).

I have always simply used an array of values from a parameters file to populate these app settings, but now I am struggling to get a keyvault value into that array. Something like shown below in an ARM parameter file.

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "someStringParam": {
        "value": "stringLiteralValueHere"
    },
    "envVars": {
        "value": [
            {
                "name": "envVarKeyName",
                "value": "stringLiteralValueHere"
            },
            {
                "name": "KVsecret1",
                "value": ##KEY VAULT SECRET HERE##
            }
        ]
    }
}

}

I have tried using a reference to the keyvault for the value but that errors on deployment.

{
                "name": "KVsecret1",
                "reference": {
                    "keyVault": {
                        "id": "/subscriptions/<subscription_id>/resourceGroups/<resource_group>/providers/Microsoft.KeyVault/vaults/<vault_name>"
                    },
                    "secretName": "secret1"
                }
            }

I have also tried using a parameter inside of the parameter file, but that just used the literal string for the value.

"parameters": {
    "KVsecret1": {
        "reference": {
            "keyVault": {
                "id": "/subscriptions/<subscription_id>/resourceGroups/<resource_group>/providers/Microsoft.KeyVault/vaults/<vault_name>"
            },
            "secretName": "KVsecret1"
        }
    },
    "envVars": {
        "value": [
            {
                "name": "envVarKeyName",
                "value": "stringLiteralValueHere"
            },
            {
                "name": "KVsecret1",
                "value": "[parameters('KVsecret1')]"
            }
        ]
    }
}

Is this possible??

EDIT: Adding some detail here.

I am also trying to shoe horn a reference to another resource to get put the app insights instrumentation key into an app setting. Below is what I would like to do, but the copy function needs to use the name of the property and that is dynamic in this case as it changes with the each member of the array from the parameter file.

{
        "type": "Microsoft.Web/sites/config",
        "apiVersion": "2022-03-01",
        "name": "[concat(parameters('backEndwebAppName'),'/appsettings')]",
        "kind": "string",
        "properties": {
            "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(concat('microsoft.insights/components/',parameters('appInsightsName')),'2020-02-02').InstrumentationKey]",
            "secret1FromKeyvault": "[parameters('secret1FromKeyvault')]",
            "copy": [
                {
                    "name": "envVarsFromParams",
                    "count": "[length(parameters('backEndEnvVariables'))]",
                    "input": {
                        "name": "[parameters('backEndEnvVariables')[copyIndex('envVarsFromParams').name]]",
                        "value": "[parameters('backEndEnvVariables')[copyIndex('envVarsFromParams').value]]"
                    }
                }
            ]
        },
        "dependsOn": [
            "[resourceId('Microsoft.Web/sites', parameters('backEndwebAppName'))]"
        ]

    },

Upvotes: 0

Views: 1915

Answers (1)

bmoore-msft
bmoore-msft

Reputation: 8737

This isn't possible today within the param file, but in your scenario (if it's as simple as your OP example) you can just union the two in your template. So in your parameter file, you have 2 params kvSecret (the reference) and envVars (all your other env vars) and then in the template use:

"variables": {
  "keySecretObj": {
    "name": "kvSecret",
    "value": "[parameters('kvSecret')]"
  },
  "envVarsFinal": "[union(parameters(variables('kvSecretObj`), parameters(`envVars`))]"

That help?

Upvotes: 1

Related Questions