Bicep - Parameters and Variables
Microsoft - Fundamentals of Bicep

Bicep - Parameters and Variables

In my previous article, I covered the creation of a basic Bicep file with the commands that are required and a breakdown of some of the syntax. In this article, I will be covering Parameters and Variables and how to use them.

What is a Parameter?

Parameters in Azure Bicep are used to pass dynamic values into a template to make it flexible and reusable. Parameter declaration may contain default value and/or constraints on an input value. A parameter will always have a type assigned to it. The following types are available:

  • string
  • object
  • array
  • int
  • bool

Define a Parameter

The syntax for declaring a parameter is as follows:

// syntax
param {parameterName} {parameterType}
param {parameterName} {parameterType} = {defaultValue}
// usage
param location string
param location string = 'ukwest1'        

What is a Variable?

Variables in Azure Bicep are used to simplify your Bicep file development. Rather than having repeated expressions or business logic, you can create a variable and then reuse the variable whenever you need to.

Define a Variable

The syntax for declaring a variable is as follows:

// syntax
var {variableName} = {variableValue}
// usage
var location = 'ukwest1'        

You can also define a variable based on other variables or parameters:

param location string = 'ukwest1'
var accountName = 'storageaccount${location}'        

Using Parameters

In this section, I will cover how to use parameters in your Bicep file and how to set them from Azure CLI when deploying. Using the base Bicep file created in my previous article, we are going to add some parameters at the top of the file. Placing all parameters at the top of the file makes it more readable and easier to find. To the top of your main.bicep, please add the following above the resource for storageAccount:

param environmentName string = 'dev'


param locations array = [
? 'westeurope'
? 'eastus2'
? 'eastasia'
]


param supportHttpsTrafficOnly bool


param storageAccountSku object = {
? 'name':'Standard_LRS'
}        

You will notice that majority of the parameters have values assigned and that one does not, this will be important to remember when we go to deploy the Bicep file. Now, to use those parameters, we are going to change up some values in the resource we had defined:

Using '${}' indicates that you are accessing a declared parameter and adding it to the current string. Change the name to:

name: 'myuniquestorageaccount${environmentName}'        

Working with arrays in Bicep is the same as working with it in most programming languages, you provide the name of the array and choose the index using '[]'. Change the location to:

location: locations[0]        

Working with objects is extremely easy, you just need to provide the name of the object followed by a '.' and the name of the value defined. Change the sku object to:

sku: { 
? ? name: storageAccountSku.name
? }        

Below kind, enter a new line and paste:

properties: {
? ? supportsHttpsTrafficOnly: supportHttpsTrafficOnly
? }        

Your resource should now look as follows:

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
? name: 'myuniquestorageaccount${environmentName}'
? location: locations[0]
? sku: {
? ? name: storageAccountSku.name
? }
? kind: 'StorageV2'
? properties: {
? ? supportsHttpsTrafficOnly: supportHttpsTrafficOnly
? }
}        

To deploy the Bicep file with a parameter, use the following command:

az deployment group create --template-file main.bicep --parameters supportHttpsTrafficOnly=false        

If you do not provide the parameter value, the Bicep file will fail to deploy. If you want to override a parameter, you can just extend the deployment command as follows:

az deployment group create --template-file main.bicep --parameters supportHttpsTrafficOnly=false environmentName=prod        

This will override the setting of the environmentName to dev.

Using Variables

To create and use variables is extremely straightforward and I will step you through the process. Underneath your last parameter, on a new line with an empty line above it, create a new variable as follows:

var accessTier = environmentName == 'prod' ? 'Premium' : 'Cool'        

Bicep allows us to use ternary statements to assign values, what we are doing in the line above is checking if environmentName is equal to 'prod' and if it is, setting accessTier to 'Premium' otherwise it will be set to 'Cool'.

We can also create boolean values using similar logic. Add the following directly below the accessTier variable:

var supportHttsOnly = environmentName == 'prod'        

The output of this statement will either be true or false, so we can now remove the parameter for supportHttpsTrafficOnly in its entirety. You will see an error in Bicep, do not worry, we will be fixing that shortly.

Our last variable will be a simplification of how we set the name for our resource:

var storageAccountName = 'storageacc${environmentName}'        

Now we need to modify the resource for storageAccount in the following ways:

  • Replace the value of name with the variable storageAccountName
  • Replace the value of supportsHttpsTrafficOnly in properties with the variable supportHttsOnly
  • Add a new property under properties called accessTier and give it the assignment of variable the accessTier

Your resource should now look as follows:

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
? name: storageAccountName
? location: locations[0]
? sku: {
? ? name: storageAccountSku.name
? }
? kind: 'StorageV2'
? properties: {
? ? supportsHttpsTrafficOnly: supportHttsOnly
? ? accessTier: accessTier
? }
}        

You can now deploy the Bicep file using the following command:

az deployment group create --template-file main.bicep        

OR

az deployment group create --template-file main.bicep --parameters environmentName=prod        

You have now deployed a Bicep file using Variables and Parameters! In my next article, I will cover Decorators and how to use them for validation and enhancing your development experience!

要查看或添加评论,请登录

Declan Taggart的更多文章

  • Bicep - Parameter Decorators

    Bicep - Parameter Decorators

    In my previous article, I covered Parameters and Variables and how to use them. This article is a continuation of my…

  • Create your first Bicep file

    Create your first Bicep file

    What is Bicep? Bicep is a domain-specific language (DSL) that uses a declarative syntax to deploy Azure Resources. In a…

  • Basic unit testing with xUnit

    Basic unit testing with xUnit

    Unit testing has many benefits and will help you identify bugs and problem areas in your code. If we look at the…

  • Dependency injection in Azure Function v3

    Dependency injection in Azure Function v3

    In this article, I am going to show how DI (Dependency Injection) can easily be achieved within an Azure Function…

    2 条评论
  • My basic wireframe for Azure Function v3

    My basic wireframe for Azure Function v3

    In this article, I will show you the wireframe I use for every Azure Function that I create and use. This demo will be…

    2 条评论

社区洞察