Wednesday, July 14, 2021

What is bicep in azure?

Bicep is a Domain Specific Language (DSL) for deploying Azure infrastructure (resources) declaratively. Bicep code is transpiled to standard ARM Template JSON files (Infrastructure as code file), which effectively treats the ARM Template as an Intermediate Language (IL). Bicep provides concise syntax, reliable type safety, and support for code reuse. We believe Bicep offers the best authoring experience for your Azure infrastructure as code solutions.

You can use Bicep instead of JSON to develop your Azure Resource Manager templates (ARM templates). The JSON syntax to create an ARM template can be verbose and require complicated expressions. Bicep syntax reduces that complexity and improves the development experience. Bicep is a transparent abstraction over ARM template JSON and doesn't lose any of the JSON template capabilities. During deployment, Bicep CLI transpiles a Bicep file into ARM template JSON.


Create Azure Resource (Infrastructure) in Visual studio code

We will create azure resource inside visual studio code with help of Bicep. Need to follow below steps.

1.       Install visual studio code below URL

https://code.visualstudio.com/download

2.       Once visual studio code is installed then need to add Bicep extension like below


3.       After installing Bicep then code IntelliSense will come in VS Code for azure resources. Now open to the folder from VS code file menu, where you want to create Bicep project and add new file name (main.bicep)   




4.       We will write code for azure storage account (infrastructure) inside bicep file like below. I have also mapped bicep code with azure storage account resource for better understanding.


Sample code:

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01'={
  name'bicepstoarge'
  location'eastus'
  kind:'Storage'
  sku:{
      name:'Premium_LRS'
  }
}

5.       Once code if written for azure resource (infrastructure) then build the code it will convert bicep file into ARM template JSON.

6.       To build the bicep file we need to run Bicep CLI in visual studio code terminal as below

Manual with PowerShell

https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/install#install-manually

 

# Create the install folder

$installPath = "$env:USERPROFILE\.bicep"

$installDir = New-Item -ItemType Directory -Path $installPath -Force

$installDir.Attributes += 'Hidden'

# Fetch the latest Bicep CLI binary

(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-win-x64.exe", "$installPath\bicep.exe")

# Add bicep to your PATH

$currentPath = (Get-Item -path "HKCU:\Environment" ).GetValue('Path', '', 'DoNotExpandEnvironmentNames')

if (-not $currentPath.Contains("%USERPROFILE%\.bicep")) { setx PATH ($currentPath + ";%USERPROFILE%\.bicep") }

if (-not $env:path.Contains($installPath)) { $env:path += ";$installPath" }

# Verify you can now access the 'bicep' command.

bicep --help

# Done!

 


7.       Once Bicep CLI is installed, now build the bicep file below command

bicep build main.bicep    


8.       After build new JSON file (main.json) will created in visual studio code. It is ARM (Azure Resource Manager) template which we will deploy with help of Azure CLI.

9.       Once ARM templated (JSON) is created now we will deploy this resource in Azure with help of Azure CLI.

Deploy resources with ARM templates and Azure CLI

We will see how to use Azure CLI with Azure Resource Manager templates (ARM templates) to deploy your resources to Azure.

To deploy ARM template, install the latest version of the Azure CLI.

URL: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli

Deploy resource in Azure need to follow these steps

1.       Run az login to create a connection with Azure





 

2.       It will open azure portal login screen. We need to put our azure loginid and password.


3.       Once login done successfully then need to select the subscription, where you want to deploy your resource.

az account set --subscription "<Your Subscription name>"

 

4.       Once subscription is selected then we ready to deploy our ARM template (JSON) on azure. We will do that with below command

az deployment group create --resource-group DJBlogs --template-file main.json


5.       Once above command is executed successfully then resource will be created in Azure


Hope it will help you to give little idea about bicep and ARM template and We can use all these to put our application infrastructure in code repository and automate our infrastructure.

Keep sharing keep learning. Cheers

No comments:

Post a Comment