Get CosmosDb Primary Connection String in ARM template
Asked Answered
L

4

15

I have an ARM template which sources the primaryMasterKey of a cosmosDb as follows:

{ "properties": { "enabled": true, "siteConfig": { "appSettings": [ { "name": "MongoDb:CnnDetails", "value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', variables('cosmosdb_full')), '2015-04-08').primaryMasterKey]" } }

How to I modify it to get the actual connection string instead?

I've tried couple of things:

  1. changed the word primaryMasterKey to primaryConnectionString. This gives an error saying:

'The language expression property 'primaryConnectionString' doesn't exist, available properties are 'primaryMasterKey, secondaryMasterKey, primaryReadonlyMasterKey, secondaryReadonlyMasterKey'

  1. changed the work listKeys to listConnectionStrings. This is red underlined in my visual studio, but seems to work when put through azure devops

'The language expression property 'primaryConnectionString' doesn't exist, available properties are 'connectionStrings'

  1. I went to https://learn.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/databaseaccounts/listconnectionstrings#code-try-0 to try it out. ListKeys returns a structure like this:

    { "primaryMasterKey": "[REDACTED]", "secondaryMasterKey": "[REDACTED]", "primaryReadonlyMasterKey": "[REDACTED]", "secondaryReadonlyMasterKey": "[REDACTED]" }

so I get why the .primaryMasterKey worked. But ListConnectionStrings returns:

{
  "connectionStrings": [
{
  "connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
  "description": "Primary MongoDB Connection String"
},
{
  "connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
  "description": "Secondary MongoDB Connection String"
},
{
  "connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
  "description": "Primary Read-Only MongoDB Connection String"
},
{
  "connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
  "description": "Secondary Read-Only MongoDB Connection String"
}
]
}

Not sure how to "index into it"?

Any clues gratefully received.

Longicorn answered 11/3, 2019 at 18:39 Comment(0)
M
6

like you normally would in almost any language:

ListConnectionStrings.connectionStrings[index].connectionString

index starts at 0.

you have a more "native" way of doing this:

first(ListConnectionStrings.connectionStrings).connectionString

but only available functions are first and last

Mitchmitchael answered 11/3, 2019 at 18:41 Comment(1)
Thanks I had tried [listConnectionStrings(resourceId('Microsoft.DocumentDB/databaseAccounts', variables('cosmosdb_full')), '2015-04-08')[1].connectionString] but I had left out the connectionStrings bit [and also was thinking one-based not zero based].Longicorn
S
26

For anyone else finding this question and wanting a fully complete ARM Template snippet, this is what I have used and is working:

"connectionStrings": [
 {
   "name": "CosmosConnection",
   "connectionString": "[listConnectionStrings(resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDbAccountName')), '2019-12-12').connectionStrings[0].connectionString]",
   "type": 3
 }
]
Stablish answered 17/2, 2020 at 11:16 Comment(0)
M
6

like you normally would in almost any language:

ListConnectionStrings.connectionStrings[index].connectionString

index starts at 0.

you have a more "native" way of doing this:

first(ListConnectionStrings.connectionStrings).connectionString

but only available functions are first and last

Mitchmitchael answered 11/3, 2019 at 18:41 Comment(1)
Thanks I had tried [listConnectionStrings(resourceId('Microsoft.DocumentDB/databaseAccounts', variables('cosmosdb_full')), '2015-04-08')[1].connectionString] but I had left out the connectionStrings bit [and also was thinking one-based not zero based].Longicorn
C
5

The answer here by oatsoda is correct but it will only work if you are within the same resource group as the Cosmos DB you are getting the connection string for. If you have the scenario where you Cosmos DB is in a different resource group to the resource you are generating an ARM template for the following snippet is what I have used to generate the connection string for an App Service and is working.

"Cosmos": {
   "value": "[listConnectionStrings(resourceId(parameters('cosmosResourceGroupName'),'Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDbName')), '2019-12-12').connectionStrings[0].connectionString]",
   "type": "Custom"
}
Cavitation answered 27/1, 2021 at 8:58 Comment(2)
Could you explain where exactly your JSON should be plugged in the ARM template?Coleorhiza
This would go into the properties of a connection strings section e.g. { "name": "connectionstrings", "type": "config", "apiVersion": "2018-11-01", "properties": { "Cosmos": { "value": "[listConnectionStrings(resourceId(parameters('cosmosResourceGroupName'),'Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDbName')), '2019-12-12').connectionStrings[0].connectionString]", "type": "Custom" } } }`Cavitation
U
0

In the Cosmos linked ARM template named linkedTemplate_cosmos_db-gdp-event-ammi-dev-ne-001 I used the following code.

"outputs": {
"ConnectionString": {
  "value": "[listConnectionStrings(resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('accountName')), '2019-12-12').connectionStrings[0].connectionString]",
  "name": "CosmosConnection",
  "type": "string"
}
},

and then in the ARM template (linkedTemplate_Main) that uses the output parameter, the following, e.g a function app configuration setting

 "COSMOS_CONNECTION_STRING": {
        "value": "[reference('linkedTemplate_cosmos_db-gdp-event-ammi-dev-ne-001').outputs.ConnectionString.value]"
Ury answered 25/5, 2022 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.