6

Is there any way to retrieve the Instrumentation Key for an Application Insights (which resides in another resource group) in an ARM template ?

I have already created an appInsights using ARM template using the below code,

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "AppInsightsName": { "type": "string" },
    "Location": { "type": "string", "defaultValue": "westeurope" }
  },
  "variables": {
    //"apiVersion": "2018-02-01-preview",
    "apiVersion": "2016-08-01",
    "location": "[parameters('Location')]",
    "ApplicationInsightsName": "[parameters('AppInsightsName')]"
  },
  "resources": [
    {
      "apiVersion": "2014-04-01",
      "type": "Microsoft.Insights/components",
      "name": "[variables('ApplicationInsightsName')]",
      "location": "[variables('location')]",
      "kind": "other",
      "properties": {
        "applicationId": "[variables('ApplicationInsightsName')]"
      }
    }
  ]
}

Now i am trying to link azure function app which runs in another resource group with this appInsights.

Below is the code i have tried,

{
  "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
  "value": "[reference(resourceId(variables('AppInsightsResourceGroup'),'Microsoft.Insights/components', variables('ApplicationInsightsName'))).InstrumentationKey]"
}

But i am getting the below error,

enter image description here

Can someone give some idea how to crack this?

PP006
  • 681
  • 7
  • 17

1 Answers1

8

You can use the reference function for resources that are already deployed from another template. You just need to pass in the apiVersion parameter as indicated in the docs at https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#reference. Note that you also need to change the property that you are referencing from '.InstrumentationKey' to '.properties.InstrumentationKey'.

"value": "[reference(resourceId(variables('AppInsightsResourceGroup'),'Microsoft.Insights/components', variables('ApplicationInsightsName')), '2015-05-01', 'Full').properties.InstrumentationKey]"

You can deploy the following template to validate (just replace the two variables with your values):

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {
      "AppInsightsResourceGroup": "myAIRG",
      "ApplicationInsightsName": "myAI"
  },
  "resources": [
  ],
  "outputs": {
      "appInsightsKey": {
          "type": "string",
          "value": "[reference(resourceId(variables('AppInsightsResourceGroup'),'Microsoft.Insights/components', variables('ApplicationInsightsName')), '2015-05-01', 'Full').properties.InstrumentationKey]"
      }
  }
}
kwill
  • 10,867
  • 1
  • 28
  • 26