azure-docs icon indicating copy to clipboard operation
azure-docs copied to clipboard

Bicep loops only work with values that can be determined at the start of deployment

Open thisjustin816 opened this issue 2 years ago • 4 comments

When trying to loop through some values of a created existing resource, I got the following error:

This expression is being used in the for-expression, which requires a value that can be calculated at the start of the deployment. Properties of [app service] which can be calculated at the start include "apiVersion", "id", "type". Bicep(BCP178)

This limitation is not listed in the documentation.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

thisjustin816 avatar Nov 03 '22 19:11 thisjustin816

@thisjustin816

Thanks for your feedback! We will investigate and update as appropriate.

Naveenommi-MSFT avatar Nov 04 '22 04:11 Naveenommi-MSFT

@mumian Can you please provide your inputs on this?

SwathiDhanwada-MSFT avatar Nov 07 '22 08:11 SwathiDhanwada-MSFT

@thisjustin816 - Hi Justin, can you share your code?

mumian avatar Nov 11 '22 18:11 mumian

@thisjustin816 - Hi Justin, can you share your code?

resource portalAppService 'Microsoft.Web/sites@2022-03-01' existing = {
  name: appService.name
}

resource apiService 'Microsoft.Web/sites@2022-03-01' existing = {
  name: 'api-${resourceNameSuffix}'
}

resource cors 'Microsoft.Web/sites/config@2022-03-01' = {
  name: 'web'
  parent: apiService
  properties: {
    cors: {
      allowedOrigins: [for hostname in portalAppService.properties.enabledHostNames: 'https://${hostname}']
      supportCredentials: true
    }
  }
}

The deployment is a vue app with an api backend. Both are deployed as app services (in separate deployments/pipelines) to the same resource group. I use a bicep registry module for the app service deployments. I also conditionally apply a custom domain based on environment so I recreate the app service resource after applying the domain so that all hostnames are available and since the app service is abstracted by the module. I'm using this to add CORS entries to the backend app service.

The workaround is to just wrap that resource in a module and pass the hostnames in as a parameter to make it a separate deployment.

thisjustin816 avatar Nov 11 '22 19:11 thisjustin816

@thisjustin816 - thank you . I have created a PR to update the limits - https://github.com/MicrosoftDocs/azure-docs-pr/pull/218298

#please-close

mumian avatar Nov 14 '22 20:11 mumian