Featured image of post Azure Bicep: The Smart Way to Handle Azure Resources

Azure Bicep: The Smart Way to Handle Azure Resources

Curious about Azure Bicep? Dive into this guide to simplify Azure resources management. From hands-on examples to clear comparisons, we've got it all covered. Explore Bicep and make Azure management a breeze! πŸš€!

Azure Bicep: The Smart Way to Handle Azure Resources πŸš€

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.

Azure Bicep to The Rescure

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:

1
2
3
4
5
targetScope = 'subscription'
resource iaMachsResourceGroup 'Microsoft.Resources/resourceGroups@2022-09-01' = {
  name: 'iaMachsRG'
  location: 'australiaeast'
}

And here is the equivalent ARM template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "$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": {}
      }
    ]
}

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.

Bicep VS Code Extenstion

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:

1
2
3
4
5
6
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 πŸ—οΈ

  1. By setting targetScope = 'subscription', you’re telling Azure to place the new resource group within your subscription. This helps keep your resources neatly organized. 😊
  2. resource: This is Bicep’s way of telling Azure that you’re creating a new resource, in this case it’s a Resource Group.
  3. 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.
  4. 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.
  5. 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.

About API Versions πŸ”„

APIs evolve. As time passes, Microsoft rolls out new versions. Each one might introduce new features, better performance, or patch up bugs. So, always keep an eye out for the latest to get the best out of Azure.

Squiggly Line in VS Code

Avoid Hard Coding Values in Your 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:

1
az deployment sub create --location australiaeast --template-file main.bicep

Post-deployment, check the Azure portal to ensure your resources are up and running.

Resource Group on Azure Portal

And there you go! πŸŽ‰ You’re now part of the Azure Bicep community. Keep exploring and happy coding!