Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.25k stars 754 forks source link

Referencing a property for an existing resource named by module generates invalid ARM #8773

Closed alexangas closed 2 years ago

alexangas commented 2 years ago

Bicep version v0.11.1

Describe the bug I am using a module to name resources consistently. In one of the templates that use this module, properties for those resources are used as parameter values to nested modules. This works without issue for new resources.

When referencing an existing module and attempting to perform the same operation, the Bicep builds but ARM deployment fails. The error is:

Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template resource 'application-slot' at line '1466' and column '9' is not valid: The template function 'reference' is not expected at this location. Please see https://aka.ms/arm-template-expressions for usage details.

To Reproduce Below is a snippet of the template that reproduces the issue.

module resourceNames '../modules/sitecore/resource-names.bicep' = {
  name: 'resourceNames-slot'
}

resource applicationInsights 'Microsoft.Insights/components@2020-02-02' existing = {
  name: resourceNames.outputs.applicationInsights
}

var applicationInsightsInstrumentationKey = applicationInsights.properties.InstrumentationKey

module application 'nested/application-slot.bicep' = {
  name: 'application-slot'
  params: {
    slotName: slotName
    applicationInsightsInstrumentationKey: applicationInsightsInstrumentationKey
  }
}

The ARM parameter generated is:

"applicationInsightsInstrumentationKey": {
  "value": "[reference(resourceId('Microsoft.Insights/components', reference(resourceId('Microsoft.Resources/deployments', 'resourceNames-slot')).outputs.applicationInsights.value), '2020-02-02').InstrumentationKey]"
},

Additional context The workaround is to supply the names for existing resources as parameters. However this is not desirable as this template is the top level, so the resource-names.bicep module cannot be used to do this.

Expected behaviour is that this functionality either works, or that the bicep build blocks template generation with a helpful error.

alexangas commented 2 years ago

Also, this may be related to #8144 but I lost my way in that issue so possibly not.

alex-frankel commented 2 years ago

I do think it is a duplicate of #8144. Closing for now.