I’ve been developing Azure Resource Manager (ARM) templates for many years. I love how ARM contributes to automation and to repetitive and standardized Azure deployments across different Azure Subscriptions. Whether your favourite approach is ARM, REST Api, CLI, PowerShell or anything else, automation and infrastructure as code are key to become successful with Azure.
But did you know the main driver behind automation is actually the Dutch Stroopwafel? 🙂 To learn why, check out the recording of a session I presented back in 2017 at the E2E conference in Orlando, Florida!
There is no doubt that ARM is extremely powerful, GitHub is filled with thousands and thousands of samples you can reuse and also contribute to. Despite the power of ARM, the complexity of writing JSON code from scratch can be very challenging.
Yes, JSON is based on a declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it. But, you have to build up quite some experience to become a seasoned ARM Template developer. A big part of the complexity is introduced by all of the colons, brackets, double quotes, parenthesis et cetera you have to use. I always advise people who are new to ARM Templates to start by deploying objects in the Azure Portal, grabbing the corresponding ARM code from those deployments and reverse engineering that ARM code. And secondly, browse and search on GitHub to learn from what others created.
Earlier this week I noticed the tweet below by Mark Russinovich where he referred to Project Bicep, to simplify Azure Declarative modelling!
So what is Project Bicep in more detail? The description below is coming from the public Github location (aka.ms/bicep)
“..Bicep is a Domain Specific Language (DSL) for deploying Azure resources declaratively. It aims to drastically simplify the authoring experience with a cleaner syntax and better support for modularity and code re-use. Bicep is a transparent abstraction over ARM and ARM templates, which means anything that can be done in an ARM Template can be done in bicep. All resource types, apiVersions, and properties that are valid in an ARM template are equally valid in Bicep on day one. Bicep compiles down to standard ARM Template JSON files, which means the ARM JSON is effectively being treated as an Intermediate Language (IL)…”
The description grabbed my attention so I decided to dive in and get my hands dirty! Since a specific use case always helps understanding a new technology, I based my testing on existing ARM code to deploy Windows Virtual Desktop to see what Bicep would bring to the table.
The diagram below explains where to position Project Bicep.
Setting up your VS Code to work with Bicep is super simple, you install the required CLI and optionally install the VS Code Extension. I can really encourage to do the latter as well because it provides you authoring support, intellisense and validation. Here is a link to the setup steps.
Once you’ve followed the steps referenced above, you’re ready to start authoring your first .bicep file. Building a .json file from a .bicep file is as simple as running the command below.
I this case I used an empty .bicep source file because that gives you get a good idea of what happens. Below is the end result after the build, an empty .json template with the various ARM Template sections we’re familiar with.
Lets now dig in and create a .bicep file that builds us an ARM Template to deploy WVD backplane components (workspaces, hostpool and appgroup).
Let’s start by defining parameters and variables. Below is what this looks like in .bicep format.
As you can see the syntax is super simple! There is no need to worry about colons, brackets, double quotes, parenthesis et cetera. If we build this part of the code it already becomes very clear how powerful Bicep is! Below is the corresponding .json output that the Bicep build generates.
We just simplified our infrastructure as code from 28 lines to 8 lines of code, and from 714 characters to 307!
Not only did we trim down the number of lines of code, bicep is already much more readable! And this is only the beginning!
Lets take a look at the .bicep code to create a WVD App Group. Below is the code that is needed. Again, notice how extremely simple and readable the code is!
Now compare that to the ARM code that is generated by the Bicep build process!
Take special notice to the way we can reference a resource ID of another object. Simplicity to the max!
Within VS code we’re also treated with tab completion options as per example below which make authoring Bicep files even easier.
Bicep also comes with implicit dependencies. If you are familiar with creating multiple objects using a single ARM Template, you’ll know that you need to explicitly tell ARM which objects depend on which other objects. This ensures that ARM created the various Azure objects in the correct order. Bicep has implicit dependencies! In this case I’m referencing the resource id of the host pool objects.
The Bicep build not only takes away the complexity of such a reference, it also automatically generates the required DependsOn section!
For my first test I created a .Bicep file that generates the ARM code to create a WVD Hostpool, AppGroup and Workspace and creates the necessary connections between the objects as well. The end result?
41 lines (1223 characters) in .bicep code that generate the equivalent in .json code containing of 75 lines (2432 characters)!
Here is the end result after deploying in the Azure Portal.
And according to ARM, that only took 2 seconds to deploy! 🙂
To share what I have built and to you help you jumpstart your .bicep projects, I have published the full .bicep file on my GitHub:
Finally, I advise to keep watching aka.ms/Bicep because with the current preview release (I would not advise to use for product deployments yet) there are a few limitations as shown below. One of the limitations I ran into is the lack of support for arrays as of now. This is why I was not able to link the WVD App Group with the WVD Workspace yet.
I’m super excited and I think this is a great move by Microsoft! It really provides even more power to ARM! I’m looking forward to new improvements and I’m sure Bicep will also be covered during the upcoming Ignite 2020 event!