Hands-on Demo – Create Bicep templates
Let’s go step-by-step and create a simple Bicep template. This will demonstrate how to structure and define parameters, resources, outputs, and other features such as loops and conditions.
We'll create a Bicep template to deploy an Azure Virtual Network (VNet) with a subnet, and use a parameter to conditionally create a storage account.
1. Creating Bicep Template
Setting Up Your Environment
Ensure you have the following installed:
Visual Studio Code with the Bicep extension.
Bicep CLI installed (either via package managers like
winget,choco, or manual installation).
Create the Bicep Template
Let’s create a simple deployment template that:
Accepts parameters for location, VNet name, and whether to create a storage account.
Creates a VNet with a subnet.
Conditionally creates a storage account based on the parameter.
Define Parameters for Location and VNet Configuration
We’ll define the following parameters:
location: Specifies where the resources will be created.vnetName: Name of the Virtual Network.subnetName: Name of the Subnet.createStorageAccount: Boolean flag to decide whether to create a storage account.
Define Variables for Address Prefixes
We will define variables for the address prefixes of the Virtual Network and Subnet.
Define Resources (VNet, Subnet, and Conditional Storage Account)
VNet: We’ll create a Virtual Network with a single subnet.
Storage Account: We’ll create a storage account conditionally, based on the
createStorageAccountparameter.
Outputs
The template will output the IDs of the created resources (VNet, Subnet, and Storage Account).
2. Create the Bicep File (main.bicep)
Here's the Bicep template that will deploy a Virtual Network and optionally a Storage Account:
xxxxxxxxxx441// Parameters2param location string = 'East US' // Default value3param vnetName string = 'myVNet'4param subnetName string = 'mySubnet'5param createStorageAccount bool = false // Default is false6// Variables7var addressPrefix = '10.0.0.0/16'8var subnetPrefix = '10.0.1.0/24'9// Resources10// Create Virtual Network11resource vnet 'Microsoft.Network/virtualNetworks@2020-05-01' = {12 name: vnetName13 location: location14 properties: {15 addressSpace: {16 addressPrefixes: [17 addressPrefix18 ]19 }20 }21}22// Create Subnet within the Virtual Network23resource subnet 'Microsoft.Network/virtualNetworks/subnets@2020-05-01' = {24 name: '${vnetName}/${subnetName}'25 parent: vnet26 properties: {27 addressPrefix: subnetPrefix28 }29}30// Conditionally Create Storage Account31resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = if (createStorageAccount) {32 name: '${vnetName}storage'33 location: location34 properties: {35 sku: {36 name: 'Standard_LRS'37 }38 kind: 'StorageV2'39 }40}41// Outputs42output vnetId string = vnet.id43output subnetId string = subnet.id44output storageAccountId string = storageAccount.id3. How the Template Works
Parameters Section
location: Specifies where to deploy the resources, with a default value of'East US'.vnetName: Name of the Virtual Network.subnetName: Name of the subnet within the VNet.createStorageAccount: A boolean flag that determines whether to create a storage account (default:false).
Variables Section
addressPrefix: The address space of the Virtual Network (10.0.0.0/16).subnetPrefix: The address prefix for the subnet (10.0.1.0/24).
Resources Section
Virtual Network (VNet): Creates a VNet with the specified name, location, and address space.
Subnet: Creates a subnet within the previously created VNet, using the subnet address prefix.
Conditional Storage Account: Only creates the storage account if the
createStorageAccountparameter istrue.
Outputs Section
vnetId: The ID of the created Virtual Network.
subnetId: The ID of the created subnet.
storageAccountId: The ID of the created storage account (if created).
4. Deploy the Template
To deploy this template, you’ll use the Azure CLI or PowerShell.
Using Azure CLI:
Open a terminal window and navigate to the directory where the main.bicep file is located.
Deploy with Parameters:
If you want to create a storage account, pass true for the createStorageAccount parameter:
xxxxxxxxxx41az deployment group create \2--resource-group <your-resource-group> \3--template-file main.bicep \4--parameters createStorageAccount=trueIf you don’t want to create the storage account, just leave the default value (false):
xxxxxxxxxx31az deployment group create \2--resource-group <your-resource-group> \3--template-file main.bicepUsing PowerShell:
xxxxxxxxxx41New-AzResourceGroupDeployment `2-ResourceGroupName <your-resource-group> `3-TemplateFile main.bicep `4-createStorageAccount $true5. Review the Output
Once the deployment is complete, you can review the output from the deployment.
If the
createStorageAccountparameter wastrue, you’ll see the storage account ID in the output.If
false, the output will not include a storage account ID.
You can also check the created resources in the Azure Portal, or use the following Azure CLI command to verify the resources:
xxxxxxxxxx71az network vnet show \2--resource-group <your-resource-group> \3--name <your-vnet-name>4
5az storage account show \6--resource-group <your-resource-group> \7--name <your-storage-account-name>6. Modify the Template
You can extend this Bicep template by adding more resources or changing the logic.
For example:
Add a public IP address for the VNet.
Add more subnets by looping over an array of subnet names.
Use conditions to deploy different types of resources (like different storage account types).
Example: Add a Loop to Create Multiple Subnets
You can modify the subnet resource section to create multiple subnets based on an array of subnet names:
xxxxxxxxxx81param subnetNames array = ['subnet1', 'subnet2', 'subnet3']2resource subnets 'Microsoft.Network/virtualNetworks/subnets@2020-05-01' = [for subnetName in subnetNames: {3 name: '${vnetName}/${subnetName}'4 parent: vnet5 properties: {6 addressPrefix: '10.0.${subnetNames.indexOf(subnetName)}.0/24'7 }8}]This would create multiple subnets with different address prefixes.
Summary
This demo showcased how to create a simple Bicep template for deploying a Virtual Network with a subnet, and conditionally creating a storage account. By leveraging parameters, variables, resources, outputs, and features like loops and conditional deployments, you can build flexible, reusable infrastructure templates for Azure.






















Leave a Reply