Azure DevOps: Retrieve Application Insights Instrumentation Key
Recently I had to set up Azure Application Insight instances via a Azure DevOps CI/CD pipeline. I did this using ARM-Templates. It was straight forward except retrieving the newly created AppInsights Instrumentation-Key.
I needed the Instrumentation Key of the newly created AppInsights to store it in the applications Azure Key Vault from where the application gets its secrets.
To do this I added a ‘Azure Powershell’ step to my Azure DevOps pipeline to run a Powershell script with the right Azure Context (Subscription and Resource-Group) set.
Regarding Microsoft’s documentation there is a PowerShell function to do this Called
Get-AzureRmApplicationInsights. However this function did not return any value (and no error).
I have three pipeline variables:
rg_nameholds the Azure Resource-Group name
appinsights_namecontains the resource name of the AppInsights
appInsights_instrumentationkeyreceives the instrumentation key
The following little Azure Powershell script did the trick:
Write-Host "Resource Group Name: $(rg_name)" Write-Host "AppInsights Name: $(appinsights_name)" $instrumentationKey = (Get-AzApplicationInsights -ResourceGroupName "$(rg_name)" -Name "$(appinsights_name)").InstrumentationKey Write-Host "Instrumentation Key: $instrumentationKey" Write-Host "##vso[task.setvariable variable=appInsights_instrumentationkey;]$instrumentationKey"
It receives the resource group name and the AppInsights name via pipeline variable. The instrumentation key gets written to the third pipeline variable
As you see I have to use
Get-AzApplicationInsights and receive all props and then get the value of
.InstrumentationKey. This call worked for me while the
Get-AzureRmApplicationInsights did not.
A later step in my pipeline then writes
appInsights_instrumentationkey to a key-vault from where it gets picked up by the application.
Hope this helps!
Leave a Reply