top of page

LetsDevOps: Azure Bicep -> Data types, Parameters and Variables, How to use in bicep template.

Writer's picture: Sumit RajSumit Raj

Introduction

In this blog we will learn data types, parameters and variables and how to use them effectively in bicep template.


  • Data Types

  • Parameters

  • variables



Data Types



Data Types Syntax


Arrays

Define under [ ]

param numberArray array = [1, 2, 3]

boolean

param issetting bool = true

int

param vmcount int = 1

string

param staccountname string = 'demotest'

object

Define under { }

param mixedObject object = {name: 'test name', id: '123-abc'}

output accessorResult string = environmentSettings['dev'].name

Secure String/Object

Value of the parameter isn't saved to the deployment history and isn't logged.

@secure()
param password string 

Parameters

Parameters helps to provide information to a Bicep template at deployment time. It also helps in making the flexible and reusable bicep template.


Declare Parameter

param <parameter-name> <parameter-data-type>

param staccountname string

Add a default value

param staccountname string = 'st-demo0103'

Parameters Type Format


Here is the example for all data types in while defining parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  
  "parameters": {
    "exampleString": {
      "value": "test string"
    },
    "exampleInt": {
      "value": 4
    },
    "exampleBool": {
      "value": true
    },
    "exampleArray": {
      "value": [
        "value 1",
        "value 2"
      ]
    },
    "exampleObject": {
      "value": {
        "property1": "value1",
        "property2": "value2"
      }
    }
  }
}


Decorators

@description('Must be at least 3 Characeters.')
@minLength(3) 
@maxLength(24)
param storageAccountName string

@allowed([   
'dev'
'test'
'prod'
])
param environment string


Variables

You use variables to simplify your Bicep file development.


A variable can't have the same name as a parameter, module, or resource.


Define variable


var <variable-name> = <variable-value>

Use variable

param rgLocation string
param storageNamePrefix string = 'STG'

var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().id)}'

UseCase

Create storage account with parameter/variables.



Demo




Comments


bottom of page