Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.23k stars 748 forks source link

Reference resource group that is created via a module #4992

Open romanovacca opened 2 years ago

romanovacca commented 2 years ago

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

alex-frankel commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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).

StijnDevogel commented 2 years ago

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

alex-frankel commented 2 years ago

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

jonathh21 commented 1 year ago

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 commented 8 months ago

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 commented 7 months ago

I had the same issue today