1
votes

I am trying to get the connection string for a cosmos db. In powershell when I do

Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" -ResourceGroupName $ResourceGroupName -Name $dbName | fl

I get a result showing the keys.

D:\> Invoke-AzureRmResourceAction -Action listKeys `
>>                                -ResourceType "Microsoft.DocumentDb/databaseAccounts" `
>>                                -ApiVersion "2015-04-08" `
>>                                -ResourceGroupName $ResourceGroupName -Name $dbName | fl

Confirm
Are you sure you want to invoke the 'listKeys' action on the following resource:
/subscriptions/(snip)/resourceGroups/example/providers/Microsoft.DocumentDb/databaseAccounts/myExampleDb
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

primaryMasterKey           : (snip)
secondaryMasterKey         : (snip)
primaryReadonlyMasterKey   : (snip)
secondaryReadonlyMasterKey : (snip)



D:\>

However if I try to list the connection strings, like the example in the documentation shows, I get no results

D:\> Invoke-AzureRmResourceAction -Action listConnectionStrings `
>>                                -ResourceType "Microsoft.DocumentDb/databaseAccounts" `
>>                                -ApiVersion "2015-04-08" `
>>                                -ResourceGroupName $ResourceGroupName -Name $dbName | fl

Confirm
Are you sure you want to invoke the 'listConnectionStrings' action on the following resource:
/subscriptions/(snip)/resourceGroups/example/providers/Microsoft.DocumentDb/databaseAccounts/myExampleDb
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

D:\>

With a resource manager template, if I deploy the following resource manager template

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
      "dbName": { 
          "type": "string"
      }
  },
  "resources": [
  ],
  "outputs": {
      "listConnectionStrings": {
          "type": "object",
          "value": "[listConnectionStrings(resourceid('Microsoft.DocumentDB/databaseAccounts', parameters('dbName')), '2016-03-19')]"
      },
      "listkeys": {
          "type": "object",
          "value": "[listKeys(resourceid('Microsoft.DocumentDB/databaseAccounts', parameters('dbName')), '2016-03-19')]"
      }
    }
}

I get back from the outputs

{
    "listConnectionStrings":  {
        },
    "listkeys":  {
        "primaryMasterKey": "(snip)",
        "secondaryMasterKey": "(snip)",
        "primaryReadonlyMasterKey": "(snip)",
        "secondaryReadonlyMasterKey": "(snip)"
    }
}

What am I doing wrong that prevents the connection strings from being shown?

2
Do you have a MongoDB account? It looks like Azure's responses are contextual to the account invoking the query in regard to Cosmos, so if the account didn't need the connection string I don't think it'll give you one.TheMadTechnician

2 Answers

3
votes

Azure Cosmo DB is a multimodel database service. The connection string is only available for the database account using the MongoDB API.

List Connection Strings

For MongoDB accounts, the connection string to connect your MongoDB app to the database account can be retrieved using the following command.

Below is the output of List Connection Strings action for CosmoDB using the MongoDB database account. enter image description here

0
votes

I also had a similar problem and I tried various ways but this worked for me.

(Get-AzCosmosDBAccountKey -ResourceGroupName "RgName" -Name "accountName" -Type "Keys")."PrimaryMasterKey"

If you would like to get the connection string you could also use the below command

(Get-AzCosmosDBAccountKey -ResourceGroupName "RgName" -Name "accountName" -Type "ConnectionStrings")."Primary SQL Connection String"