3
votes

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?

2

2 Answers

3
votes

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://docs.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]"
      }
  }
}
-1
votes

As you already figured out, you can't use the reference function for resources you deployed in another template. See also: Reference valid uses

You can either write a single ARM template which deploys your Application Insight and your Azure Function App, or you split the deployments using a linked template: See also: Using linked and nested templates when deploying Azure resources

Another option would be to output the InstrumentationKey in your first deployment and store it somewhere where you can retrieve it in your Function App deployment (e. g. Azure KeyVault or Azure DevOps Variable)....