27

Is there any way to retrieve the Instrumentation Key for an Application Insights instance in an Azure Resource Group template ?

I've tried the instructions here to retrieve the list of list* operations available on Azure resources, but Microsoft.Insights/components doesn't appear in the list anywhere. It's making me think that retrieving an Instrumentation Key in the template isn't currently possible

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Alex Marshall
  • 10,162
  • 15
  • 72
  • 117

4 Answers4

61

After some digging and experimenting, this is what I found works:

"outputs": {
    "MyAppInsightsInstrumentationKey": {
        "value": "[reference(resourceId('Microsoft.Insights/components', variables('myAppInsightsInstanceName')), '2014-04-01').InstrumentationKey]",
        "type": "string"
    }
}
Alex Marshall
  • 10,162
  • 15
  • 72
  • 117
  • 2
    You may also be able to use the `listKeys()` function pointed at the Insights instance name: https://azure.microsoft.com/en-us/documentation/articles/resource-group-template-functions/#listkeys I had a similar question for the Log Analytics/OMS/OpsInsight Workspace Primary & Secondary SharedKeys, and while the resourceProvider does not contain and list* operations as that blog indicates is required, it still was able to return back my keys. – JoeBrockhaus May 25 '16 at 20:20
  • @Alex-Marshall, this is helpful. Is there any chance you could address this question: http://stackoverflow.com/q/37570408/188474 – Brett Jun 01 '16 at 13:40
9

Try it (using azure cli)

az resource show -g $RESOURCE_GROUP -n $APP_INSIGHTS --resource-type "microsoft.insights/components" --query properties.InstrumentationKey
Felipe Augusto
  • 1,341
  • 1
  • 16
  • 18
5

Instrumentation Key belongs to resource, you can find it in Azure Resource manager template. If you want to find Instrumentation Key, you need to define ResourceType to Microsoft.Insights/components. Try the below code:

$resourcevalue=Get-AzureRmResource -ResourceGroupName Default-ApplicationInsights-*** -ResourceType Microsoft.Insights/components -ResourceName **hdinsights -ApiVersion 2015-05-01 $resourcevalue.Properties.InstrumentationKey

Lily_user4045
  • 795
  • 4
  • 11
  • 2
    Thanks, but I'm looking to retrieve the key in the template itself so that it can be automatically populated into application settings in the portal. – Alex Marshall Apr 19 '16 at 17:03
  • Your snippet returned a empty `Properties` property. For me the following worked: `( Get-AzApplicationInsights ).InstrumentationKey` – abbgrade Mar 10 '19 at 14:57
0

The answer from Alex Marshall will work fine, but I just wanted to contribute by saying you could do this with Bicep as well. The syntax would be as follows:

resource appInsights 'Microsoft.Insights/components@2020-02-02' existing = {
  name: 'appinsights-name'
  scope: resourceGroup('rg-appinsights-resides-in')
}


output appString string = appInsights.properties.ConnectionString
output appinsight string = appInsights.properties.InstrumentationKey
notStan
  • 266
  • 1
  • 9