Cant create a KeyVault and use its getSecret method in the same template
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Bicep version**
run `bicep --version` via the Bicep CLI, `az bicep version` via the AZ CLI or via VS code by navigating to the extensions tab and searching for Bicep
**Describe the bug**
- On [issue 9175](https://github.com/Azure/bicep/issues/9175) of this repo, @alex-frankel mentions that you can (indeed) use an existing keyVault with the `existing` keyword to retreive a secret and pass it onto a module.
- It is also possible to deploy a keyVault without using it, and later add the resources that require the secret from the keyVault
- **The problem is when you want to create a keyVault and use it on the same deployment**
**To Reproduce**
The `main.bicep` file looks like this:
```bicep
//1. Generate a secret (used initially)
@secure()
param initialSecret string = newGuid()
param tenantId string = subscription().tenantId
param location string = resourceGroup().location
//2. Key Vault
resource KeyVault 'Microsoft.KeyVault/vaults@2019-09-01' = {
name: 'example'
location: location
properties: {
enabledForDeployment: true
enabledForTemplateDeployment: true
enabledForDiskEncryption: true
tenantId: tenantId
sku: {
name: 'standard'
family: 'A'
}
}
}
resource secret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
name: 'secret'
parent: KeyVault
properties: {
contentType: 'text/plain'
attributes: {
enabled: true
}
value: initialSecret
}
}
//3. USE THE SECRET
module example 'example.bicep' = {
name: 'example'
params: {
secret: KeyVault.getSecret('secret')
}
}
```
The dummy module saved in `example.bicep` looks like this:
```bicep
@secure()
param secret string
```
Execute using CLI
```bash
az deployment group create -g xxxxxxxxxx -f ./main.bicep
```
Result is:
```json
{"code": "KeyVaultParameterReferenceNotFound", "message": "The specified KeyVault '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.KeyVault/vaults/example' could not be found. Please see https://aka.ms/arm-keyvault for usage details."}
```
**Additional context**
The obvious workarounds are either:
- Comment out the module that uses the `getSecret`, deploy, then uncomment and re-deploy
- Separate the keyVault and secrets, deploy separately. Then in the main.bicep file import the keyVault using the `existing` keyword
The problem of the workarounds are:
- Two step deployment and dependency managed OUTSIDE the code. This is confusing and annoying.
- Deletion of the keyVault when using `--mode Complete`. I.e. you ideally want to manage all creations and deletions in one place
Contributor guide
Research direction
Start by reproducing the deployment with main.bicep, example.bicep, and the shown az deployment group create command. Compare the KeyVault.getSecret reference with the Key Vault and secret resources created in the same template; done means a single deployment can create them and pass the secret to the module without the KeyVaultParameterReferenceNotFound error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure
- Domain
- cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100