How To Use Variables in a Bicep Tempate

In this article, we will show you how to use variables in a Bicep template. Variables are a great way to keep your templates organized and easy to edit.

They allow you to insert specific information into your templates, such as the name of your resources and more. Let’s get started!

Declare a Variable

To declare a variable in a Bicep template, we are using the var keyword, the variable’s name, and the value of the variable.

In the example below, I’m declaring a variable called storageaccount with the name ntweeklystr0001

var storageaccountname = 'ntweeklystr0001'

After declaring the variable in the template, I can start using it and reference it. You can see the end-to-end code in action in the template below. The first line declares the variable, and it is used in line number three.

Template

var storageaccountname  = 'ntweeklystr0001'
resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: storageaccountname
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
  }
}

Use Parameters

We can also use parameters with Bicep using the following example. When using parameters, we need to define the type (string, int or bool) and set a default value (optional)

perm storageaccountname string = 'ntweeklystr0001'

Parameters are excellent when passing values to a Bicep template or using a template file.

About Bicep

The Resource Manager template language Azure Bicep enables you to deploy Azure resources declaratively. It’s a domain-specific language, which implies it was created specifically to deploy Azure resources. Bicep is only used to create Resource Manager templates.

Bicep is designed to be simple to understand and learn, regardless of your prior experience with other programming languages. Bicep templates can use all resource types, API versions, and property values.

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.