Skip to content

Commit 773a39f

Browse files
Merge pull request #252869 from tfitzmac/0926createrg
move create resource group
2 parents 43bb71d + 52461de commit 773a39f

File tree

4 files changed

+161
-117
lines changed

4 files changed

+161
-117
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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)

articles/azure-resource-manager/bicep/deploy-to-resource-group.md

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ module exampleModule 'module.bicep' = {
148148
}
149149
```
150150

151-
For an example template, see [Create resource group](#create-resource-group).
151+
For an example template, see [Create resource group with Bicep](create-resource-group.md).
152152

153153
### Scope to tenant
154154

@@ -248,53 +248,7 @@ resource storageAcct 'Microsoft.Storage/storageAccounts@2019-06-01' = {
248248

249249
## Create resource group
250250

251-
From a resource group deployment, you can switch to the level of a subscription and create a resource group. The following template deploys a storage account to the target resource group, and creates a new resource group in the specified subscription.
252-
253-
```bicep
254-
@maxLength(11)
255-
param storagePrefix string
256-
257-
param firstStorageLocation string = resourceGroup().location
258-
259-
param secondResourceGroup string
260-
param secondSubscriptionID string = ''
261-
param secondLocation string
262-
263-
var firstStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
264-
265-
// resource deployed to target resource group
266-
module firstStorageAcct 'storage2.bicep' = {
267-
name: 'storageModule1'
268-
params: {
269-
storageLocation: firstStorageLocation
270-
storageName: firstStorageName
271-
}
272-
}
273-
274-
// module deployed to subscription
275-
module newRG 'resourceGroup.bicep' = {
276-
name: 'newResourceGroup'
277-
scope: subscription(secondSubscriptionID)
278-
params: {
279-
resourceGroupName: secondResourceGroup
280-
resourceGroupLocation: secondLocation
281-
}
282-
}
283-
```
284-
285-
The preceding example uses the following Bicep file for the module that creates the new resource group.
286-
287-
```bicep
288-
targetScope='subscription'
289-
290-
param resourceGroupName string
291-
param resourceGroupLocation string
292-
293-
resource newRG 'Microsoft.Resources/resourceGroups@2021-01-01' = {
294-
name: resourceGroupName
295-
location: resourceGroupLocation
296-
}
297-
```
251+
For information about creating resource groups, see [Create resource group with Bicep](create-resource-group.md).
298252

299253
## Next steps
300254

articles/azure-resource-manager/bicep/deploy-to-subscription.md

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
title: Use Bicep to deploy resources to subscription
3-
description: Describes how to create a Bicep file that deploys resources to the Azure subscription scope. It shows how to create a resource group.
3+
description: Describes how to create a Bicep file that deploys resources to the Azure subscription scope.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 06/23/2023
6+
ms.date: 09/26/2023
77
---
88

99
# Subscription deployments with Bicep files
1010

11-
This article describes how to set scope with Bicep when deploying to a subscription.
11+
To simplify the management of resources, you can deploy resources at the level of your Azure subscription. For example, you can deploy [policies](../../governance/policy/overview.md) and [Azure role-based access control (Azure RBAC)](../../role-based-access-control/overview.md) to your subscription, which applies them across your subscription.
1212

13-
To simplify the management of resources, you can deploy resources at the level of your Azure subscription. For example, you can deploy [policies](../../governance/policy/overview.md) and [Azure role-based access control (Azure RBAC)](../../role-based-access-control/overview.md) to your subscription, which applies them across your subscription. You can also create resource groups within the subscription and deploy resources to resource groups in the subscription.
13+
This article describes how to set the deployment scope to a subscription in a Bicep file.
1414

1515
> [!NOTE]
1616
> You can deploy to 800 different resource groups in a subscription level deployment.
@@ -170,7 +170,7 @@ resource exampleResource 'Microsoft.Resources/resourceGroups@2022-09-01' = {
170170
}
171171
```
172172

173-
For examples of deploying to the subscription, see [Create resource groups](#create-resource-groups) and [Assign policy definition](#assign-policy-definition).
173+
For examples of deploying to the subscription, see [Create resource groups with Bicep](create-resource-group.md) and [Assign policy definition](#assign-policy-definition).
174174

175175
To deploy resources to a subscription that is different than the subscription from the operation, add a [module](modules.md). Use the [subscription function](bicep-functions-scope.md#subscription) to set the `scope` property. Provide the `subscriptionId` property to the ID of the subscription you want to deploy to.
176176

@@ -201,7 +201,7 @@ module exampleModule 'module.bicep' = {
201201
}
202202
```
203203

204-
If the resource group is created in the same Bicep file, use the symbolic name of the resource group to set the scope value. For an example of setting the scope to the symbolic name, see [Create resource group and resources](#create-resource-group-and-resources).
204+
If the resource group is created in the same Bicep file, use the symbolic name of the resource group to set the scope value. For an example of setting the scope to the symbolic name, see [Create resource group with Bicep](create-resource-group.md).
205205

206206
### Scope to tenant
207207

@@ -242,69 +242,7 @@ For more information, see [Management group](deploy-to-management-group.md#manag
242242

243243
## Resource groups
244244

245-
### Create resource groups
246-
247-
To create a resource group, define a [Microsoft.Resources/resourceGroups](/azure/templates/microsoft.resources/allversions) resource with a name and location for the resource group.
248-
249-
The following example creates an empty resource group.
250-
251-
```bicep
252-
targetScope='subscription'
253-
254-
param resourceGroupName string
255-
param resourceGroupLocation string
256-
257-
resource newRG 'Microsoft.Resources/resourceGroups@2022-09-01' = {
258-
name: resourceGroupName
259-
location: resourceGroupLocation
260-
}
261-
```
262-
263-
### Create resource group and resources
264-
265-
To create the resource group and deploy resources to it, add a module. The module includes 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.
266-
267-
The following example 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.
268-
269-
```bicep
270-
targetScope='subscription'
271-
272-
param resourceGroupName string
273-
param resourceGroupLocation string
274-
param storageName string
275-
param storageLocation string
276-
277-
resource newRG 'Microsoft.Resources/resourceGroups@2022-09-01' = {
278-
name: resourceGroupName
279-
location: resourceGroupLocation
280-
}
281-
282-
module storageAcct 'storage.bicep' = {
283-
name: 'storageModule'
284-
scope: newRG
285-
params: {
286-
storageLocation: storageLocation
287-
storageName: storageName
288-
}
289-
}
290-
```
291-
292-
The module uses a Bicep file named **storage.bicep** with the following contents:
293-
294-
```bicep
295-
param storageLocation string
296-
param storageName string
297-
298-
resource storageAcct 'Microsoft.Storage/storageAccounts@2022-09-01' = {
299-
name: storageName
300-
location: storageLocation
301-
sku: {
302-
name: 'Standard_LRS'
303-
}
304-
kind: 'Storage'
305-
properties: {}
306-
}
307-
```
245+
For information about creating resource groups, see [Create resource group with Bicep](create-resource-group.md).
308246

309247
## Azure Policy
310248

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@
398398
items:
399399
- name: Access control
400400
href: scenarios-rbac.md
401+
- name: Create resource group
402+
href: create-resource-group.md
401403
- name: Monitoring
402404
href: scenarios-monitoring.md
403405
- name: Secrets

0 commit comments

Comments
 (0)