Ever struggled with Azure Resource Manager (ARM) templates? These JSON files are essential for setting up your resources on Azure. Powerful? Absolutely. But letโs be honest, they can be a bit tough to read and manage. ๐ค
Azure Bicep to the rescue! ๐ Created by Microsoft, this domain-specific language (DSL) refines ARM templates to be more user-friendly. Hereโs our agenda for today:
- A brief intro to Azure Bicep.
- Bicep vs. ARM templates: Spotting the differences.
- Advantages of Azure Bicep.
- A hands-on guide: Deploying an Azure resource with Bicep.
Why Should You Care About Azure Bicep? ๐คทโโ๏ธ
Designed for Azure: Azure Bicepโs sole purpose is to describe and deploy Azure resources, being a DSL it is tightly integrated with Azure. As Azure introduces new features, you can immediately start to use it in your Bicep files without waiting for updates.
Say Goodbye to Complex JSON: Azure Bicep does away with the intricacies of JSON, allowing you to state your requirements more directly.
Enhanced Developer Experience: Azure Bicep is rich in features like intellisense, code completion, and validation, making coding and debugging smoother.
Understanding Azure Bicep ๐โโ๏ธ
Azure Bicep isnโt here to replace ARM templates. Instead, think of it as a friendly interface layered on top of them. While both serve the same purpose, their syntax differs notably. For instance, here is how you can define a resource group in Azure Bicep:
targetScope = 'subscription' resource iaMachsResourceGroup 'Microsoft.Resources/resourceGroups@2022-09-01' = { name: 'iaMachsRG' location: 'australiaeast' }
And here is the equivalent ARM template:
<code>{ "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ { "type": "Microsoft.Resources/resourceGroups", "apiVersion": "2018-05-01", "location": "australiaeast", "name": "iaMachsRG", "properties": {} } ] }</code>
Notice the simplicity? Azure Bicep is concise and feels more intuitive. No more wrestling with curly braces, quotation marks, or schema references. Itโs all straightforward.
Advantages of Azure Bicep ๐
- Readability: Itโs easier on the eyes compared to JSON.
- Dev-friendly Features: Azure CLI, Bicep CLI, and even a VS Code extension โ Bicep has you covered.
- Perfect Sync with ARM: It integrates perfectly with existing ARM templates.
Setting Up Azure Bicep
Ready to get started? Hereโs what youโll need:
Azure CLI: This handy command-line tool lets you talk to Azure from any platform. Bicep CLI: Your go-to for all things Bicep. VS Code Extension: An extension to enhance your Bicep coding experience in VS Code.
Installation steps:
- Follow the Azure CLI installation guide here โ.
- UsInstall the Bicep CLI using
az bicep install
. - Search for โBicepโ in the VS Code Extensions marketplace.
If youโve not use Azure CLI before check out my YouTube video below: ๐
Creating Your First Azure Bicep Script ๐
Time for action! Letโs create a Bicep script for an Azure resource group. Open VS Code, create a new file with the .bicep extension. You can name it anything you want, but for this example, weโll name it main.bicep
.
In the main.bicep file, write the following code:
targetScope = 'subscription' // Define a resource for the resource group resource iaMachsResourceGroup 'Microsoft.Resources/resourceGroups@2022-09-01' = { name: 'iaMachsRG' location: 'australiaeast' }
Decoding the Bicep Code: ๐
Letโs take a moment to demystify whatโs going on in the Bicep code youโve just created.
Key Terms at a Glance ๐๏ธ
- By setting
targetScope = 'subscription'
, youโre telling Azure to place the new resource group within your subscription. This helps keep your resources neatly organized. ๐ - resource: This is Bicepโs way of telling Azure that youโre creating a new resource, in this case itโs a Resource Group.
- iaMachsResourceGroup: Think of this as a friendly nickname for your resource within the Bicep file. Itโs like saving a phone number in your contacts list under a name like โJohnโ. When you call โJohnโ you donโt dial his name on your phone, you dial his actual phone number. But in your contacts, you refer to him by this name because itโs easier to remember and understand. Similarly,
iaMachsResourceGroup
is the easy-to-remember name you use in your code. But remember, itโs not the name Azure uses to identify the resource group. - iaMachsRG: Now this is the real deal. This is the actual name Azure will use to identify your resource groupโkind of like Johnโs actual phone number.
- Microsoft.Resources/resourceGroups@2022-09-01: This term has two parts:
- Resource Type:
Microsoft.Resources/resourceGroups
indicates that weโre setting up a resource group. - API Version:
2022-09-01
tells Azure which version of its Resource Manager (ARM) API to use. Itโs essential because each API version offers different features.
- Resource Type:
About API Versions ๐
Squiggly Line in VS Code
Weโre using โaustraliaeastโ as a hardcoded value for simplicity while learning Bicep. Itโs not ideal due to its impact on your code reusability and flexibility. Donโt worry about the squiggly line for now – weโll address this in a future blog post. Stay tuned! ๐
Deploy Time! ๐
Ready to bring your Bicep file to life in Azure?
Using Azure CLI:
Navigate to your Bicep fileโs location and run:
az deployment sub create --location australiaeast --template-file main.bicep
Copy
Post-deployment, check the Azure portal to ensure your resources are up and running.
And there you go! ๐ Youโre now part of the Azure Bicep community. Keep exploring and happy coding!
About the Author:
Ahmed Muhi is an Auckland-based cloud consultant with a deep-rooted background as a network architect. Guiding Clients in their Cloud Journey to both AWS and Azure. Specializing in Azure solutions, Ahmedโs professional journey of over 15 years is fuelled by a relentless passion for learning new skills and technologies, particularly around Cloud-Native, Networking, and Security technologies within the Azure platformโ๏ธ.
When heโs not guiding clients through the intricacies of the Cloud, Ahmed loves to create content. His blog and YouTube channel (https://www.youtube.com/@iamachs/) ๐บ๐ serve as platforms where he shares his knowledge and experiences. Ahmed volunteers his time to running the Aotearoa Azure Meetup, and he also co-hosts โAzure All Stars,โ a podcast aimed at illuminating the efforts of those in the Azure world and fostering inspiration among newcomers. ๐๏ธ๐
Apart from being a cloud enthusiast, Ahmed loves to clear his mind with a good run ๐โโ๏ธ
Reference:
Muhi, A. (2024). Azure Bicep: The Smart Way to Handle Azure Resources. Available at: https://www.iamachs.com/p/azure-bicep-the-smart-way-to-handle-azure-resources/ [Accessed: 4th April 2024].