Mark
Mark

Reputation: 5

Error when creating a New Resource Group in Bicep using a Module

I have a simple module to create a new Azure Resource Group:

The Module Code is:

targetScope='subscription'

param resourceGroupName string
param resourceGroupLocation string

resource newRG 'Microsoft.Resources/resourceGroups@2021-01-01' = {
  name: resourceGroupName
  location: resourceGroupLocation
}

I then call the module from my main:

param SubscriptionID string = ''
param resourceGroupName string = 'rg-09'
param resourceGroupLocation string = 'westus3'

// module deployed to subscription
module newRG 'modules/rg.bicep' = {
  name: 'newResourceGroup'
  scope: subscription(SubscriptionID)
  params: {
    resourceGroupName: resourceGroupName
    resourceGroupLocation: resourceGroupLocation
  }
}

To execute the code I use:

New-AzSubscriptionDeployment -TemplateFile .\main_rg.bicep -Location 'westus3'

The error I get is:

New-AzDeployment: 11:34:18 PM - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template resource 'newResourceGroup' at line '29' and column '9' is not valid: The template function 'RESOURCEGROUP' is not expected at this location. Please see https://aka.ms/arm-functions for usage details.. Please see https://aka.ms/arm-functions for usage details.'.

Can someone please tell me what I have done wrong

Upvotes: 0

Views: 1492

Answers (1)

Jahnavi
Jahnavi

Reputation: 8018

I also received the same error when I tried in my environment previously.

enter image description here

After a workaround on this, I modified your code as below and was able to deploy successfully with New-AzSubscriptionDeployment command.

Need to check:

  1. Once you added the target scope in main.bicep & module.bicep then no need of adding the scope field separately in the code.

  2. As @Thomas said, you need to add the targetScope='subscription' in each bicep file.

main.bicep:

targetScope='subscription'
param  resourceGroupName  string = 'rg-090'
param  resourceGroupLocation  string = 'eastus'
module  newRG  'module.bicep' = {
name: 'newResourceGroupcreate'
params: {
resourceGroupName: resourceGroupName
resourceGroupLocation: resourceGroupLocation
   }
}

module.bicep:

targetScope='subscription'
param  resourceGroupName  string
param  resourceGroupLocation  string
resource  newRG  'Microsoft.Resources/resourceGroups@2021-01-01' = {
name: resourceGroupName
location: resourceGroupLocation
}

main.parameters.json:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceGroupName": {
"value": "rg-090"
},
"resourceGroupLocation": {
"value": "eastus"
    }
  }
}

Deployed with New-AzSubscriptionDeployment:

New-AzSubscriptionDeployment `
 -Location "westus3" `
 -TemplateFile ".\main.bicep" `
 -TemplateParameterFile ".\main.parameters.json"

enter image description here

enter image description here

Refer bicep template in MSDoc.

Upvotes: 1

Related Questions