Deploying a resource group via parameter
In this guide, we'll walk through the steps to deploy a resource with a parameter file in Azure using Bicep.
Rather than passing parameters as inline values in your script, you can use a Bicep parameters file with the .bicepparam file extension that contains the parameter values.
A single Bicep file can have multiple Bicep parameters files associated with it. However, each Bicep parameters file is intended for one particular Bicep file. This relationship is established using the using statement within the Bicep parameters file.
So in short you create 1 deployment file and use multiple parameter files to deploy several resources in one go.
Create a bicep file
Open Visual Studio Code in the C:\DTX-IaC-Demo
folder and copy the resourcegroup-networking.bicep
to a new file with the name resourcegroup.bicep
.
- Select the file
resourcegroup-networking.bicep
in the explorer
- Press CTRL-C to copy the file
- Press CTRL-V to paste the file
- Rename the file to
resourcegroup.bicep
by selecting the fileresourcegroup-networking copy.bicep
and pressing F2
We can now reuse the resourcegroup.bicep
file to deploy the resource group.
Create the parameter file
Visual Studio Code has a nice feature to create a parameter file for a bicep file.
- Select the
resourcegroup.bicep
file and right click, chooseGenerate Parameters File
- Output format: bicepparam
- Parameters: all
A .bicepparam
file is created with the name resourcegroup.bicepparam
and should look like this:
using './resourcegroup.bicep'
param location = 'westeurope'
param name = 'rg-networking'
We can now create several parameter files for the different resources we want to deploy, and reuse this template in other modules.
Line 1 points to the bicep file we want to use, and line 3 and 4 are the parameters we want to use in the bicep file.
We can now create some new parameters files to create the resource groups we need later on:
- Copy the
resourcegroup.bicepparam
file to a new file with the nameresourcegroup-002.bicepparam
.- Set the name for the resource group to
rg-virtualmachine
- Set the name for the resource group to
- Copy the
resourcegroup.bicepparam
file to a new file with the nameresourcegroup-003.bicepparam
.- Set the name for the resource group to
rg-monitoring
- Set the name for the resource group to
Deploy all the parameter file
foreach ($parameterFile in Get-ChildItem -Path . -Recurse -Filter 'resourcegroup*.bicepparam') { $paramFilePath = $($parameterFile.Name).Replace("\", "/"); az deployment sub create --parameters $paramFilePath --location westeurope }
This script will loop through all the parameter files that start with the name resourcegroup
and have file extension of .bicepparam
in the current folder and deploy the resources to the subscription.
The file found are deployed to the location westeurope
.
Template
Your parameter file resourcegroup-002.bicepparam
should look like this:
using './resourcegroup.bicep'
param location = 'westeurope'
param name = 'rg-virtualmachine'
Version overview
This document has the following versions:
Version | Date | Overview of changes |
---|---|---|
1.1 | 2024-03-18 | Updated the document. |
1.0 | 2024-03-06 | Initial version. |