Logo

dev-resources.site

for different kinds of informations.

User-defined function in Azure Bicep

Published at
4/4/2024
Categories
azure
iac
bicep
cloud
Author
omiossec
Categories
4 categories in total
azure
open
iac
open
bicep
open
cloud
open
Author
8 person written this
omiossec
open
User-defined function in Azure Bicep

Bicep allows you to define your functions for your bicep deployment. These functions can ease standardization expressions frequently used or perform some calculations. Think about naming conventions where you need to concatenate several strings to form a name.
Bicep user-defined functions are similar to user-defined functions in ARM templates but far more simpler.
To declare a function, you simply need to use the β€œfunc” statement followed by a list of parameters (if any) with their type, the data type returned by the function, and an expression, the corpus of the function.
Func (param1, param1Type, param2, param2Type, …) functionType => Function Expression
Just like in the ARM template function, the expression can only use the function parameters, it can't access any variable or parameter present in the bicep project.
A simple example to understand.

func sayMyName(firstName string, lastName string) string => '${firstName} ${lastName}'

output myName string = sayMyName('Olivier', 'Miossec')
Enter fullscreen mode Exit fullscreen mode

The function sayMyName takes two parameters, firstName and lastName both using the string type. The function returns a string, which is the concatenation of the two parameters separated by a space.
As you see user user-defined function is Bicep can help to enforce naming conventions. Imagine you want to ensure that a resource name is defined by the name of the application, an increment, and the resource type.

func resourceName (appName string, increment int, resourceType string) string => toLower('${appName}-${increment}-${resourceType}')

output name string = resourceName('app',2, 'vnic')
Enter fullscreen mode Exit fullscreen mode

The function creates a name by concatenating parameters with β€˜-β€˜

User-defined functions can't use variables and cannot use the reference function to retrieve.
They can only use one single expression, so it can be challenging to create a complex calculation. But you overcome this limitation by creating several user-defined functions, as you can call any other functions inside your functions.

func returnParam (myParam string) string  => '${myParam}'

func addPrefix (prefix string, appName string) string => concat(prefix, '-', returnParam(appName))

output name string = addPrefix('vm', 'web')
Enter fullscreen mode Exit fullscreen mode

You can’t surcharge a user-defined function by changing the type of a parameter or the number of parameters. You will get an error; Bicep will only try to evaluate the first expression. So, if you have an operation that can use several types you will need to use several functions.

bicep Article's
30 articles in total
Favicon
Deploying and Configuring a Hybrid Identity Lab Using Bicep - Part 1: Active Directory Setup and Sync
Favicon
How to setup an Azure Machine Learning Workspace securelyπŸ›‘οΈπŸ”’πŸ”‘
Favicon
Creating a Custom Role for Secure Bicep Deployments in Azure
Favicon
Create a GitHub pipeline to test, review, and deploy a Bicep template.
Favicon
A KeyVault for the Power Platform
Favicon
Kickstart projects with azd Templates
Favicon
Conditional deployment in Azure Bicep
Favicon
Rush configuration
Favicon
user-defined type in Azure Bicep, an introduction
Favicon
Set version numbers in Bicep templates
Favicon
Securing your Azure deployments with PSRule
Favicon
Versioned Bicep templates- Deployment
Favicon
Change Management in Infrastructure as a Code (IaC)
Favicon
Azure Verified Modules: Consolidated Standards for a Good IaC
Favicon
Getting Started with Azure Bicep
Favicon
Using Azure Bicep to deploy MS Graph resources
Favicon
Deploying static webs apps with the Azure cli and bicep
Favicon
Azure API Management: Harnessing Bicep for Effortless User and Subscription Creation
Favicon
User-defined function in Azure Bicep
Favicon
Expose your Open API specs with Azure API management
Favicon
Deploy multiple APIs in Azure API management, hosted in the same App service.
Favicon
Add Azure Developer CLI deployment ID and UTC timestamp to Bicep files
Favicon
🦾 Top 5 Azure Bicep tips & tricks to get started πŸš€
Favicon
Exploring the awesome Bicep Test Framework πŸ§ͺ
Favicon
The issue of recursive module calls in declarative infrastructure-as-code
Favicon
Azure Bicep - Finally functions to manipulate CIDRs
Favicon
Multi Scopes Deployment with Azure Bicep
Favicon
Azure Deployment Stacks, deploy and manage a landing zone with Bicep
Favicon
Azure Open AI: handling capacity and quota limits with Bicep
Favicon
Learn bicep based on the GUI of Azure Portal

Featured ones: