bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Unclear where a resource is referencing itself

Open pamelafox opened this issue 11 months ago • 8 comments

Bicep version

It's whatever version is used by azd, presumably fairly recent.

Describe the bug

I keep getting the error that a template resource is referencing itself. I've been trying for an hour to delete changes to see what it means, but I am very flummoxed. Is there any way to get a better hint as to where the reference is?

This is for an azd provision using a number of Bicep files, but I have only added one file and changed a few files, as you can see in this diff between my branch and a working branch: https://github.com/jamesc/chatgpt-quickstart/compare/auth-enabled...pamelafox:chatgpt-quickstart:bicep?expand=1

Full error

ERROR: deployment failed: failing invoking action 'provision', error deploying infrastructure: starting deployment to subscription: PUT https://management.azure.com/subscriptions/b24b5122-ed51-497e-94f7-406bf93e7bfc/providers/Microsoft.Resources/deployments/authbicep-1710806274
--------------------------------------------------------------------------------
RESPONSE 400: 400 Bad Request
ERROR CODE: InvalidTemplate
--------------------------------------------------------------------------------
{
  "error": {
    "code": "InvalidTemplate",
    "message": "Deployment template validation failed: 'The template resource 'Microsoft.Resources/resourceGroups/authbicep-rg' cannot reference itself. Please see https://aka.ms/arm-function-reference for usage details.'.",
    "additionalInfo": [
      {
        "type": "TemplateViolation",
        "info": {
          "lineNumber": 0,
          "linePosition": 0,
          "path": ""
        }
      }
    ]
  }
}
--------------------------------------------------------------------------------

  To Reproduce

You can check out that repo at my branch, run azd env set USE_AUTHENTICATION true and then azd up.

pamelafox avatar Mar 19 '24 00:03 pamelafox

Can you see what happens when removing the graph resources? I am just wondering if something weird is happening with that new provider in particular.

Also, can you provide a repro without the azd dependency? Otherwise, we'd ask you to file the issue there.

alex-frankel avatar Mar 19 '24 14:03 alex-frankel

I still get the error when I remove the Bicep that references the graph provider. However, the error goes away if I remove bicepconfig.json, the file that contains:

{
    "experimentalFeaturesEnabled": {
        "extensibility": true,
        "microsoftGraphPreview": true
    }
}

So something about that configuration is causing it to find a self-reference error in the standard non-graph Bicep files (which isn't there otherwise).

I'll try to replicate with az deployment next.

pamelafox avatar Mar 20 '24 18:03 pamelafox

Update: I get the error still if I remove "microsoftGraphPreview" from that config (and leave extensbility: true, but I don't get the error if I set extensibility to false.

pamelafox avatar Mar 20 '24 18:03 pamelafox

Replicated with az deployment:

?127chatgpt-quickstart % az deployment sub create --location eastus --template-file infra/main.bicep --parameters name=authbicep location=eastus2
A new Bicep release is available: v0.26.54. Upgrade now by running "az bicep upgrade". /Users/pamelafox/chatgpt-quickstart/infra/core/ai/cognitiveservices.bicep(43,21) : Warning outputs-should-not-contain-secrets: Outputs should not contain secrets. Found possible secret: function 'listKeys' [https://aka.ms/bicep/linter/outputs-should-not-contain-secrets] /Users/pamelafox/chatgpt-quickstart/infra/aca.bicep(15,7) : Warning no-unused-params: Parameter "useAuthentication" is declared but never used. [https://aka.ms/bicep/linter/no-unused-params] /Users/pamelafox/chatgpt-quickstart/infra/aca.bicep(16,7) : Warning no-unused-params: Parameter "clientId" is declared but never used. [https://aka.ms/bicep/linter/no-unused-params] /Users/pamelafox/chatgpt-quickstart/infra/aca.bicep(18,7) : Warning no-unused-params: Parameter "clientCertificateThumbprint" is declared but never used. [https://aka.ms/bicep/linter/no-unused-params] /Users/pamelafox/chatgpt-quickstart/infra/aca.bicep(24,5) : Warning no-unused-vars: Variable "openIdIssuer" is declared but never used. [https://aka.ms/bicep/linter/no-unused-vars] WARNING: The following experimental Bicep features have been enabled: Extensibility. Experimental features should be enabled for testing purposes only, as there are no guarantees about the quality or stability of these features. Do not enable these settings for any production usage, or your production environment may be subject to breaking.

{"code": "InvalidTemplate", "message": "Deployment template validation failed: 'The template resource 'Microsoft.Resources/resourceGroups/authbicep-rg' cannot reference itself. Please see https://aka.ms/arm-function-reference for usage details.'.", "additionalInfo": [{"type": "TemplateViolation", "info": {"lineNumber": 0, "linePosition": 0, "path": ""}}]} ?1chatgpt-quickstart %

pamelafox avatar Mar 20 '24 18:03 pamelafox

(Also replicated after upgrading az bicep)

pamelafox avatar Mar 20 '24 18:03 pamelafox

Thanks for reporting! Here's a minimal repro:

  • bicepconfig.json:
    {
      "experimentalFeaturesEnabled": {
        "symbolicNames": true
      }
    }
    
  • main.json:
    targetScope = 'subscription'
    
    param rgName string
    
    resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
      name: rgName
    #disable-next-line no-loc-expr-outside-params
      location: deployment().location
    }
    
    resource resourceGroup2 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if(false) {
      name: resourceGroup.name
    }
    

anthony-c-martin avatar Mar 20 '24 23:03 anthony-c-martin

I believe this is related to #12204, and I feel like this is something worth us prioritizing because of how unintelligible the error message is.

The workaround to your main.bicep would be to change:

resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: '${name}-rg'
  location: location
  tags: tags
}

resource openAiResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if (!empty(openAiResourceGroupName)) {
  name: !empty(openAiResourceGroupName) ? openAiResourceGroupName : resourceGroup.name
}

To:

var rgName = '${name}-rg'

resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: rgName
  location: location
  tags: tags
}

resource openAiResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if (!empty(openAiResourceGroupName)) {
  name: !empty(openAiResourceGroupName) ? openAiResourceGroupName : rgName
}

anthony-c-martin avatar Mar 20 '24 23:03 anthony-c-martin

Thank you! Trying now. Is that a change we should make to all our azd templates? I believe a number of them use the former pattern. Or will that not ever become an issue in non-experimental Bicep?

pamelafox avatar Mar 21 '24 07:03 pamelafox