Getting Started With Bicep: Compiling, Deploying, and Decompiling
Table of Contents
In the first post of the ‘Getting Started with Bicep’ series we learned what is Azure Bicep and how to install it and begin working with it. Next, we learned how to create our first Bicep template and user parameters and variables and the different data types there are. Now, in this section we will learn how to compile our Bicep templates and deploy them to Azure.
Compile Bicep to ARM
The first item we need to do is to compile or transpile (Transpiling is a specific term for taking source code written in one language and transforming into another language that has a similar level of abstraction.) our Bicep code to ARM (JSON) code. Then, once compiled, the resulting ARM (JSON) template will be deployed to Microsoft Azure.
From our previous post on ‘Getting Started with Bicep: Building your First Bicep Template‘ we created a basic Bicep template that would deploy a Storage Account in Azure. Now, we will use that template and deploy it to our Azure Environment. The template (below) users one parameter and one variable.
param storageAccountName string = 'bicepstorage630618' var location = 'northcentralus' resource stgact 'Microsoft.Storage/storageAccounts@2021-06-01' = { name: storageAccountName // Globally unique storage account name location: location // Azure Region kind: 'Storage' sku: { name: 'Standard_LRS' //SKU } }
Each .bicep
file is compiled into a single ARM Template JSON file to deploy to Azure. To compile or transpile our Bicep template to ARM JSON, we need to run bicep.exe followed by the build parameter and then give it the bicep template file. In my example, I have saved the above template as storageAccount.bicep, and will be compiling it using the following command
bicep build ./storageAccount.bicep
I can now parse that directory and see I have a newly created .json file. Viewing the contents of it I can see that my easy to read and understand .bicep template has been compiled to an Azure ARM JSON template. I have gone from 13 lines of Bicep code to 32 lines of ARM JSON code. This is one of the big reasons Microsoft created Bicep on top of ARM.
Deploy to Azure
Now that we have our JSON template, we can use it to deploy our Azure Infrastructure. This can be done using the Azure CLI, Azure PowerShell, or in the Azure Portal itself.
Azure CLI
First, we need to connect to the Azure CLI by running az login.
Next, we must create a resource group to deploy our resources to. Usually you would have this in the same bicep template and deploy them together, but to keep things simple we create a basic bicep template that would deploy a single resource.
Using the az command line we can create a new resource group
az group create -n rg-bicep -l northcentralus
I can view the Azure portal and see that my newly created Resource Group has been created within my tenant.
Now that I have my Resource Group create, I am ready to deploy my template file to Azure. Using the Azure CLI I can deploy the template using the following code:
az deployment group create -f storageAccount.json -g rg-bicep
NOTE: You do not actually need to transpile to ARM from Bicep, you can feed it the .bicep file directly as shown below
Bouncing back to the Azure Portal I can now see my Resource Group has a newly created Storage Account in it!
Azure PowerShell
Another way to deploy our ARM template is by using Azure PowerShell.
First, we must log into Azure by running the following command:
Connect-AzAccount
Now that we are connected to our Azure Tenant, we need to create a Resource Group to deploy our resources that are define in the Bicep/ARM template to. Usually you would have this in the same bicep template and deploy them together, but to keep things simple we create a basic bicep template that would deploy a single resource.
New-AzResourceGroup -Name rg-bicep -Location northcentralus
Now that we have our Resource Group created, we can continue deploying our template.
New-AzResourceGroupDeployment -TemplateFile ./storageAccount.json -ResourceGroupName rg-bicep
NOTE: You can actually feed the .bicep config file directly instead of compiling to ARM and then deploying the ARM JSON file by feeding it the .bicep file instead of the .json
Back in the Azure Portal I can now see my Resource Group has a newly created Storage Account in it!
Azure Portal
Navigate to the Azure portal at portal.azure.com and select All Resources. Find Templates and click on it and select ‘+ Create‘
Give you template a proper name and description so you understand later from a glance what the template is doing and deploying. When you have finished click Next: ARM Template
Paste your JSON ARM template, replacing anything that was here by default.
Click Add when finished. Click your template and click Deploy. It will bring you to the deployment screen which will read your template and display deployment options. Below we can see that it wants me to select or create a new Resource Group (because I am not specifying one in the template file itself).
Once you have satisfied all of the prerequisites click Purchase
Clicking on the notification when you begin the deployment will allow you to view the status of your deployment. Here I can see that everything was successfully deployed.
What-If Statement
The What-If statement (which is designated by ‘-c’ will evaluate your template and see what will be changing in your environment. This is a good way to verify that your template is doing what you expect it to be doing and where you expect it to be going. In the example below, we can see that my template will be creating something.
Decompiling
Bicep allows you to decompile ARM JSON templates to .Bicep templates. The decompiling process is a best-effort process. To decompile and ARM JSON template you need to specify the decompile parameter. The following example decompiles my storageAccount.JSON template
bicep decompile storageAccount.json
Viewing the newly created decompiled bicep file, I can see the following bicep code.
My name is Bradley Wyatt; I am a 5x Microsoft Most Valuable Professional (MVP) in Microsoft Azure and Microsoft 365. I have given talks at many different conferences, user groups, and companies throughout the United States, ranging from PowerShell to DevOps Security best practices, and I am the 2022 North American Outstanding Contribution to the Microsoft Community winner.