Mastering Azure Bicep Parameters

Introduction

Hello everyone! We’ve been on an exciting journey exploring Azure Bicep. We’ve seen what it is, how it compares to ARM templates, set up our environment, and even dived into its syntax. Today, we’re going to take another step forward. We’ll explore one of the key components of Azure Bicep - Parameters. So, let’s get started!

Logging in to Azure

Before we start, let’s ensure we’re logged into Azure. Here’s how you can do it.

  1. Open your terminal or command prompt: You can use any terminal or command prompt that you’re comfortable with.
  2. Log in to Azure: Use the az login command to log in to Azure.
    # Log in to Azure
    
    az login
    
  3. Set your subscription: Use the az account set command to set your subscription.
    # Set your subscription
    az account set --subscription "YourSubscriptionName"
    

Azure Bicep Parameters

Parameters in Azure Bicep are like the inputs to your Bicep file. They allow you to pass values into your Bicep file at deployment time. This makes your Bicep files more flexible and reusable.

You can define a parameter using the param keyword. Here’s an example.

param storageAccountName string = 'mystorageaccount'

In this example, storageAccountName is a parameter of type string. The default value for this parameter is ‘my storage account’.

param storageAccountName string = 'mystorageaccount'

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: storageAccountName
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
}

You can override a parameter's default value at deployment time. Here’s how.

# Deploy the Bicep file with a parameter

az deployment group create --resource-group myResourceGroup \
  --template-file ./main.bicep \
  --parameters storageAccountName=myNewStorageAccount

In this example, we’re deploying the Bicep file and passing a new value for the storageAccountName parameter.

Conclusion

Well done! You’ve just learned how to use parameters in Azure Bicep. Parameters are a powerful feature that makes your Bicep files more flexible and reusable. In our next session, we’ll explore resources in Azure Bicep. So, stay tuned and keep learning!