Imports in Bicep

This article describes the syntax you use to export and import shared functionality, and namespaces for Bicep extensions.

Export variables, types, and functions

The @export() decorator indicates that a given statement can be imported by another file. This decorator is only valid on type, var, and func statements. Variable statements marked with @export() must be compile-time constants.

The syntax for exporting functionality for use in other Bicep files is:

@export()
<statement_to_export>

Import variables, types, and functions

The syntax for importing functionality from another Bicep file is:

import {<symbol_name>, <symbol_name>, ...} from '<bicep_file_name>'

With optional aliasing to rename symbols:

import {<symbol_name> as <alias_name>, ...} from '<bicep_file_name>'

Using the wildcard import syntax:

import * as <alias_name> from '<bicep_file_name>'

You can mix and match the preceding syntaxes. To access imported symbols using the wildcard syntax, you must use the . operator: <alias_name>.<exported_symbol>.

Only statements that have been exported in the file being referenced are available to be imported.

Functionality that has been imported from another file can be used without restrictions. For example, imported variables can be used anywhere a variable declared in-file would normally be valid.

Example

exports.bicep

@export()
type myObjectType = {
  foo: string
  bar: int
}

@export()
var myConstant = 'This is a constant value'

@export()
func sayHello(name string) string => 'Hello ${name}!'

main.bicep

import * as myImports from 'exports.bicep'
import {myObjectType, sayHello} from 'exports.bicep'

param exampleObject myObjectType = {
  foo: myImports.myConstant
  bar: 0
}

output greeting string = sayHello('Bicep user')
output exampleObject myImports.myObjectType = exampleObject

Import namespaces and extensions (Preview)

Note

The experimental feature extensibility must be enabled from the Bicep config file to use this feature.

The syntax for importing namespaces is:

import 'az@1.0.0'
import 'sys@1.0.0'

Both az and sys are Bicep built-in namespaces. They're imported by default. For more information about the data types and the functions defined in az and sys, see Data types and Bicep functions.

The syntax for importing Bicep extensions is:

import '<extension-name>@<extension-version>'

The syntax for importing Bicep extensions, which require configuration is:

import '<extension-name>@<extension-version>' with {
  <extension-properties>
}

For an example, see Bicep Kubernetes extension.

Next steps