Create resource groups by using Bicep
You can use Bicep to create a new resource group. This article shows you how to create resource groups when deploying to either the subscription or another resource group.
Define resource group
To create a resource group with Bicep, define a Microsoft.Resources/resourceGroups resource with a name and location for the resource group.
The following example shows a Bicep file that creates an empty resource group. Notice that its target scope is subscription
.
targetScope='subscription'
param resourceGroupName string
param resourceGroupLocation string
resource newRG 'Microsoft.Resources/resourceGroups@2024-03-01' = {
name: resourceGroupName
location: resourceGroupLocation
}
To deploy the Bicep file to a subscription, use the subscription-level deployment commands.
For Azure CLI, use az deployment sub create.
az deployment sub create \
--name demoSubDeployment \
--location centralus \
--template-file resourceGroup.bicep \
--parameters resourceGroupName=demoResourceGroup resourceGroupLocation=centralus
For the PowerShell deployment command, use New-AzDeployment or its alias New-AzSubscriptionDeployment
.
New-AzSubscriptionDeployment `
-Name demoSubDeployment `
-Location centralus `
-TemplateFile resourceGroup.bicep `
-resourceGroupName demoResourceGroup `
-resourceGroupLocation centralus
Create resource group and resources
To create the resource group and deploy resources to it, add a module that defines the resources to deploy to the resource group. Set the scope for the module to the symbolic name for the resource group you create. You can deploy to up to 800 resource groups.
The following example shows a Bicep file that creates a resource group, and deploys a storage account to the resource group. Notice that the scope
property for the module is set to newRG
, which is the symbolic name for the resource group that is being created.
targetScope='subscription'
param resourceGroupName string
param resourceGroupLocation string
param storageName string
param storageLocation string
resource newRG 'Microsoft.Resources/resourceGroups@2024-03-01' = {
name: resourceGroupName
location: resourceGroupLocation
}
module storageAcct 'storage.bicep' = {
name: 'storageModule'
scope: newRG
params: {
storageLocation: storageLocation
storageName: storageName
}
}
The module uses a Bicep file named storage.bicep with the following contents:
param storageLocation string
param storageName string
resource storageAcct 'Microsoft.Storage/storageAccounts@2023-04-01' = {
name: storageName
location: storageLocation
sku: {
name: 'Standard_LRS'
}
kind: 'Storage'
properties: {}
}
Create resource group during resource group deployment
You can also create a resource group during a resource group level deployment. For that scenario, you deploy to an existing resource group and switch to the level of a subscription to create a resource group. The following Bicep file creates a new resource group in the specified subscription. The module that creates the resource group is the same as the example that creates the resource group.
param secondResourceGroup string
param secondSubscriptionID string = ''
param secondLocation string
// module deployed at subscription level
module newRG 'resourceGroup.bicep' = {
name: 'newResourceGroup'
scope: subscription(secondSubscriptionID)
params: {
resourceGroupName: secondResourceGroup
resourceGroupLocation: secondLocation
}
}
To deploy to a resource group, use the resource group deployment commands.
For Azure CLI, use az deployment group create.
az deployment group create \
--name demoRGDeployment \
--resource-group ExampleGroup \
--template-file main.bicep \
--parameters secondResourceGroup=newRG secondSubscriptionID={sub-id} secondLocation=westus
For the PowerShell deployment command, use New-AzResourceGroupDeployment.
New-AzResourceGroupDeployment `
-Name demoRGDeployment `
-ResourceGroupName ExampleGroup `
-TemplateFile main.bicep `
-secondResourceGroup newRG `
-secondSubscriptionID {sub-id} `
-secondLocation westus
Next steps
To learn about other scopes, see: