Saturday 15 May 2021

Azure Infrastructure as Code (IaC) with Bicep - Getting Started with Development

 Bicep is the latest declarative IaC tool from Microsoft for Azure, which is built on top of Azure Resource Manager (ARM) templates. Bicep offers you the ability to deploy even preview resources on Azure as it is always will be the most up to date IaC declarative tool Azur. Bicep convertors to into ARM JSON for execution. We have seen how to setup development environment for Bicep in windows in the post here. In this post let's get started with developing Bicep code to deploy infrastructure to Azure.

Let's create a folder named Infra and add our first Bicep file to it in Vs Code. Bicep files should be added with .bicep extension. Once you add the man.bicep file you will see VS Code is trying to load the Bicep language service as you have already setup the extension for Bicep in VS code as described in here.

Let's try to create a resource group with Bicep as the first step. We need to setup our Bicep target scope to subscription in order to setup a create group. Target scope can be resource group, tenant, management group or subscription in Azure. 

Pretty awesome intellisense. Lets go ahead with subscription scope. To create a resource group start typing rg and you would see the intellisense kick in and show you a template.

Once you press enter to select the template resource group template would be added. However, there is an issue with the added template. Until you get rid of single quotes in the resource type it will be shown as a syntax issue.

You can change the resource identifier name to nay preferred name. the single quotes should not be there and it is accepted as proper syntax.

As the next step let's parameterize the resource group name and the location. 

Let's try to execute the resource group creation using PowerShell. Open a PowerShell window and change directory to Infra folder which contains the main.bicep file. Then execute az login command to and when browser opened log on to your Azure account. Next execute az account set --subscription yoursubscriptionid command to select the Azure subscription. Then you can run below command with parameters to get the details of what would happen if the deployment is done. Note --what-if is giving you the the actions going to happen if the deployment done. As indicated a resource group would be created.

az deployment sub create --location centralus --template-file main.bicep --parameters rgName=rg-bicepdemo01 rgLocation=centralus --what-if

To deploy the resource group run the command without --what-if.

If you try to rerun the command with --what-f you can see there is no change as the resource group is already created.

In the next post let's see how we can create resources in the resource group we created with Bicep.

No comments:

Popular Posts