Getting Started with Azure Bicep
Tags: azure, bicep, iac
Published: 2022-03-30 14:28:04

I've been spending a lot of time recently digging into Azure Bicep and learning how to deploy Azure resources using this new domain specific language. 

## What is Azure Bicep?

Azure Bicep is a declarative language that will help you deploy your Azure resources.  I've also heard Azure Bicep described as a Infrastructure as Code (IaC) tool. 

Azure Bicep is an evolution from Azure Resource Manager (ARM) templates that we've had for many years.  You don't need any prior knowledge of ARM templates to get started with Azure Bicep. 

## What do you need to get started with Azure Bicep?

You'll need Visual Studio Code, the Bicep extension and Azure CLI or Azure PowerShell, all installed locally.   These tools will help you write, visualise and deploy your resources via an Azure Bicep template. 

## Azure Bicep Templates

Azure Bicep templates have the file extension .bicep  When you use Visual Studio Code and the Bicep extension you'll be given help and warned about syntax errors or given help to author the template.  

There are key areas to an Azure Bicep template, we're going to take a simple Azure Virtual Network template as an example to work on and deploy today. 

```bicep
param location string = resourceGroup().location

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2021-05-01' = {
  name: 'sarahs-network'
  location: location
  tags: {
    Owner: 'Sarah'
    Purpose: 'Tutorial'
  }
  properties: {
    addressSpace: {
      addressPrefixes: [
        '20.0.0.0/16'
      ]
    }
    subnets: [
      {
        name: 'servers'
        properties: {
          addressPrefix: '20.0.0.0/24'
        }
      }
      {
        name: 'desktops'
        properties: {
          addressPrefix: '20.0.1.0/24'
        }
      }
      {
        name: 'resources'
        properties: {
          addressPrefix: '20.0.2.0/24'
        }
      }
    ]
  }
}

```

Let's look closely at some key parts of this resource definition:

- The first line is a defining the parameter for our resources' location.  We've set it so the resource is deployed in the same Azure location as our resource group. 

- The **resource ** keyword at the start tells Bicep that you're defining a resource. 

- The next part is a _symbolic name_. In the example, the resource's symbolic name is **virtualNetwork**. Symbolic names are used within Bicep to refer to the resource. 

- We then have the resource type and API version of the resource - **Microsoft.Network/virtualNetworks@2021-05-01**.  This tells Bicep we're declaring a Virtual Network and then which version of the API to use. 

- We've then declared the resource name in the second line, we're calling this Virtual Network _"Sarahs-Network"_.

- We then set information like location, tags for the resource. 

- We then start to declare information about the Virtual Network and the subnets we want to be deployed. 

We've kept the template simple in this example, but you could declare things like DHCP options, DDOS Protection, BGP, encryption and much more.  To find out more about what you can declare for a Virtual Network [check out the documentation here](https://docs.microsoft.com/azure/templates/microsoft.network/virtualnetworks?tabs=bicep&WT.mc_id=AZ-MVP-5004737). 

## Visualise your Azure Bicep template

As part of the [Azure Bicep extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep) you have the ability to visualise your template. You can press CTRL + K V on a Windows machine or you can select the visualise button in the right hand corner. 

![Azure Bicep visualiser button inside Visual Studio Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4gm0xcamdbc5rs753x9.png)
 
Because we have a simple template the visualiser doesn't add much value however, if we visualise a a Bicep template with nested templates then we can see the power of the visualiser. 

![Azure Bicep templates visualised within Visual Studio Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1mfgw6hs8wrz9vwv04a.png)
 

## Deploy an Azure Template using Azure CLI

The first thing we need to do before deploying our template, is make sure we are logged into Azure CLI on our local machine.  We type in the following command that will log us in to our relevant Azure subscription. 

```bash
az login
```

Now we need to create an Azure Resource Group for our resources to reside in.  We'll use a Azure CLI command to do that. 

```bash
az group create -l westus -n vnet-tutorial --tags Owner=Sarah Purpose=Tutorial
```

Now to deploy our template: 

```bash
az deployment group create --resource-group vnet-tutorial --template-file vnet.bicep
```

In the command we are saying deploy to our **vnet-tutorial** resource group and we're telling the command to deploy our **vnet.bicep** template. 

The command will start to run and after a few minute you should have your Virtual Network deployed. 

![Deployed resource within Azure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddlwwctvaprmcybwd24f.png)
 
## Is Azure Bicep production ready?

Yes, as of [version 0.3 Azure Bicep](https://github.com/Azure/bicep/releases) is production ready.  It is supported by Microsoft Support Plans and Bicep has 100% parity with what can be accomplished by ARM template. 

I hope that helps you get started and explains a bit more about Azure Bicep.  Do check out the [Microsoft Learn module](https://docs.microsoft.com/learn/paths/fundamentals-bicep/?WT.mc_id=AZ-MVP-5004737) or my video explaining why Azure Bicep has become so popular.

{% youtube skptmDnvgwQ %}