Closed yks0000 closed 2 years ago
As you've found, we have some limitations in the deployments engine, which makes using module outputs tricky.
The name
property for every resource has to be known at the start of the deployment - meaning it cannot be calculated based on an output from a module. This is because the full deployment graph must be calculable when the file is first deployed.
The best workaround I can suggest here is to re-establish the reference to the CDN resource using existing
, rather than using the module to output the ID. In your parent file, this would look something like like:
var cdnName = 'static'
module static_stg 'profile.bicep' = {
name: 'static.stg'
params: {
cdn_profile_name: cdnName
...
}
}
resource cdn 'Microsoft.Cdn/profiles@2021-06-01' existing = {
name: cdnName
}
resource afdEndpoint 'Microsoft.Cdn/profiles/afdendpoints@2021-06-01' existing = {
parent: cdn
name: // what goes here?
}
resource static_stg_aero_routes 'Microsoft.Cdn/profiles/afdendpoints/routes@2021-06-01' = {
parent: afdEndpoint
name: 'aero-assets-route'
properties: {
...
}
}
Again, you'll need to do something creative to workaround this limitation that for-loops must be evaluated at deploy-time:
var cdnName = 'static'
var customDomainConfig = [
{ hostname: 'static-afd-test.example.com' }
]
module static_stg 'profile.bicep' = {
name: 'static.stg'
params: {
cdn_profile_name: cdnName
customDomainConfig: customDomainConfig
...
}
}
resource cdn 'Microsoft.Cdn/profiles@2021-06-01' existing = {
name: cdnName
}
resource customDomains 'Microsoft.Cdn/profiles/customdomains@2021-06-01' existing = [for config in customDomainConfig: {
parent: cdn
name: replace(config.hostname, '.', '-')
}]
resource afdEndpoint 'Microsoft.Cdn/profiles/afdendpoints@2021-06-01' existing = {
parent: cdn
name: // what goes here?
}
resource static_stg_aero_routes 'Microsoft.Cdn/profiles/afdendpoints/routes@2021-06-01' = {
parent: afdEndpoint
name: 'aero-assets-route'
properties: {
...
customDomains: [for (config, index) in customDomainConfig: {
id: customDomains[index]
}]
...
}
}
Once #7083 has been merged, you should be able to do this with the map()
function instead which would not have this limitation.
Thank you @anthony-c-martin. Will watch for https://github.com/Azure/bicep/pull/7083
Any examples with the map function?
Bicep version Bicep CLI version 0.7.4 (5afc312467)
Describe the bug
Unable to use output of a module in a resource block declared in child bicep file. Getting error as below
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 static_stg which can be calculated at the start include "name".bicep(BCP178)
This expression is being used in an assignment to the "name" property of the "Microsoft.Cdn/profiles/afdEndpoints/routes" type, which requires a value that can be calculated at the start of the deployment. Properties of static_stg which can be calculated at the start include "name".bicep(BCP120)
To summarize, I am trying to create a CDN profile with some custom domains. Later using that module in child file (say, stage.bicep and prod.bicep) and adding routes, rules to it. While referring to output in name and within the property in child bicep, getting errors as above.
To Reproduce
Create a module with file profile.bicep
Try to use the Module in stage.bicep
To help find where exactly the error is, please check the snippet as below:
and