1

Is there anyway to use the reference funtion in an ARM parameter file? I understand the following can be used to retrieve the intrumentation key of an app insights instance but this doesnt seem to work in a parameter file.

"[reference('microsoft.insights/components/web-app-name-01', '2015-05-01').InstrumentationKey]"

I currently set a long list of environment variables using an array from a parameter file and need to include the dynamic app insights instrumentation key to that list of variables.

Andrew Wiebe
  • 111
  • 13

3 Answers3

2

Unfortunately, no.

Reference function only works at runtime. It can't be used in the parameters or variables sections because both are resolved during the initial parsing phase of the template.

Here is an excerpt from the docs and also how to use reference correctly:

You can't use the reference function in the variables section of the template. The reference function derives its value from the resource's runtime state. However, variables are resolved during the initial parsing of the template. Construct values that need the reference function directly in the resources or outputs section of the template.

1

Not in a param file... it's possible to simulate what you want by nested a deployment if that's an option. So your param file can contain the resourceId of the insights resource and then a nested deployment can make the reference call - but TBH, probably easier to fetch the key as a pipeline step (or similar).

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "insightsResourceId": {
      "type": "string",
      "defaultValue": "'microsoft.insights/components/web-app-name-01'"
    }
  },
  "resources": [
    {
      "apiVersion": "2018-02-01",
      "type": "Microsoft.Resources/deployments",
      "name": "nestedDeployment",
      "properties": {
        "mode": "Incremental",
        "parameters": {
          "instrumentationKey": {
            "value": "[reference(parameters('insightsResourceId'), '2015-05-01').InstrumentationKey]"
          }
        },
        "template": {
            // original template goes here
        }
      }
    }
  ]
}

bmoore-msft
  • 8,376
  • 20
  • 22
0

Way 1

Use the reference function in your parameter file for resources that are already deployed in another template. For that you have to pass the ApiVersion parameter. Refer MsDoc. which follows:

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

You need to change the property that you are referencing from '.InstrumentationKey' to '.properties.InstrumentationKey'.

Refer to Kwill answer for more information.

Way 2

Get the Content of parameter file in PowerShell variable/Object using

$ParameterObject = Get-Content ./ParameterFileName.json
Update the Parameter file values using 
#Assign the parameter values using 
$ParameterObject.parameters.<KeyName>.value = "your dynamic value"

Pass $parameterObject to -TemplateParameterObject parameter

Refer here

Way 3

You have to Add/Modify the parameter file values using (PowerShell/ Dev lang (like Python, c#,...) ). After changing the parameter file try to deploy it.

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15