Bicep is a Domain-Specific Language (DSL) for defining Azure Resource Manager (ARM) templates. It provides a concise and readable way to describe Azure resources and their properties. Understanding data types is crucial in Bicep, as they define the structure and behavior of resources.
This is a new series of articles about ‘BiCep’ technology by Microsoft – it is a game changer for resource management in the cloud – well worth investigating if you are a cloud builder!
Basic Data Types in Bicep
Strings
Strings in Bicep are sequences of characters enclosed in single or double quotes. They are used to represent text values. Strings in the Bicep support interpolation, enabling dynamic content insertion.
Example
param storageAccountName string = ‘myStorageAccount’
Multi-line Strings
Bicep supports multi-line strings using triple quotes ”’ ”’. This is useful when defining long text values. The characters entered within the opening and closing sequence are read exactly as they are, and there is no need for escaping characters.
Example
var longText = ”’
This is a
multi-line
string
”’
Integers
Integers are whole numbers without a decimal point. In Bicep, integers are represented as 64-bit integers. However, the range of values may be constrained by the SDK or command-line tool used for deployment when passed as inline parameters. For instance, when deploying a Bicep template using PowerShell, integer types typically range from -2147483648 to 2147483647.
To work around this limitation, it is advisable to specify large integer values in a parameter file. Additionally, it is worth noting that resource types may impose their own restrictions on integer properties.
Note. Bicep does not currently support floating-point, decimal, or binary formats.
Example
param numberOfInstances int = 3
Bool
Boolean values represent true or false. These data types are useful for logical decisions and conditions in your code.
Note. Avoid enclosing the value in quotation marks.
Example
param useSSLEncryption bool = true
Complex Data Types
Objects
Objects in Bicep are collections of key-value pairs enclosed in curly braces {}. They are used to represent complex data structures and are commonly used to define properties of Azure resources. Each key in an object is a property name, and the corresponding value is the property value.
Objects allow you to group related properties together, which can improve the readability and maintainability of your Bicep code. They also enable you to pass structured data to resources or modules, making your templates more flexible and reusable.
Example
var storageAccountConfig = {
name: 'myStorageAccount',
type: 'Standard_LRS',
tags: {
environment: 'production',
department: 'finance'
}
};
You can access the properties of an object using dot notation (.). For example, to access the name property of ‘storageAccountConfig’, you would use ‘storageAccountConfig.name’.
Arrays
Arrays in Bicep are ordered collections of values enclosed in square brackets []. They are used to represent lists of items. Arrays can be declared in a single line or multiple lines, providing flexibility in coding.
Use commas (,) to separate values in single-line declarations, while in multiple-line declarations, they are not required.
Example
var instanceTypes = [
'Standard_DS1_v2',
'Standard_DS2_v2',
'Standard_DS3_v2'
];
Secure Objects
Bicep has introduced secure objects to enhance security by protecting sensitive information. These objects are encrypted and provide an extra layer of protection for confidential data. Thus, to enhance the security of a string or object, apply the @secure() decorator to it.
Example
@secure()
param mySuperSecretObject object
Secure Strings
Similar to secure objects, secure strings provide additional security for sensitive information. These strings are encrypted and are designed to prevent unauthorized access to critical data.
Example
@secure()
param intAsString string
var num = int(intAsString)
Now that we have covered the supported data types in Bicep, let’s explore how user-defined data types can further enhance your Bicep code in the next section.
User-Defined Data Types in Bicep
Bicep extends its supported data types by enabling the definition of user-defined types using the `type` statement. This feature enhances code reusability and simplifies Bicep projects by allowing developers to define custom types based on primitive literals, arrays, and objects.
User-defined types can be utilized across a Bicep file, including in `param` statements and object type properties, making it easy to encapsulate complex data structures and reuse them throughout your code.
This capability empowers developers to create efficient and maintainable infrastructure as code solutions. Whether representing tagged union types or importing types from external sources, Bicep’s user-defined data types provide the flexibility and expressiveness needed for robust Azure deployments.
Example
type SkuType = 'Standard_LRS' | 'Standard_GRS';
type StorageAccountConfig = {
name: string;
sku: SkuType;
};
Wrap-up
So then, we have learned that mastering data types in Bicep is essential for creating concise and effective Azure Resource Manager templates. By understanding and utilizing the various data types available, you can accurately define the structure and behavior of your Azure resources, leading to more efficient deployments.
Happy cloud building!