Azure Bicep
Azure Bicep is the next revision of ARM templates designed to solve some of the issues developers were facing when deploying their resources to Azure. It’s an Open Source tool and, in fact is a domain-specific language (DSL) that provides a means to declarative codify infrastructure, which describes the topology of cloud resources such as VMs, Web Apps, and networking interfaces. It also encourages code reuse and modularity in designing the infrastructure as code files.
Install Bicep
Installing the Vs Code Extension.
To install Bicep VS Code extension you will need to open VS Code, Select Extensions and search for Bicep. Once installed you will be able to write .bicep files in VS code. the below image shows that I have already installed this extension.
To verify you’ve it installed, create a file with the .bicep extension and watch the language mode change in the lower right corner of VS Code.
Installing Bicep CLI
You need to have Azure CLI version 2.20.0 or later installed to able to install the Bicep CLI. Issue the below command to install Bicep.
1 |
az bicep install |
Create a Bicep Template
in this example we will create a Bicep template that will create a storage account. In Vs Code create a new file called main.bicep. Start typing storage and a menu should appear and then select res-storage. You should end up with a snippet that looks like:
1 2 3 4 5 6 7 8 |
resource storageaccount 'Microsoft.Storage/storageAccounts@2021-02-01' = { name: 'name' location: location kind: 'StorageV2' sku: { name: 'Premium_LRS' } } |
This file will deploy an Azure Storage Account, however, we need to modify the file to make it ready for deployment. First let’s add two parameters, one for the name since it should be unique, and one for the location.
1 2 |
param storageName string = 'stg${uniqueString(resourceGroup().id)}' param location string = resourceGroup().location |
when you assign a value to a parameter you are making that parameter optional. Now replace the name value with the parameter value as shown below:
1 2 3 4 5 6 7 8 9 10 11 |
param storageName string = 'stg${uniqueString(resourceGroup().id)}' param location string = resourceGroup().location resource storageaccount 'Microsoft.Storage/storageAccounts@2021-02-01' = { name: storageName location: location kind: 'StorageV2' sku: { name: 'Premium_LRS' } } |
Visualize resources
You can use VS Code to visualize the resources defined in your Bicep file. Click on the visualizer button at the top right-hand corner:
Compile the File
although you don’t need to compile the file in order to deploy it, it’s worth knowing how to do this. Open the VS Code integrated terminal and type the following in:
1 |
az bicep build -f ./main.bicep |
this will create a an ARM template, compare the two and look at how concise the Bicep version is.
Deploying the Bicep file
Now is the time to deploy the Bicep file you’ve created. In the same terminal run the following commands: replace the uniuqeName with a name of your choice
1 2 |
az group create --name Bicep --location uksouth az deployment group create --resource-group Bicep --template-file main.bicep --parameters storageName=uniqueName |
When the deployment finishes, you’ll be getting a message indicating the deployment succeeded.
More Information