Tutorial: Deploying Azure Resources in Multiple Resource Groups with Azure Bicep Language

Ajinkya Bhabal
4 min readMar 14, 2021

Introduction

What is Azure Bicep?

Bicep is a Domain Specific Language (DSL) for deploying Azure resources declaratively. It’s evolved version of Azure ARM templates, with some additional great benefits. It simplifies the authoring experience in IaaC with a cleaner syntax, improved type safety, and better support for modularity and code re-use.

Here in Bicep we still create ARM template after building the code and through which deployment is done.

Those who are already familiar with IaaC 3rd party tool like eg. Terraform can easily get along with me. Now the reason for trying this tool is obviously it’s azure native service.

As I have been working on Terraform for long time, I feel that terraform lags behind when bringing new features and updates on cloud services, at the end it’s an open source tool it takes lot’s of effort to bring latest feature. I got fond of terraform because it provide some of the features like e.g. Multi-cloud, Dry runs, Module reusability, Easy to understand language, Data sources that no other tool can provide efficiently in that category.

For this tutorial, We will deploy lifecycle of virtual machine along with it’s resources. So here first resource group will contain Network Resources and second resource group will contain Virtual Machine Resources.

Tools Used in this Tutorial:

· Microsoft Visual Studio Code

· Powershell 7

· Bicep

· Azure Az Powershell Module

Now let’s get started with the initial configuration.

Microsoft Azure has created wonderful Visual studio extension for Bicep, with built-in Intellisense! Make sure to add it.

Bicep — Visual Studio Marketplace

Now we need our PowerShell to be Authenticated with an Azure.

Connect-AzAccount

First take a look at our network resources module.

Here Network Security Group with 3389 Inbound rule and Virtual Network with Single Subnet is created, also NSG is attached to Subnet. To learn more about code visit bicep official repository.

Second will be Virtual Machine resources Module:

Here we will create Public IP Address, Network Interface and Windows 2016 Datacenter Server resource. To learn more about code visit bicep official repository.

Now challenge comes, where we will be deploying resources in multiple resource group and subnet id need to be passed to the Network Interface of Virtual Machine.

I have used Reusability feature i.e. Modules in Bicep.

The solution for our challenge is we have passed virtual network resource object in output and use in another Virtual Machine Module where we add NIC to that Subnet.

The last file will be parameters, it’s the same as ARM Template parameter file in JSON format.

Now let’s deploy resources into an Azure.

First we will create two resource groups through PowerShell.

We will perform Bicep build in which ARM template will be generated for resource deployment. With Native ARM template deployment command resources will be created.

bicep build .\main.bicepNew-AzResourceGroupDeployment -TemplateFile "./main.json" -ResourceGroupName "RG1" -TemplateParameterFile "./parameters.json"

Let’s take a look at the Azure Portal to check successful deployment.

Resource Group 1 Deployments:-

Resource Group 2 Deployments:-

The overcoming multi resource group deployment was big challenge because, in PowerShell or CLI Command you have option to pass single resource group name in parameter and scope option in bicep is still under development.

As we have now came to an end, share your experience with me in the comments and suggest me what other feature or topic in IaaC should i cover next. Thank you.

Full code is available on github :- ajinkya101/bicep-multi-rg (github.com)

You can connect me @Ajinkya Bhabal | LinkedIn

Reference:

Copyrights ©2021 | All rights reserved by Ajinkya Ravindra Bhabal

--

--