Imports in Bicep
This article describes the syntax you use to export and import shared functionality and namespaces. Using compile-time imports automatically enables language version 2.0 code generation.
Export variables, types, and functions
The @export()
decorator indicates that another file can import a specific statement. 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 by using the wildcard syntax, you must use the .
operator: <alias_name>.<exported_symbol>
.
Only statements that were exported in the file being referenced are available for import.
You can use functionality that was imported from another file without restrictions. For example, you can use imported variables anywhere that 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
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.
Related content
- To learn about the Bicep data types, see Data types.
- To learn about the Bicep functions, see Bicep functions.
- To learn about how to use the Kubernetes extension, see Bicep Kubernetes extension.
- To go through a Kubernetes extension tutorial, see Quickstart - Deploy Azure applications to Azure Kubernetes Services by using Bicep Kubernetes extension..
- To learn about how to use the Microsoft Graph extension, see Bicep templates for Microsoft Graph.