Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

referencing a KeyVault secret in an ARM template fails with 'The resource is not defined in the template'

I am trying to create a KeyVault reference in the AppConfig section of an Azure web app. The KeyVault reference references a secret which exists in a KeyVault which is part of a different resourcegroup and thus does not exist in the template.

according to the documentation of the reference() template function you should be able to reference a resource which is not part of the template as long as you provide the complete resourceId and the apiVersion.

But when I use that to reference the secret I keep getting a validation error which says:

Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The resource 'Microsoft.KeyVault/vaults//secrets/' is not defined in the template.

I followed this guide. for how to use KeyVault references in ARM templates.

Below code is a sample of a situation which does not work.

{
  "type": "Microsoft.Web/sites",
  "apiVersion": "2016-08-01",
  "name": "[variables('webAppName')]",
  "location": "[resourceGroup().location]",
  "properties": {
    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('serverFarmName'))]",
    "siteConfig": {
      "alwaysOn": true,
      "appSettings": [
        {
          "name": "<secretName>",
          "value": "[concat('@Microsoft.KeyVault(SecretUri=', reference(variables('secretResourceId')).secretUriWithVersion, ')')]"
        },
      ]
    }
  },
  "identity": {
    "type": "SystemAssigned"
  },
  "dependsOn": [
    "[resourceId('Microsoft.Web/serverfarms', variables('serverFarmName'))]"
  ]
}

The variable is defined like this:

"variables": {
  "secretResourceId": "[resourceId(subscription().subscriptionId, parameters('keyVaultResourceGroup'), 'Microsoft.KeyVault/vaults/secrets', parameters('keyVaultName'), 'secretName')]"
},

Is this something specific to references to KeyVault secrets?

As soon as I try the same but with a keyvault and secret inside the template it works perfectly fine.

like image 741
Tjeerd Avatar asked Jan 23 '20 09:01

Tjeerd


People also ask

How do you get the KeyVault secret in arms template?

Deploy key vaults and secrets To access a key vault during template deployment, set enabledForTemplateDeployment on the key vault to true . If you already have a key vault, make sure it allows template deployments. To create a new key vault and add a secret, use: Azure CLI.

How do I remove secret from KeyVault?

In order to permanently remove the secret, we must have additional 'Purge' permission. We can give them to ourselves through the portal. Once we have the appropriate permissions, we can remove the secret. Only a deleted secret can be 'permanently deleted', for which the 'az keyvault secret purge' command is used.


1 Answers

The documentation for the reference function mentions that the second parameter to the function, apiVersion is required when the referring resource isn't provisioned within the same template.

So, instead of

reference(variables('secretResourceId')).secretUriWithVersion

something like this should work

reference(variables('secretResourceId'), '2018-02-14').secretUriWithVersion
like image 94
PramodValavala-MSFT Avatar answered Oct 27 '22 12:10

PramodValavala-MSFT