File functions for Bicep

This article describes the Bicep functions for loading content from external files.

loadFileAsBase64

loadFileAsBase64(filePath)

Loads the file as a base64 string.

Namespace: sys.

Parameters

Parameter Required Type Description
filePath Yes string The path to the file to load. The path is relative to the deployed Bicep file, and it should be a compile-time constant (cannot use variables).

Remarks

Use this function when you have binary content you would like to include in deployment. Rather than manually encoding the file to a base64 string and adding it to your Bicep file, load the file with this function. The file is loaded when the Bicep file is compiled to a JSON template. Hence variables cannot be used in filePath as they are not resolved at this stage. During deployment, the JSON template contains the contents of the file as a hard-coded string.

This function requires Bicep version 0.4.412 or later.

The maximum allowed size of the file is 96 Kb.

Return value

The file as a base64 string.

loadJsonContent

loadJsonContent(filePath, [jsonPath], [encoding])

Loads the specified JSON file as an Any object.

Namespace: sys.

Parameters

Parameter Required Type Description
filePath Yes string The path to the file to load. The path is relative to the deployed Bicep file. The path is relative to the deployed Bicep file, and it should be a compile-time constant (cannot use variables).
jsonPath No string JSONPath expression to take only a part of the JSON into ARM.
encoding No string The file encoding. The default value is utf-8. The available options are: iso-8859-1, us-ascii, utf-16, utf-16BE, or utf-8.

Remarks

Use this function when you have JSON content or minified JSON content that is stored in a separate file. Rather than duplicating the JSON content in your Bicep file, load the content with this function. You can load a part of a JSON file by specifying a JSON path. The file is loaded when the Bicep file is compiled to the JSON template. Hence variables cannot be used in filePath as they are not resolved at this stage. During deployment, the JSON template contains the contents of the file as a hard-coded string.

In VS Code, the properties of the loaded object are available intellisense. For example, you can create a file with values to share across many Bicep files. An example is shown in this article.

This function requires Bicep version 0.7.4 or later.

The maximum allowed size of the file is 1,048,576 characters, including line endings.

Return value

The contents of the file as an Any object.

Examples

The following example creates a JSON file that contains values for a network security group.

{
  "description": "Allows SSH traffic",
  "protocol": "Tcp",
  "sourcePortRange": "*",
  "destinationPortRange": "22",
  "sourceAddressPrefix": "*",
  "destinationAddressPrefix": "*",
  "access": "Allow",
  "priority": 100,
  "direction": "Inbound"
}

You load that file and convert it to a JSON object. You use the object to assign values to the resource.

param location string = resourceGroup().location

var nsgconfig = loadJsonContent('nsg-security-rules.json')

resource newNSG 'Microsoft.Network/networkSecurityGroups@2021-02-01' = {
  name: 'example-nsg'
  location: location
  properties: {
    securityRules: [
      {
        name: 'SSH'
        properties: {
          description: nsgconfig.description
          protocol: nsgconfig.protocol
          sourcePortRange: nsgconfig.sourcePortRange
          destinationPortRange: nsgconfig.destinationPortRange
          sourceAddressPrefix: nsgconfig.sourceAddressPrefix
          destinationAddressPrefix: nsgconfig.destinationAddressPrefix
          access: nsgconfig.access
          priority: nsgconfig.priority
          direction: nsgconfig.direction
        }
      }
    ]
  }
}

You can reuse the file of values in other Bicep files that deploy a network security group.

loadTextContent

loadTextContent(filePath, [encoding])

Loads the content of the specified file as a string.

Namespace: sys.

Parameters

Parameter Required Type Description
filePath Yes string The path to the file to load. The path is relative to the deployed Bicep file. The path is relative to the deployed Bicep file, and it should be a compile-time constant (cannot use variables).
encoding No string The file encoding. The default value is utf-8. The available options are: iso-8859-1, us-ascii, utf-16, utf-16BE, or utf-8.

Remarks

Use this function when you have content that is more stored in a separate file. Rather than duplicating the content in your Bicep file, load the content with this function. For example, you can load a deployment script from a file. The file is loaded when the Bicep file is compiled to the JSON template. Hence variables cannot be used in filePath as they are not resolved at this stage. During deployment, the JSON template contains the contents of the file as a hard-coded string.

Use the loadJsonContent() function to load JSON files.

This function requires Bicep version 0.4.412 or later.

The maximum allowed size of the file is 131,072 characters, including line endings.

Return value

The contents of the file as a string.

Examples

The following example loads a script from a file and uses it for a deployment script.

resource exampleScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  name: 'exampleScript'
  location: resourceGroup().location
  kind: 'AzurePowerShell'
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '/subscriptions/{sub-id}/resourcegroups/{rg-name}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{id-name}': {}
    }
  }
  properties: {
    azPowerShellVersion: '3.0'
    scriptContent: loadTextContent('myscript.ps1')
    retentionInterval: 'P1D'
  }
}

Next steps