Use Bicep to Compile or Decompile ARM Templates

Published On: July 17, 2022Last Updated: January 8, 2024By Tags: , 2.7 min readViews: 314

TOC

What is Bicep?

Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources. In a Bicep file, you define the infrastructure you want to deploy to Azure, and then use that file throughout the development lifecycle to repeatedly deploy your infrastructure. Your resources are deployed in a consistent manner.

Microsoft

Why use Bicep over generic ARM Templates?

Although generic JSON ARM Templates are still needed for Azure Resource Manager, using Bicep makes creating ARM templates more consumable by the masses as it uses very simple syntax compared to its big sister, see the example in the Benefits of Bicep section of the documentation for examples.


Getting started with Bicep

Let’s assume that you already have an IaC (Infrastructure as Code) strategy, and you are already using ARM Templates for deployment. Sometimes it can be a struggle for other colleagues/teams to reverse engineer your complex templates. So you decide to take a look at Bicep to help make them more consumable to your colleagues and team members.

Well, Microsoft and the Bicep team could not have made this simpler to achieve!. Using the Bicep command line tools, you can easily decompile (convert) your standard json template to Bicep… and the best bit, your json template file (if used) still works seamlessly.

Pre-requisites

Info

Please visit the Install Bicep tools page to install the Bicep tools.

Decompile an ARM Template

This really couldn’t be any simpler, but just for the sake of completeness before you see the warning anyway, the decompilation of the templates is on a best efforts basis, if your template does not decompile then you can raise an issue on the link provided in your shell.

  1. Open VSCode to the directory where your templates are stored
  2. Type bicep decompile <filename>
  3. Review the output of the file

The output file will be located in the same directory as the original file, but with a bicep extension. You can specify the output directory (which will need to exist first) by using the –outdir parameter, or you can specify the file name but using the –outfile parameter.

Tip

You can not specify both –outdir and –outfile together.

decompile
decompile

Compile and ARM Template from Bicep

Again, this is relatively straightforward. My biggest use case for this at the moment is when I want to publish a Deploy to Azure Button as this still requires a JSON template to be passed in.

  1. Open VSCode to the directory where your templates are stored
  2. Type bicep build <filename>
  3. Review the output of the file

The output file will be located in the same directory as the original file, but with a JSON extension. You can specify the output directory (which will need to exist first) by using the –outdir parameter, or you can specify the file name but using the –outfile parameter.

compile
compile

Conclusion

I enjoyed learning about Bicep over the course of a weekend, and going forward it will be my chosen language to be building template files for Azure as its so much easier than ARM templates for others to read.

Why not start your journey using some of the resources below.

Watch out for more Bicep and ARM posts coming soon.

Resources

Use Bicep to Compile or Decompile ARM Templates

Published On: July 17, 2022Last Updated: January 8, 2024By Tags: , 2.7 min readViews: 314

TOC

What is Bicep?

Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources. In a Bicep file, you define the infrastructure you want to deploy to Azure, and then use that file throughout the development lifecycle to repeatedly deploy your infrastructure. Your resources are deployed in a consistent manner.

Microsoft

Why use Bicep over generic ARM Templates?

Although generic JSON ARM Templates are still needed for Azure Resource Manager, using Bicep makes creating ARM templates more consumable by the masses as it uses very simple syntax compared to its big sister, see the example in the Benefits of Bicep section of the documentation for examples.


Getting started with Bicep

Let’s assume that you already have an IaC (Infrastructure as Code) strategy, and you are already using ARM Templates for deployment. Sometimes it can be a struggle for other colleagues/teams to reverse engineer your complex templates. So you decide to take a look at Bicep to help make them more consumable to your colleagues and team members.

Well, Microsoft and the Bicep team could not have made this simpler to achieve!. Using the Bicep command line tools, you can easily decompile (convert) your standard json template to Bicep… and the best bit, your json template file (if used) still works seamlessly.

Pre-requisites

Info

Please visit the Install Bicep tools page to install the Bicep tools.

Decompile an ARM Template

This really couldn’t be any simpler, but just for the sake of completeness before you see the warning anyway, the decompilation of the templates is on a best efforts basis, if your template does not decompile then you can raise an issue on the link provided in your shell.

  1. Open VSCode to the directory where your templates are stored
  2. Type bicep decompile <filename>
  3. Review the output of the file

The output file will be located in the same directory as the original file, but with a bicep extension. You can specify the output directory (which will need to exist first) by using the –outdir parameter, or you can specify the file name but using the –outfile parameter.

Tip

You can not specify both –outdir and –outfile together.

decompile
decompile

Compile and ARM Template from Bicep

Again, this is relatively straightforward. My biggest use case for this at the moment is when I want to publish a Deploy to Azure Button as this still requires a JSON template to be passed in.

  1. Open VSCode to the directory where your templates are stored
  2. Type bicep build <filename>
  3. Review the output of the file

The output file will be located in the same directory as the original file, but with a JSON extension. You can specify the output directory (which will need to exist first) by using the –outdir parameter, or you can specify the file name but using the –outfile parameter.

compile
compile

Conclusion

I enjoyed learning about Bicep over the course of a weekend, and going forward it will be my chosen language to be building template files for Azure as its so much easier than ARM templates for others to read.

Why not start your journey using some of the resources below.

Watch out for more Bicep and ARM posts coming soon.

Resources