How do we get a connection string for a cosmos database in in a module. #8951
-
I have a cosmos database I'm deploying through bicep, and a function app which will interact with the database. I want to deploy the database and configure the app with the Because I have a number of similar scenarios, I want to have a First I tried emitting it as an output: output accountEndpoint string = cosmosAccount.listConnectionStrings().connectionStrings[0].connectionString This fails because of this linter error (outputs shouldn't contain secrets). Fair enough, seems legit. I could override that but I want to do something that might compromise security. I've seen this discussion where the recommended fix is to use the module cosmacc 'modules/cosmacc.bicep' = {
// properties omitted for brevity
}
resource cosmacc_existing 'Microsoft.DocumentDB/databaseAccounts@2022-08-15' existing = {
name: cosmacc.outputs.accountName
scope: resourceGroup(resourceGroupName)
}
module functionapp 'modules/functionapp.bicep' = {
name: 'functionapp'
scope: resourceGroup(resourceGroupName)
params: {
appSettings: [
{
name: 'myConnectionString'
value: listConnectionStrings(cosmacc_existing.id, cosmacc_existing.apiVersion).connectionStrings[0].connectionString
}
]
}
} Running this through the PowerShell module, I get this: PS> new-AzSubscriptionDeployment -Name "deployment" -TemplateFile main.bicep -Location uksouth -Verbose
VERBOSE: Using Bicep v0.11.1
VERBOSE:
New-AzDeployment: 18:36:39 - Error: Code=InvalidTemplate; Message=Deployment template validation failed:
'The template resource 'functionapp' at line '539' and column '9' is not valid:
The template function 'reference' is not expected at this location. Please see
https://aka.ms/arm-template-expressions for usage details..
Please see https://aka.ms/arm-template-expressions for usage details.'.
New-AzDeployment: The deployment validation failed Is |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can call list connection strings against your existing cosmos DB reference;
I wouldn't recommend putting that directly in the function app setting though, and I imagine you'll get warnings about it (I hope). It would be better to put this value in a key vault secret and use a key vault secret reference (https://learn.microsoft.com/en-us/azure/app-service/app-service-key-vault-references?tabs=azure-cli) from your function app settings. |
Beta Was this translation helpful? Give feedback.
You can call list connection strings against your existing cosmos DB reference;
cosmacc_existing.listConnectionStrings().connectionStrings[0].connectionString
I wouldn't recommend putting that directly in the function app setting though, and I imagine you'll get warnings about it (I hope). It would be better to put this value in a key vault secret and use a key vault secret reference (https://learn.microsoft.com/en-us/azure/app-service/app-service-key-vault-references?tabs=azure-cli) from your function app settings.