When it comes to deploying my infrastructure to Azure I prefer an imperative approach over a declarative approach so I starting to work a lot with Azure CLI and Azure PowerShell.
That being said, I am working on an Web App which will be deployed to an App Service in Azure.
When I deploy the App Service I would like to add an App Setting COSMOSDB_CONNECTIONSTRING
with the value of the Cosmos DB Primary SQL Connection String
.
I thought this would be pretty straight-forward BUT my lack of familiarity with PowerShell made what ended up a simple task a bit more challenging.
So sharing my learning experience with other developers and more likely my future self with a faulty memory.
Open Visual Studio Code, or whatever tool you use to work with PowerShell files.
Create a new PowerShell file.
At the top of your file Add the following code:
#Requires -Module Az.CosmosDB
This will install the Az.CosmosDB module if it is not currently installed.
Then add the call to get the connections strings from CosmosDB.
$cosmosDBConnectionStrings = Get-AzCosmosDBAccountKey -ResourceGroupName YOUR_RESOURCE_GROUP_NAME -Name YOUR_COSMOS_ACCOUNT_NAME -Type "ConnectionStrings"
If you were to run the Get-AzCosmosDBAccountKey
from the command line, the output would look like:
The output object is a Hashtable.
This is where things started to derail for me, if I would have treated the Hashtable like I would in C#, I would have been done in a matter of minutes, but I didn’t.
Anyway, the code to get the Primary SQL Connection String
is as follows:
$cosmosDBPrimarySQLConnectionString = $cosmosDBConnectionStrings["Primary SQL Connection String"]
That’s it!
Now I could assign my App Setting with this value.
Was just that simple.
Discover more from Matt Ruma
Subscribe to get the latest posts sent to your email.