You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Azure Resource Manager (ARM) templates are infrastructure as code (JSON) in Azure.
You can define dependencies to define their deploy order.
{"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#","contentVersion": "",// (Required) Your own version to ensure right template is deployed"apiProfile": "",// API versions for resource types."parameters": {},// prompted when deployment is executed."variables": {},"functions": [],"resources": [],// (Required) Resource types that are deployed or updated"outputs": {}// Values that you want to return after deployment.}
Only parameter files are changed to include Azure Key Vault references.
Cons
Within the parameter file, Azure Key Vault resource ID must be hard-coded.
The hard-coded resource ID includes the subscription ID, which might be considered as a sensitive data.
With dynamic resource ID
Use this when you do not want to hardcode the Key Vault ID with e.g. subscription id.
Dynamically construction ID does not work in ARM template, neither in parameters file.
Nested templates are the key to using this dynamic id.
Using linked template
Notice templateLink
It links to another ARM file that will use secret value as string
Template must exist in the remote location
{"apiVersion": "2015-01-01","name": "nestedTemplate","type": "Microsoft.Resources/deployments","properties": {"mode": "Incremental","templateLink": {"uri": "[concat(parameters('templateBaseUri'), 'my-nested-template.json')]","contentVersion": "1.0.0.0"},"parameters": {"resourcegroup": {"value": "[parameters('resourcegroup')]"},"vaultName": {"value": "[parameters('vaultName')]"},"secretToPass": {// here vault ID & secret name is dynamically generated"reference": {"keyVault": {"id": "[resourceId(subscription().subscriptionId, parameters('resourcegroup'), 'Microsoft.KeyVault/vaults', parameters('vaultName'))]"},"secretName": "examplesecret"}}}}
Pros
There is no hard-coded value required.
Cons
Additional linked templates should be written increasing maintenance effort.
Using nested template
Another option is to have a nested template
{"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {"vaultName": {"type": "string","metadata": {"description": "The name of the keyvault that contains the secret."}},"secretName": {"type": "string","metadata": {"description": "The name of the secret."}},"vaultResourceGroupName": {"type": "string","metadata": {"description": "The name of the resource group that contains the keyvault."}},"vaultSubscription": {"type": "string","defaultValue": "[subscription().subscriptionId]","metadata": {"description": "The name of the subscription that contains the keyvault."}}},"resources": [{"type": "Microsoft.Resources/deployments","apiVersion": "2018-05-01","name": "dynamicSecret","properties": {"mode": "Incremental","expressionEvaluationOptions": {"scope": "inner"},"template": {// nested child template"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {"adminPassword": {// gets from the parent"type": "securestring"}},// ... stripped rest of the template},"parameters": {"adminPassword": {// here vault ID & secret name is dynamically generated"reference": {"keyVault": {"id": "[resourceId(parameters('vaultSubscription'), parameters('vaultResourceGroupName'), 'Microsoft.KeyVault/vaults', parameters('vaultName'))]"},"secretName": "[parameters('secretName')]"}}}}}],"outputs": {}}