Skip to content

Commit b9ef58d

Browse files
authored
Merge pull request #178318 from johndowns/bicep-best-practices
Update Bicep best practices
2 parents b7ecd73 + 1ed0cfc commit b9ef58d

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

articles/azure-resource-manager/bicep/best-practices.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Describes practices to follow when creating your Bicep files so the
44
author: johndowns
55
ms.author: jodowns
66
ms.topic: conceptual
7-
ms.date: 06/01/2021
7+
ms.date: 11/02/2021
88
---
99
# Best practices for Bicep
1010

@@ -36,8 +36,6 @@ For more information about Bicep parameters, see [Parameters in Bicep](parameter
3636

3737
## Variables
3838

39-
* Use lower camel case for variable names, such as `myVariableName`.
40-
4139
* When you define a variable, the [data type](data-types.md) isn't needed. Variables infer the type from the resolve value.
4240

4341
* You can use Bicep functions to create a variable.
@@ -46,14 +44,30 @@ For more information about Bicep parameters, see [Parameters in Bicep](parameter
4644

4745
For more information about Bicep variables, see [Variables in Bicep](variables.md).
4846

49-
## Naming
47+
## Names
48+
49+
* Use lower camel case for names, such as `myVariableName` or `myResource`.
5050

5151
* The [uniqueString() function](bicep-functions-string.md#uniquestring) is useful for creating globally unique resource names. When you provide the same parameters, it returns the same string every time. Passing in the resource group ID means the string is the same on every deployment to the same resource group, but different when you deploy to different resource groups or subscriptions.
5252

5353
* Sometimes the `uniqueString()` function creates strings that start with a number. Some Azure resources, like storage accounts, don't allow their names to start with numbers. This requirement means it's a good idea to use string interpolation to create resource names. You can add a prefix to the unique string.
5454

5555
* It's often a good idea to use template expressions to create resource names. Many Azure resource types have rules about the allowed characters and length of their names. Embedding the creation of resource names in the template means that anyone who uses the template doesn't have to remember to follow these rules themselves.
5656

57+
* Avoid using `name` in a symbolic name. The symbolic name represents the resource, not the resource's name. For example, instead of this:
58+
59+
```bicep
60+
resource cosmosDBAccountName 'Microsoft.DocumentDB/databaseAccounts@2021-04-15' = {
61+
```
62+
63+
use this:
64+
65+
```bicep
66+
resource cosmosDBAccount 'Microsoft.DocumentDB/databaseAccounts@2021-04-15' = {
67+
```
68+
69+
* Avoid distinguishing variables and parameters by the use of suffixes.
70+
5771
## Resource definitions
5872

5973
* Instead of embedding complex expressions directly into resource properties, use variables to contain the expressions. This approach makes your Bicep file easier to read and understand. It avoids cluttering your resource definitions with logic.
@@ -64,19 +78,21 @@ For more information about Bicep variables, see [Variables in Bicep](variables.m
6478

6579
* When possible, avoid using the [reference](./bicep-functions-resource.md#reference) and [resourceId](./bicep-functions-resource.md#resourceid) functions in your Bicep file. You can access any resource in Bicep by using the symbolic name. For example, if you define a storage account with the symbolic name toyDesignDocumentsStorageAccount, you can access its resource ID by using the expression `toyDesignDocumentsStorageAccount.id`. By using the symbolic name, you create an implicit dependency between resources.
6680

81+
* Prefer using implicit dependencies over explicit dependencies. Although the `dependsOn` resource property enables you to declare an explicit dependency between resources, it's usually possible to use the other resource's properties by using its symbolic name. This creates an implicit dependency between the two resources, and enables Bicep to manage the relationship itself.
82+
6783
* If the resource isn't deployed in the Bicep file, you can still get a symbolic reference to the resource using the `existing` keyword.
6884

6985
## Child resources
7086

7187
* Avoid nesting too many layers deep. Too much nesting makes your Bicep code harder to read and work with.
7288

73-
* It's best to avoid constructing resource names for child resources. You lose the benefits that Bicep provides when it understands the relationships between your resources. Use the `parent` property or nesting instead.
89+
* Avoid constructing resource names for child resources. You lose the benefits that Bicep provides when it understands the relationships between your resources. Use the `parent` property or nesting instead.
7490

7591
## Outputs
7692

7793
* Make sure you don't create outputs for sensitive data. Output values can be accessed by anyone who has access to the deployment history. They're not appropriate for handling secrets.
7894

79-
* Instead of passing property values around through outputs, use the `existing` keyword to look up properties of resources that already exist. It's a best practice to look up keys from other resources in this way instead of passing them around through outputs. You'll always get the most up-to-date data.
95+
* Instead of passing property values around through outputs, use the `[existing` keyword](resource-declaration.md#existing-resources) to look up properties of resources that already exist. It's a best practice to look up keys from other resources in this way instead of passing them around through outputs. You'll always get the most up-to-date data.
8096

8197
For more information about Bicep outputs, see [Outputs in Bicep](outputs.md).
8298

0 commit comments

Comments
 (0)