Deploying a Resource Group
In this guide, we'll walk through the steps to deploy a resource group in Azure using Bicep.
A resource group is a container that holds related resources for an Azure solution.
The resource group can include all the resources for the solution, or only those resources that you want to manage as a group.
You decide how you want to allocate resources to resource groups based on what makes the most sense for your organization.
Generally, add resources that share the same lifecycle to the same resource group so you can easily deploy, update, and delete them as a group.
The resource group stores metadata about the resources. Therefore, when you specify a location for the resource group, you are specifying where that metadata is stored. For compliance reasons, you may need to ensure that your data is stored in a particular region.
Create a bicep file
Open Visual Studio Code in the C:\DTX-IaC-Demo
folder and create a new file with the name resourcegroup-networking.bicep
.
Create code
Write the Bicep code to define the resource group and save the file. Below is a sample code snippet:
targetScope = 'subscription'
param location string = 'westeurope'
param name string = 'rg-networking'
resource resResourceGroup 'Microsoft.Resources/resourceGroups@2022-09-01' = {
location: location
name: name
}
Make sure you save your files before you start a deployment!
Template build-up
Target scope
By default, the target scope is set to resourceGroup. If you're deploying at the resource group level, you don't need to set the target scope in your Bicep file.
The allowed values are:
- resourceGroup - default value, used for resource group deployments
- subscription - used for subscription deployments
- managementGroup - used for management group deployments
- tenant - used for tenant deployments
As shown in the use-case a under a subscription a resource group is created, under a resource group all other resources can be created.
We're starting with a resource group so the target scope is set to subscription.
Parameters
The parameters are used to define the input values for the deployment, all these parameters can be used throughout the deployment.
When setting parameters you don't have to change the static name(s)/value(s) in the template when you change it.
Resource
All resources are defined in the resource block, Azure has several resource providers and resource types.
More information can be found here
When deploying a resource this is your starting point.
After setting the resource provider all mandatory fields must be supplied.
With the autocomplete function is Visual Studio Code (with the bicep extension enabled) you are pointed to wrong code or missing options.
Remove the complete line name: name
, you will see the curly red line under resResourceGroup
pointing you to the error.
Re-add the line name: name
(CTRL-Z) back in to the template, so that we can deploy the resource group.
Deploy the resource
Within the Visual Studio Code terminal at the following line:
az deployment sub create --template-file .\resourcegroup-networking.bicep --location westeurope
Error deploying the resource group?
When getting the error below if you run the script.
[WinError 193] %1 is not a valid Win32 application
Run the following command in a PowerShell command to fix the issue.
Get-ChildItem -Path $env:USERPROFILE -Recurse -Filter "bicep.exe" | Remove-Item
az bicep install
Check the deployed resource
- Open the azure portal via this link
- Login with your credentials
- Scroll down to the chapter
Navigate
and click onResource Groups
- In this list you will see the newly created resource group
- Delete the resource group in the portal
- Click on the resource group and click on the
Delete resource group
button - Enter the resource group name and click
delete
- Wait for the deletion to complete
- Click on the resource group and click on the
- Re-run the PowerShell link and refresh the portal to see the (re)created Resource Group
Summary
If the deployment was successful, you should see the resource group in the Azure portal.
And the output of the command should look something like below:
{
"id": "/subscriptions/xxxxxxxxe-xxxx-xxxx-xxxx-xxxxxxxxxxx/providers/Microsoft.Resources/deployments/resourceGroup",
"location": "westeurope",
"name": "resourceGroup",
"properties": {
"correlationId": "770bbf04-45be-440c-b927-8309d61d9b30",
"debugSetting": null,
"dependencies": [],
"duration": "PT1.3241446S",
"error": null,
"mode": "Incremental",
"onErrorDeployment": null,
"outputResources": [
{
"id": "/subscriptions/xxxxxxxxe-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/rg-networking"
}
],
"outputs": null,
"parameters": {
"location": {
"type": "String",
"value": "westeurope"
},
"name": {
"type": "String",
"value": "rg-networking"
}
},
"parametersLink": null,
"providers": [
{
"id": null,
"namespace": "Microsoft.Resources",
"providerAuthorizationConsentState": null,
"registrationPolicy": null,
"registrationState": null,
"resourceTypes": [
{
"aliases": null,
"apiProfiles": null,
"apiVersions": null,
"capabilities": null,
"defaultApiVersion": null,
"locationMappings": null,
"locations": [
"westeurope"
],
"properties": null,
"resourceType": "resourceGroups",
"zoneMappings": null
}
]
}
],
"provisioningState": "Succeeded",
"templateHash": "9667660555647686390",
"templateLink": null,
"timestamp": "2024-02-28T11:10:26.829111+00:00",
"validatedResources": null
},
"tags": null,
"type": "Microsoft.Resources/deployments"
}
Template
Your template resourcegroup-networking.bicep
should look like this:
targetScope = 'subscription'
param location string = 'westeurope'
param name string = 'rg-networking'
resource resResourceGroup 'Microsoft.Resources/resourceGroups@2022-09-01' = {
location: location
name: name
}
Version overview
This document has the following versions:
Version | Date | Overview of changes |
---|---|---|
1.1 | 2024-03-18 | Added comments from test run. |
1.0 | 2024-02-28 | Initial version. |