แก้ไข

แชร์ผ่าน


Imports in Bicep

This article describes the syntax you use to export and import shared functionality and namespaces for Bicep extensions. 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 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.