|
| 1 | +--- |
| 2 | +title: Use Bicep to create a new resource group |
| 3 | +description: Describes how to use Bicep to create a new resource group in your Azure subscription. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 09/26/2023 |
| 6 | +--- |
| 7 | + |
| 8 | +# Create resource groups by using Bicep |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +## Define resource group |
| 13 | + |
| 14 | +To create a resource group with Bicep, define a [Microsoft.Resources/resourceGroups](/azure/templates/microsoft.resources/allversions) resource with a name and location for the resource group. |
| 15 | + |
| 16 | +The following example shows a Bicep file that creates an empty resource group. Notice that its target scope is `subscription`. |
| 17 | + |
| 18 | +```bicep |
| 19 | +targetScope='subscription' |
| 20 | +
|
| 21 | +param resourceGroupName string |
| 22 | +param resourceGroupLocation string |
| 23 | +
|
| 24 | +resource newRG 'Microsoft.Resources/resourceGroups@2022-09-01' = { |
| 25 | + name: resourceGroupName |
| 26 | + location: resourceGroupLocation |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +To deploy the Bicep file to a subscription, use the subscription-level deployment commands. |
| 31 | + |
| 32 | +For Azure CLI, use [az deployment sub create](/cli/azure/deployment/sub#az-deployment-sub-create). |
| 33 | + |
| 34 | +```azurecli-interactive |
| 35 | +az deployment sub create \ |
| 36 | + --name demoSubDeployment \ |
| 37 | + --location centralus \ |
| 38 | + --template-file resourceGroup.bicep \ |
| 39 | + --parameters resourceGroupName=demoResourceGroup resourceGroupLocation=centralus |
| 40 | +``` |
| 41 | + |
| 42 | +For the PowerShell deployment command, use [New-AzDeployment](/powershell/module/az.resources/new-azdeployment) or its alias `New-AzSubscriptionDeployment`. |
| 43 | + |
| 44 | +```azurepowershell-interactive |
| 45 | +New-AzSubscriptionDeployment ` |
| 46 | + -Name demoSubDeployment ` |
| 47 | + -Location centralus ` |
| 48 | + -TemplateFile resourceGroup.bicep ` |
| 49 | + -resourceGroupName demoResourceGroup ` |
| 50 | + -resourceGroupLocation centralus |
| 51 | +``` |
| 52 | + |
| 53 | +## Create resource group and resources |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +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. |
| 58 | + |
| 59 | +```bicep |
| 60 | +targetScope='subscription' |
| 61 | +
|
| 62 | +param resourceGroupName string |
| 63 | +param resourceGroupLocation string |
| 64 | +param storageName string |
| 65 | +param storageLocation string |
| 66 | +
|
| 67 | +resource newRG 'Microsoft.Resources/resourceGroups@2022-09-01' = { |
| 68 | + name: resourceGroupName |
| 69 | + location: resourceGroupLocation |
| 70 | +} |
| 71 | +
|
| 72 | +module storageAcct 'storage.bicep' = { |
| 73 | + name: 'storageModule' |
| 74 | + scope: newRG |
| 75 | + params: { |
| 76 | + storageLocation: storageLocation |
| 77 | + storageName: storageName |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +The module uses a Bicep file named **storage.bicep** with the following contents: |
| 83 | + |
| 84 | +```bicep |
| 85 | +param storageLocation string |
| 86 | +param storageName string |
| 87 | +
|
| 88 | +resource storageAcct 'Microsoft.Storage/storageAccounts@2022-09-01' = { |
| 89 | + name: storageName |
| 90 | + location: storageLocation |
| 91 | + sku: { |
| 92 | + name: 'Standard_LRS' |
| 93 | + } |
| 94 | + kind: 'Storage' |
| 95 | + properties: {} |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Create resource group during resource group deployment |
| 100 | + |
| 101 | +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. |
| 102 | + |
| 103 | +```bicep |
| 104 | +param secondResourceGroup string |
| 105 | +param secondSubscriptionID string = '' |
| 106 | +param secondLocation string |
| 107 | +
|
| 108 | +// module deployed at subscription level |
| 109 | +module newRG 'resourceGroup.bicep' = { |
| 110 | + name: 'newResourceGroup' |
| 111 | + scope: subscription(secondSubscriptionID) |
| 112 | + params: { |
| 113 | + resourceGroupName: secondResourceGroup |
| 114 | + resourceGroupLocation: secondLocation |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +To deploy to a resource group, use the resource group deployment commands. |
| 120 | + |
| 121 | +For Azure CLI, use [az deployment group create](/cli/azure/deployment/group#az-deployment-group-create). |
| 122 | + |
| 123 | +```azurecli-interactive |
| 124 | +az deployment group create \ |
| 125 | + --name demoRGDeployment \ |
| 126 | + --resource-group ExampleGroup \ |
| 127 | + --template-file main.bicep \ |
| 128 | + --parameters secondResourceGroup=newRG secondSubscriptionID={sub-id} secondLocation=westus |
| 129 | +``` |
| 130 | + |
| 131 | +For the PowerShell deployment command, use [New-AzResourceGroupDeployment](/powershell/module/az.resources/new-azresourcegroupdeployment). |
| 132 | + |
| 133 | +```azurepowershell-interactive |
| 134 | +New-AzResourceGroupDeployment ` |
| 135 | + -Name demoRGDeployment ` |
| 136 | + -ResourceGroupName ExampleGroup ` |
| 137 | + -TemplateFile main.bicep ` |
| 138 | + -secondResourceGroup newRG ` |
| 139 | + -secondSubscriptionID {sub-id} ` |
| 140 | + -secondLocation westus |
| 141 | +``` |
| 142 | + |
| 143 | +## Next steps |
| 144 | + |
| 145 | +To learn about other scopes, see: |
| 146 | + |
| 147 | +* [Resource group deployments](deploy-to-resource-group.md) |
| 148 | +* [Subscription deployments](deploy-to-subscription.md) |
| 149 | +* [Management group deployments](deploy-to-management-group.md) |
| 150 | +* [Tenant deployments](deploy-to-tenant.md) |
0 commit comments