Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Bicep - Practical Use Cases of String Functions in Azure Deployments

·791 words·4 mins· 100 views · 5 likes ·
Microsoft Microsoft Azure Bicep Bicep Functions

Bicep is a language specifically designed for deploying Azure resources and offers a more user-friendly syntax than JSON. One of the cool features of Bicep is its built-in functions that allow performing a variety of operations and manipulations on data while creating Azure Resource Manager (ARM) templates.

Bicep functions expand the capabilities of Bicep templates, allowing you to create more dynamic and flexible templates that better fit your Azure resource deployment needs.

Here are the main functions you can access in Bicep:

  • Array functions
  • CIDR functions
  • Date functions
  • Deployment value functions
  • File functions
  • Lambda functions
  • Logical functions
  • Numeric functions
  • Object functions
  • Resource functions
  • Scope functions
  • String functions

In this blog post, I want to show you some practical applications of string functions in Bicep when working with Azure resources. Let’s go.

String functions>

String functions #

In the Azure environment, managing and manipulating strings is a common task that can significantly impact the creation and handling of resources. String operations can range from simple tasks, such as formatting resource names or connection strings, to more complex operations, such as making REST API calls or parsing API responses.

Concatenation with concat()>

Concatenation with concat() #

Useful for combining several strings when forming resource names or creating URI paths.

var storageAccountName = concat('storage', uniqueString(resourceGroup().id))
// This results in a unique name for a storage account.
Checking Emptiness with empty()>

Checking Emptiness with empty() #

This can be used to check if an optional value has been provided in the parameters.

param optionalStorageAccountName string = ''
resource optionalStorageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = if (!empty(optionalStorageAccountName)) {
  name: optionalStorageAccountName
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  kind: 'StorageV2'
//Determine if a resource should be created depending on whether a value has been provided in the parameters.
Getting String Length with length()>

Getting String Length with length() #

Ideal for validating the size of strings, such as ensuring resource names meet length requirements.

var storageAccountName = 'myuniquestorageaccount'
assert(length(storageAccountName) <= 24, 'Storage account name must be 24 characters or less')
// This would verify that the provided storage account name isn't too long.
Replacing Substrings with replace()>

Replacing Substrings with replace() #

Useful for formatting strings, like removing unwanted characters from resource names.

var resourceNameWithDashes = 'my-resource-name'
var validResourceName = replace(resourceNameWithDashes, '-', '')
// This would remove all hyphens from the resource name, which could be useful if a service does not support hyphens in resource names.
Get the first numbers of characters using take()>

Get the first numbers of characters using take() #

This can be useful when working with resource names or connection strings that need to be shortened to meet Azure specific limits.

var resourceName = 'MyVeryLongResourceNameThatExceedsAzureLimits'
var shortenedResourceName = take(resourceName, 24) 
// Use this function to shorten the resource name to a specific length.
Lowercasing and Uppercasing with toLower() and toUpper()>

Lowercasing and Uppercasing with toLower() and toUpper() #

These can come in handy when handling case-sensitive input data, like the CORS policy keys for storage accounts.

var storageAccountName = toLower(concat('ST${projectName}', uniqueString(resourceGroup().id)))
// Use lowercase letters when creating storage accounts to ensure the correct format. 
var environmentVariable = toUpper('myenvironmentvariable')
// Use all caps for environment variables in apps that require it.
Trimming Spaces with trim()>

Trimming Spaces with trim() #

Useful for ensuring input data doesn’t have unwanted white space at the beginning or end.

// Define the input parameter, it could be a name of an Azure resource with trailing whitespaces.
param resourceName string = ' myResourceName '

var trimmedResourceName = trim(resourceName)
// Use this function to remove extra spaces at the start or end of your text. 
Building URIs with uri()>

Building URIs with uri() #

Useful for forming URIs securely, like when creating URIs for service endpoints.

var endpoint = uri('', 'api/HttpTrigger')
// This would result in ''
Extracting Substrings with substring()>

Extracting Substrings with substring() #

Great for extracting specific parts of strings, like getting a slice of a resource identifier.

var resourceId = '/subscriptions/00000d-00f0-00d0-0000-000ab0aa000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM'
var subscriptionId = substring(resourceId, length('/subscriptions/'), indexOf(resourceId, '/resourceGroups/') - length('/subscriptions/'))
// This would result in '00000d-00f0-00d0-0000-000ab0aa000'
Generating Unique Strings with uniqueString()>

Generating Unique Strings with uniqueString() #

Extremely useful for creating unique identifiers based on string values, commonly used to generate unique resource names in Azure.

var storageAccountName = uniqueString(resourceGroup().id)
// This generates a unique name for a storage account based on the resource group's ID.

These are just a few examples of how the string functions in Bicep can be used to deploy and manage Azure resources. Remember that Bicep has many more features that we can use in multiple ways to meet our specific needs.

References and useful links #

Thank you for taking the time to read my post. I sincerely hope that you find it helpful.