Bicep Language For Deploying Azure Resources

27 / Mar / 2024 by Deepak . 0 comments

Introduction 

Bicep is a domain-specific language (DSL) that deploys Azure resources with declarative syntax. With a flexible syntax designed especially for Azure Resource Manager (ARM) templates, the Bicep language offers a useful way to manage Azure infrastructure installations.Bicep provides an attractive solution for environments where enterprises want to apply Infrastructure as Code (IaC) principles. It allows application code to be automated, version-controlled, and defined alongside infrastructure specifications. The language makes it easier to use agile development techniques like Continuous Integration/Continuous Deployment (CI/CD) thanks to its smooth interaction with Azure services and tools like Azure CLI and Azure DevOps. Additionally, Bicep acts as a link for companies moving away from ARM templates, facilitating a more seamless transition as they update their infrastructure provisioning procedures. 

Scenario 

In this blog, we will see how we can utilise Bicep as Infrastructure as Code (IaC) principles to ensure consistency, repeatability, and version control. Whether provisioning virtual machines, storage accounts, or Azure App Services, Bicep empowers you to define your infrastructure requirements efficiently in a meaningful manner.

Bicep Installation 

There are two types available for Bicep installation : 

  • Visual Studio Code and Bicep extension : Language support and resource auto completion are available in Visual Studio Code when the Bicep plugin is installed. The extension facilitates Bicep file creation and validation.

To install the extension, search for bicep in the Extensions tab in VS Code and select Install

  • Azure CLI  : You have all you need to deploy and decompile Bicep files when you utilise the Azure CLI with Bicep. When a command that requires the Bicep CLI is run using Azure CLI, it installs it automatically.

 Azure CLI version 2.20.0 or later must be installed. 

Install Azure CLI with one command : 

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

 Install Azure Bicep : 

az bicep install

To validate your Bicep CLI and Azure CLI installation :            

az --version 
az bicep version

Parameters and Variables in Bicep

  • Resource Manager settles the parameter values. Resource Manager substitutes the resolved value for the parameter whenever it is used.
  • One of the data types must be selected for each parameter.
  • In a Bicep file, you are restricted to 256 parameters.
  • A parameter cannot share a name with another parameter in the same scope, nor with a variable, resource, or output.
  • Decorators are used by parameters to store metadata or constraints.  
  • The decorators are positioned above the parameter’s declaration and have the @expression format. 

Declaration : 

param <parameter-name> <parameter-data-type> = <default-value>
@secure() 

param password string  

@description('description here') 

param size string
  • To make developing your Bicep file easier, you can use variables.
  • There cannot be a variable named the same as a resource, module, or parameter.
  • the variable doesn’t have a data type specified.

Declaration

var <variable-name> = <default-value>

Resource Declaration in Bicep

Azure resources within your infrastructure are defined and provisioned using resource declarations in Bicep. Use the resource keyword to add a resource declaration. The resource has a symbolic name that you have set. Names with symbols have case-sensitivity. They might have underscores (_), digits, and letters in them. Numbers are not where they can begin. A parameter, variable, or module cannot have the same name as a resource.

Declaration 

 

Use the for syntax to deploy multiple instances of a resource :  

resource <symbolic-name> '<full-type-name>@<api-version>' = [for <item> in <collection>: {   
<properties-to-repeat> 
}]

Example of storage account in Bicep ( main.bicep file ) 

param name string = 'examplestorage'

param location string =  'westus3'

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {

  name: name 

  location: location 

  sku: {

    name: 'Standard_LRS'

    tier: 'Standard'

  }

  kind: 'StorageV2'

  properties: {

    accessTier: 'Hot'

  }

}

 

To run the above code use the below az cli command 

az deployment group create -f <bicep-file-name>  -g <resource-goup-name>

Use existing resources in Bicep

When deploying a resource that requires a value to be obtained from an existing resource, use the existing keyword.The same scope attribute is utilized if your resources are already specified in the same resource group as the current deployment and you wish to access or extract data from those resources.Use the various scope property if you wish to access or extract resources that have already been produced in different resource groups for your current deployment.

resource stg 'Microsoft.Storage/storageAccounts@2022-09-01' existing = {

  name: 'examplestorage'

  scope: resourceGroup(‘exampleRG’)

}

output blobEndpoint string = stg.properties.primaryEndpoints.blob

Modules In Bicep

Azure Bicep modules encapsulate resources and configurations into modular components, allowing you to organize and reuse your Bicep code.This encourages the ability to share infrastructure patterns across other projects or environments, as well as the reuse and maintainability of code

Definition Syntax

The basic syntax for defining a module is:

module <symbolic-name> '<path-to-file>' = {

name: '<linked-deployment-name>'

params: {

<parameter-names-and-values>

}

}

 

Let’s talk about Advantages and Disadvantages of Bicep : 

Bicep Advantages

  • Readability: Bicep code is written in a declarative language that is easy to read and understand. This makes it easier to manage and maintain your infrastructure. 
  • Maintainability: Bicep code is organised into modules, which makes it easy to reuse code and keep your infrastructure organised. 
  • Infrastructure as code (IaC): Bicep allows you to manage your infrastructure as code, which gives you more control and flexibility. 
  • Cross-platform: Bicep is a cross-platform tool that can be used on Windows, Mac, and Linux. 
  • Open source: Bicep is an open source tool that is supported by Microsoft.
  • Orchestration: Resource Manager orchestrates the deployment of interdependent resources so they’re created in the correct order. When possible, Resource Manager deploys resources in parallel so your deployments finish faster than serial deployments.
  • Integration with Azure services: Bicep is integrated with Azure services such as Azure Policy, template specs, and Blueprints.
  • Preview changes: You can use the what-if operation to get a preview of changes before deploying the Bicep file. With what-if, you see which resources will be created, updated, or deleted, and any resource properties that will be changed. The what-if operation checks the current state of your environment and eliminates the need to manage state.
  • No state or state files to manage: All state is stored in Azure. Users can collaborate and have confidence their updates are handled as expected.

Bicep Disadvantages

  • The language is domain specific, meaning that only Microsoft Azure is supported. You are not using BICEP if you are using AWS.
  • Obtaining relatively new tools and examples could be challenging.
  • Dependency on the Azure Resource Manager (ARM).  

We can compare Bicep with the terraform.

Bicep vs Terraform

  • While Terraform requires you to maintain state files, Bicep does not contain any state files. Errors arise from any corruption or deletion of the state file
  • .Bicep is compatible with all Azure resources and is built using ARM templates. Terraform, being developed by a third party, requires time for implementation for every Azure or AWS resource.
  • Bicep offers a more efficient deployment experience for Azure-centric setups because it is designed exclusively for the purpose of deploying Azure resources. However, Terraform provides support for several clouds and a larger ecosystem.

Conclusion

This blog allows us to comprehend the fundamental logic and syntax of the Bicep language, which is used to deploy Azure resources. Just outline the main benefits and fundamental syntax of the Bicep language in this blog. 

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *