Sayantan Ghosh
Sayantan Ghosh

Reputation: 1056

How to get Connection string of a cosmos DB created for table API in ARM template

I am trying to create a cosmos db account for table APIs and want to upload the connection string to a KV using ARM template.

I expect the connection string in this format:

DefaultEndpointsProtocol=https;AccountName=<<AccountName>>;AccountKey=<<Key>>;TableEndpoint=https://<<AccountName>>.table.cosmos.azure.com:443/;

I am using this:

[listConnectionStrings(resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('globalCosmosDBName')), '2019-12-12').connectionStrings[0].connectionString]

But with the above I am getting a connections string like below:

AccountEndpoint=https://<<AccountName>>.documents.azure.com:443/;AccountKey==<<Key>>;

How can I get a connection string with the table endpoint?

Upvotes: 0

Views: 2798

Answers (2)

hotfix-houdini
hotfix-houdini

Reputation: 1

Same deal for Azure Bicep. Here's what works for me for constructing the Table connection string

@description('Resource group for the table account')
param table_account_resource_group string
@description('Cosmosdb Table account name')
param table_account_name string

resource cosmosdb_table_account 'Microsoft.DocumentDB/databaseAccounts@2023-04-15' existing = {
  name: table_account_name
  scope: az.resourceGroup(table_account_resource_group)
}

var accountKey = cosmosdb_table_account.listKeys().primaryMasterKey
var tableEndpoint = replace(cosmosdb_table_account.properties.documentEndpoint, 'documents', 'table.cosmos')
var formattedConnectionString = 'DefaultEndpointsProtocol=https;AccountName=${table_account_name};AccountKey=${accountKey};TableEndpoint=${tableEndpoint};'

Upvotes: 0

Mark Brown
Mark Brown

Reputation: 8763

Our RP does not return the legacy table connection string format. It only has the format you have below.

I think the only way to do this is to use concat to build the string and use a combination of the reference and listKeys arm functions below.

"[reference(resourceId('Microsoft.DocumentDb/databaseAccounts/', parameters('globalCosmosDBName'))).documentEndpoint]"

"[listKeys(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('globalCosmosDBName')), '2020-04-01').primaryMasterKey]"

Here is the complete concat function.

“[concat(‘DefaultEndpointsProtocol=https;AccountName=’, [reference(resourceId('Microsoft.DocumentDb/databaseAccounts/', parameters('globalCosmosDBName'))).documentEndpoint], ‘;AccountKey=’, [listKeys(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('globalCosmosDBName')), '2020-04-01').primaryMasterKey], ‘;TableEndpoint=https://’, [reference(resourceId('Microsoft.DocumentDb/databaseAccounts/', parameters('globalCosmosDBName'))).documentEndpoint], ‘.table.cosmos.azure.com:443/;’]”

Upvotes: 1

Related Questions