In this section, you will learn Azure Bicep and what it offers to developers and DevOps teams. There will be a few code snippets to get your eyes familiar with the syntax and, finally, an overview of its components and building blocks. But before we begin, let's review some concepts to make sure we are on the same page.
IaC
Currently, many companies try to automate their infrastructure creation and maintenance. To do that, and to further keep track of what is happening or has happened in their environments, they use a set of scripts or code files alongside tools and processes that streamline the whole deployment for them.
This practice is called IaC and helps every team to safely establish and configure the required infrastructure for their applications and solutions. This practice became even more simplified when all cloud providers added the ability for their customers to use it, which in terms of Microsoft is called Azure Resource Manager (ARM) templates.
ARM templates
Microsoft Azure used to have a deployment model called classic, which helped users deal with individual services (called cloud services) using three components: service definition, service configuration, and service package. There is no need to delve into the details of these concepts; suffice to say that soon they realized this approach needed to change if they wanted to allow their users to leverage the full potential of IaC.
That is when ARM was introduced as a new way of deploying cloud services in Azure that supported other tools, such as the Azure portal, the Azure CLI, Azure PowerShell, and all their SDKs. Here is what the new deployment model looks like:
Figure 1.1 – Azure Resource Manager
As you can see, it's a much better-unified model, which allows their customers to deploy their resources using a centralized management layer. In addition to that, Microsoft introduced ARM templates, JavaScript Object Notation (JSON) documents, which would declare what resources are needed to be deployed and what the resource manager will use to deploy them in a group, along with their configuration and other files if needed.
Using ARM templates, developers and DevOps engineers can create repeatable deployments that offer auditability, extensibility, testing, modularity, previews, detailed logs, and a status update on the deployments as resources are getting created or modified.
Azure Bicep
Even though ARM templates are great to begin with, it turns out that JSON is not the ideal format to use, especially when the number of resources is high or the deployment model gets complex with time. So, Microsoft started to work on a project called Bicep as a revision to ARM templates to overcome some of the issues people were facing.
Azure Bicep is a Domain-Specific Language (DSL) that has all the benefits of ARM templates, but instead of using JSON, it uses a new language to overcome its shortcomings. It is designed to simplify the authoring experience of IaC and bring more integration with other tools, such as the Azure CLI and Visual Studio Code.
It is worth mentioning that Azure Bicep files will get transpiled to ARM templates very much like how TypeScript files transpile to JavaScript. This means every type, resource, and property that is valid in an ARM template is valid in Bicep as well. We will not go into the details of what a Bicep file looks like, but to pique your curiosity, here is the famous Hello World!
in Bicep language:
param message string
var hello = 'Hello World! - Hi'
output helloWorld string = '${hello} ${message}'
When you compile this file, you will get an ARM template that looks like the following:
{
„$schema": „https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "14991011523779832704"
}
},
"parameters": {
"message": {
"type": "string"
}
},
"functions": [],
"variables": {
"hello": "Hello World! - Hi"
},
"resources": [],
"outputs": {
"helloWorld": {
"type": "string",
"value": "[format('{0} {1}', variables('hello'), parameters('message'))]"
}
}
}
It is amazing how simplified the Bicep file is, don't you agree? Some of the advantages of Azure Bicep are as follows:
- Support for all current resource providers
- A syntax that simplifies the IaC and can reduce a huge amount of code compared to ARM templates
- No need to manage any state (compared to some third-party tools, such as Terraform)
- Native integration with Microsoft tools, such as the Azure CLI and Visual Studio Code
- A modular structure that allows you to break your code and create your IaC with peace of mind
- Backed up by Microsoft support plans
- Native integration with other Azure resources, such as Azure Policy and Blueprints