Bicep Functions
Bicep Functions

Azure Bicep – A brief overview of Functions

In past few articles, we have been discussing about Azure Bicep. We have seen how to create new resources or refer existing resources in bicep file. We also have seen few things about passing parameters to the bicep and how to reuse existing bicep modules.

The bicep file can also use some functions. In this short article, we are going to have a look at some basics about bicep functions.

Namespace

Functions can be from one of the two namespaces – az or sys. Generally, when we want to call any function, we do not need to specify the namespace. It is automatically resolved by bicep. But sometimes the symbolic name of a resource may conflict with a function name. In such cases, we may need to explicitly specify the namespace.

For example, if we declare a parameter with name range in a bicep file and if we want to use a range function in the same file, we will have to specify the namespace while calling the function.

The az namespace contains the functions which are related to deployment on Azure. The sys namespace contains the functions to construct values. It also contains decorators.

We are going to have a brief overview of some functions.

Some commonly used functions

There are various functions – some functions can be used to manipulate the date values & some other can be used to compute numeric values. There are also some functions to compute and modify string values.

Array Functions

These are set of functions which can be used with arrays. These are part of sys namespace. Some common functions are :

  • contains – to check if array or string contains item to find
  • empty – to check if array or string is empty
  • length – to get the length of array
  • min – to get minimum value from array
  • max – to get maximum value from array
  • rangerange(startIndex, count) is to create array containing provided number of elements (2nd parameter) starting from index specified at first parameter
  • skipskip(originalValue, numberToSkip) is to skip the number of elements from array specified in the first parameter
  • taketake(originalValue, numberToTake) is to take the number of elements specified from array specified as first parameter

There are also some other functions which can be used with arrays.

Date Functions

These are functions from sys namespace and these can be used to work with dates.

  • dateTimeAdddateTimeAdd(base, duration, [format]) is to add a time duration to a base date value. the third parameter is optional.
  • dateTimeFromEpochdateTimeFromEpoch(epochTime) converts an epoch value to datetime.
  • dateTimeToEpochdateTimeToEpoch(dateTime) converts a datetime value to epoch value
  • utcNow – provides current UTC time

Lambda Functions

These are also from sys namespace and these can be used while working with lambda expressions.

  • filter – to filter an array
  • map – applies a custom mapping function to each element in the array
  • reducereduce(inputArray, initialValue, lambda expression) to reduce array with a custom expression
  • sortsort(inputArray, lambda expression) Sorts an array with a custom sort function

Leave a ReplyCancel reply