Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using module outputs in Scope #14381

Open
JPullinger opened this issue Jun 21, 2024 · 1 comment
Open

Using module outputs in Scope #14381

JPullinger opened this issue Jun 21, 2024 · 1 comment
Labels
enhancement New feature or request Needs: Triage 🔍

Comments

@JPullinger
Copy link

Recently using a Naming module to apply standard naming convention's across a project, I ran into the issue that you can't use a output from the Naming module (RG Name) in the Scope. VSCode obviously tell's me the code is broken.

Example:
scope: az.resourceGroup(deploymentsubscription, naming_module_001.outputs.resourceGroupName)

image

Although you can just code/hardcode RG names is there any potential in the future that a naming module output could be used in the Scope?

@jeskew
Copy link
Contributor

jeskew commented Jun 26, 2024

ARM is unlikely to remove this restriction in the future, but there are a couple of alternative approaches you could try.

Assuming the "naming module" just does some calculations on parameters and returns the results as outputs without deploying any resources, you could try using user-defined functions instead. These can be reused across templates with import statements.

If the naming module needs to remain a module (i.e., if it deploys resources) and the naming logic can't be factored out, you can use an output of one module as the scope of another by introducing an extra layer of indirection:

main.bicep

module namingModule 'namingModule.bicep' = {
  ...
}

module otherModule 'indirection.bicep' = {
  name: 'other'
  parameters: {
    targetResourceGroupName: namingModule.outputs.resourceGroupName
  }
}

indirection.bicep

param targetResourceGroupName string

resource targetRg 'Microsoft.Resources/resourceGroups@2021-04-01' existing = {
  name: targetResourceGroupName
}

module mod 'mod.bicep' = {
  name: 'mod'
  scope: targetRg
  ...
}

I wouldn't recommend this approach if you can avoid it because it is verbose and thwarts many kinds of static analysis (e.g., you cannot use What-If if you follow this pattern).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Needs: Triage 🔍
Projects
Status: Todo
Development

No branches or pull requests

2 participants