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

Reference resource group that is created via a module #4992

Open
romanovacca opened this issue Oct 26, 2021 · 8 comments
Open

Reference resource group that is created via a module #4992

romanovacca opened this issue Oct 26, 2021 · 8 comments
Labels
enhancement New feature or request revisit

Comments

@romanovacca
Copy link

romanovacca commented Oct 26, 2021

Created this issue based on #1454.

What Im trying to do is within the same deployment, create a resource groupby using a module and then create other resources. When doing so, currently I cant reference the resourcegroup outputs in the scope, since this is disabled. My question is, how can it work in this setup?
I am specifically asking how it can work with a module resource group, since I know that creating the resource resourcegroup in the main template itself will work. For my current usecase, I want to use a precreated module of a resource group:

resourceGroup.bicep :

targetScope='subscription'

param resourceGroupName string
param location string 

var resourceGroupNameWithSuffix= 'rg-${resourceGroupName}'

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

output resourceGroupId string = rg.id
output resourceGroupName string = rg.name

main.bicep

targetScope='subscription'

param resourceGroupName string = 'bb-poc'

var location = deployment().location

module rg '../resources/resourceGroup.bicep' = {
  name: 'resourcegroupdeploy'
  params:{
    location: location
    resourceGroupName: resourceGroupName
  }
}

module appServicePlanDeployment '../resources/appServicePlan.bicep' = {
  name: 'appserviceplandeploy'
  scope: resourceGroup(rg.outputs.resourceGroupName)
  params:{
    appServicePlanName: 'testing'
  }
}

Current error on the line scope: resourceGroup(rg.outputs.resourceGroupName)
:
This expression is being used in an assignment to the "scope" property of the "module" type, which requires a value that can be calculated at the start of the deployment. Properties of rg which can be calculated at the start include "name".b

@ghost ghost added the Needs: Triage 🔍 label Oct 26, 2021
@alex-frankel
Copy link
Collaborator

alex-frankel commented Oct 26, 2021

The recommendation is to create the "raw" RG resource in main.bicep like so:

param resourceGroupName string = 'bb-poc'

var location = deployment().location
var resourceGroupNameWithSuffix= 'rg-${resourceGroupName}'

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

module appServicePlanDeployment '../resources/appServicePlan.bicep' = {
  name: 'appserviceplandeploy'
  scope: rg
  params:{
    appServicePlanName: 'testing'
  }
}

Is it a hard requirement to create the resource group in a module?

@romanovacca
Copy link
Author

Yeah that recommendation is what I mentioned that I know it works, but isn't what I want.
So the idea behind using a resourcegroup as a module is that that way we can enforce certain rules/policies such as, the suffix that should be in place. If a module can be used, then whenever you use that module, that resourcegroup will be created the same way with the appropriate settings, while if the resourcegroup has to be defined from scratch in the main file, that pre existing configuration is lost.

@alex-frankel
Copy link
Collaborator

Ah, sorry I missed that statement. Unfortunately, this is not possible today due to some runtime limitations. A module output is a runtime reference, but we need to be able to calculate special values like scope and name at compile-time.

In theory, we could do deeper constant folding (#444) to enable this for certain scenarios where no runtime properties are used (which this example would qualify as since no parameters are used).

@alex-frankel alex-frankel changed the title Reference resource group that is created in the same deployment Reference resource group that is created via a module Oct 26, 2021
@StijnDevogel
Copy link

@alex-frankel any chance you have an update on this on?

@alex-frankel
Copy link
Collaborator

No updates. I think we should revisit this after #2246 is implemented. I assume that functionality will be required to fix the properly.

@jonathh21
Copy link

I've come here.

Same problem. We create rsgs elsewhere.

Tried creating and asserting existing in the same module and it doesn't work.

Back to messy 'depends on' for me

@GingerSquirrel
Copy link

I have run into this issue today. I moved the resource group out of the module like suggested and then realised I couldn't also deploy a subscription in the same bicep file as this requires the scope to be the tenant which leads to this error on the resource group resource:

'A resource's scope must match the scope of the Bicep file for it to be deployable. You must use modules to deploy resources to a different scope.'

Obviously I can't put the resource group in a module because of the above issue. I am going to have to look into running separate scripts in a pipeline to get this working for me

@kmridha92
Copy link

I had the same issue today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request revisit
Projects
None yet
Development

No branches or pull requests

6 participants