Skip to content

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.

Problem

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).

Solution

I have three pipeline variables:

  • rg_name holds the Azure Resource-Group name
  • appinsights_name contains the resource name of the AppInsights
  • appInsights_instrumentationkey receives 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 appInsights_instrumentationkey.

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: