Skip to content

Commit a49d891

Browse files
Merge pull request #249682 from mumian/0828-udf
Bicep user-defined function
2 parents 773ed52 + 31f6ef2 commit a49d891

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

articles/azure-resource-manager/bicep/bicep-config.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep config file
33
description: Describes the configuration file for your Bicep deployments
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 08/08/2023
6+
ms.date: 08/30/2023
77
---
88

99
# Configure your Bicep environment
@@ -56,7 +56,7 @@ The preceding sample enables 'userDefineTypes' and 'extensibility`. The availabl
5656
- **sourceMapping**: Enables basic source mapping to map an error location returned in the ARM template layer back to the relevant location in the Bicep file.
5757
- **resourceTypedParamsAndOutputs**: Enables the type for a parameter or output to be of type resource to make it easier to pass resource references between modules. This feature is only partially implemented. See [Simplifying resource referencing](https://github.com/azure/bicep/issues/2245).
5858
- **symbolicNameCodegen**: Allows the ARM template layer to use a new schema to represent resources as an object dictionary rather than an array of objects. This feature improves the semantic equivalent of the Bicep and ARM templates, resulting in more reliable code generation. Enabling this feature has no effect on the Bicep layer's functionality.
59-
- **userDefinedFunctions**: Allows you to define your own custom functions.
59+
- **userDefinedFunctions**: Allows you to define your own custom functions. See [User-defined functions in Bicep](./user-defined-functions.md).
6060
- **userDefinedTypes**: Allows you to define your own custom types for parameters. See [User-defined types in Bicep](https://aka.ms/bicepCustomTypes).
6161

6262
## Next steps

articles/azure-resource-manager/bicep/file.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep file structure and syntax
33
description: Describes the structure and properties of a Bicep file using declarative syntax.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 06/06/2023
6+
ms.date: 08/30/2023
77
---
88

99
# Understand the structure and syntax of Bicep files
@@ -23,6 +23,8 @@ metadata <metadata-name> = ANY
2323
2424
targetScope = '<scope>'
2525
26+
func <user-defined-function-name> (<argument-name> <data-type>, <argument-name> <data-type>, ...) <function-data-type> => <expression>
27+
2628
@<decorator>(<argument>)
2729
param <parameter-name> <parameter-data-type> = <default-value>
2830
@@ -95,6 +97,21 @@ The allowed values are:
9597

9698
In a module, you can specify a scope that is different than the scope for the rest of the Bicep file. For more information, see [Configure module scope](modules.md#set-module-scope)
9799

100+
## Functions (Preview)
101+
102+
> [!NOTE]
103+
> To enable the preview feature, see [Enable experimental features](./bicep-config.md#enable-experimental-features).
104+
105+
In your Bicep file, you can create your own functions in addition to using the [standard Bicep functions](./bicep-functions.md) that are automatically available within your Bicep files. Create your own functions when you have complicated expressions that are used repeatedly in your Bicep files.
106+
107+
```bicep
108+
func buildUrl(https bool, hostname string, path string) string => '${https ? 'https' : 'http'}://${hostname}${empty(path) ? '' : '/${path}'}'
109+
110+
output azureUrl string = buildUrl(true, 'microsoft.com', 'azure')
111+
```
112+
113+
For more information, see [User-defined functions](./user-defined-functions.md).
114+
98115
## Parameters
99116

100117
Use parameters for values that need to vary for different deployments. You can define a default value for the parameter that is used if no value is provided during deployment.

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@
267267
- name: User-defined data types (type)
268268
displayName: custom
269269
href: user-defined-data-types.md
270+
- name: User-defined functions (func)
271+
displayName: custom
272+
href: user-defined-functions.md
270273
- name: Parameters (param)
271274
href: parameters.md
272275
displayName: decorators,secure,objects,constraints
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: User-defined functions in Bicep
3+
description: Describes how to define and use user-defined functions in Bicep.
4+
ms.topic: conceptual
5+
ms.custom: devx-track-bicep
6+
ms.date: 08/30/2023
7+
---
8+
9+
# User-defined functions in Bicep (Preview)
10+
11+
Within your Bicep file, you can create your own functions. These functions are available for use in your Bicep files. User-defined functions are separate from the [standard Bicep functions](./bicep-functions.md) that are automatically available within your Bicep files. Create your own functions when you have complicated expressions that are used repeatedly in your Bicep files.
12+
13+
[Bicep version 0.20 or newer](./install.md) is required to use this feature.
14+
15+
## Enable the preview feature
16+
17+
To enable this preview, modify your project's [bicepconfig.json](./bicep-config.md) file to include the following JSON:
18+
19+
```json
20+
{
21+
"experimentalFeaturesEnabled": {
22+
"userDefinedFunctions": true
23+
}
24+
}
25+
```
26+
27+
## Define the function
28+
29+
Use the `func` statement to define user-defined functions.
30+
31+
```bicep
32+
func <user-defined-function-name> (<argument-name> <data-type>, <argument-name> <data-type>, ...) <function-data-type> => <expression>
33+
```
34+
35+
## Examples
36+
37+
The following examples show how to define and use user-defined functions:
38+
39+
```bicep
40+
func buildUrl(https bool, hostname string, path string) string => '${https ? 'https' : 'http'}://${hostname}${empty(path) ? '' : '/${path}'}'
41+
42+
func sayHelloString(name string) string => 'Hi ${name}!'
43+
44+
func sayHelloObject(name string) object => {
45+
hello: 'Hi ${name}!'
46+
}
47+
48+
func nameArray(name string) array => [
49+
name
50+
]
51+
52+
func addNameArray(name string) array => [
53+
'Mary'
54+
'Bob'
55+
name
56+
]
57+
58+
output azureUrl string = buildUrl(true, 'microsoft.com', 'azure')
59+
output greetingArray array = map(['Evie', 'Casper'], name => sayHelloString(name))
60+
output greetingObject object = sayHelloObject('John')
61+
output nameArray array = nameArray('John')
62+
output addNameArray array = addNameArray('John')
63+
64+
```
65+
66+
The outputs from the preceding examples are:
67+
68+
69+
| Name | Type | Value |
70+
| ---- | ---- | ----- |
71+
| azureUrl | String | https://microsoft.com/azure |
72+
| greetingArray | Array | ["Hi Evie!","Hi Casper!"] |
73+
| greetingObject | Object | {"hello":"Hi John!"} |
74+
| nameArray | Array | ["John"] |
75+
| addNameArray | Array | ["Mary","Bob","John"] |
76+
77+
## Limitations
78+
79+
When defining a user function, there are some restrictions:
80+
81+
* The function can't access variables.
82+
* The function can only use parameters that are defined in the function.
83+
* The function can't call other user-defined functions.
84+
* The function can't use the [reference](bicep-functions-resource.md#reference) function or any of the [list](bicep-functions-resource.md#list) functions.
85+
* Parameters for the function can't have default values.
86+
87+
## Next steps
88+
89+
* To learn about the Bicep file structure and syntax, see [Understand the structure and syntax of Bicep files](./file.md).
90+
* For a list of the available Bicep functions, see [Bicep functions](./bicep-functions.md).

0 commit comments

Comments
 (0)