Leveraging Bicep to deploy Azure resources - Part 3
In this post, the focus is on highlighting Bicep as we install necessary prerequisites on Visual Studio to enable seamless work with it.
How to deploy resources on Azure ?
Deploying resources on Azure can be done directly through the Azure portal, which provides a graphical user interface (GUI) mode, or in a more technical manner using the Azure CLI. While these methods are convenient, they lack the capability to automate processes or ensure reproducibility. This limitation led Azure to introduce infrastructure as code with ARM templates, allowing for a more systematic and automated approach to resource deployment.
What are ARM templates ?
ARM templates are JSON files that define the infrastructure and configuration for our project. These templates use declarative syntax, which lets us state what we intend to deploy without having to write the sequence of programming commands to create it. Here is an example of an ARM template to create a storage account.
1{
2 "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3 "contentVersion": "1.0.0.0",
4 "resources": [
5 {
6 "type": "Microsoft.Storage/storageAccounts",
7 "apiVersion": "2022-09-01",
8 "name": "mystorageaccount",
9 "location": "East US",
10 "sku": {
11 "name": "Standard_LRS",
12 "tier": "Standard"
13 },
14 "kind": "StorageV2",
15 "properties": {}
16 }
17 ]
18}
This particular template specifies the creation of a storage account named "mystorageaccount" in the "East US" region, using the "Standard_LRS" SKU.
By the way, we observe the essence of declarative; in this context, we articulate what we wish to deploy without specifying the procedural steps. In practice, Microsoft interprets the file in an imperative manner, converting the definition into the corresponding REST API operation. This operation is then transmitted to the Microsoft.Storage resource provider.
1PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/mystorageaccount?api-version=2022-09-01
2REQUEST BODY
3{
4 "location": "eastus",
5 "sku": {
6 "name": "Standard_LRS"
7 },
8 "kind": "StorageV2",
9 "properties": {}
10}
This JSON file can subsequently be stored, reused for future purposes, and version-controlled, for example, using Git. It can also be incorporated into a continuous delivery (CD) pipeline within Azure DevOps, enabling the complete automation of the deployment process.
What are the limitations ?
So far, everything aligns with the process. However, the adoption of this syntax by software engineers proved to be challenging; they found it cumbersome and difficult to use. This was especially notable considering that other providers, such as Terraform, offered a much cleaner and intuitive method for declaring resources. In response to this feedback, Microsoft took action by introducing a new, more adapted language. Enter Bicep.
What is Bicep ?
Bicep is a declarative syntax for defining ARM templates. It is designed to simplify and enhance the authoring experience for ARM templates.
Bicep also employs a declarative syntax, allowing users to specify the desired state of their Azure resources without detailing the procedural steps to achieve that state.
Bicep abstracts the complexity of ARM templates, making it more concise and readable. It provides a higher-level abstraction that is closer to the intent of the user.
Bicep introduces a type system, enhancing code safety by ensuring that the specified types match the expected Azure resource types.
Bicep provides built-in validation tools to check for errors and issues in the code before deployment, reducing the likelihood of runtime errors.
Remember that Bicep is intended to be a more user-friendly alternative to writing ARM templates directly. It aims to streamline the process of provisioning and managing Azure resources by providing a more intuitive and efficient authoring experience. Indeed, it is not a groundbreaking technology in and of itself, nor is it something unprecedented.
Unlike Terraform and Ansible, which are versatile tools usable across various cloud providers such as Azure, AWS, and GCP, Bicep is specifically designed for Azure. It's important to note that Bicep should not be employed if the intention is to provision resources for tools like BigQuery or Redshift.
Now, we will delve into the practical aspects of Bicep. However, before proceeding, we need to install some prerequisites to ensure its proper functionality. Although this may be a bit cumbersome, it is essential for us to move forward and explore concrete examples.
Setting up Bicep development and deployment environment
The installation process outlined in this section is somewhat opinionated, emphasizing the use of Visual Studio 2022 for writing Bicep files and deploying them with Azure CLI. However, alternative options exist, such as crafting files in a basic editor and deploying them using PowerShell.
Creating Bicep files
Mastering Bicep's syntax entirely may not be feasible, but Intellisense comes to the rescue. In this context, we'll utilize the Bicep extension for Visual Studio 2022, which can be downloaded from here. Once the extension is downloaded, simply install it in the IDE.
Deploying Bicep files
Deploying Bicep files directly from Visual Studio is not currently supported. The deployment of a Bicep file can only be accomplished using Azure CLI or Azure PowerShell, and in this series, we will be opting for Azure CLI.
To install Azure CLI on Windows, follow the instructions provided here.
- After installing Azure CLI, open a command prompt and execute the command 'az bicep version'.
- Run the command 'az bicep install' to resolve the error.
With all the components in place, it's now time to explore tangible examples of Bicep files.