You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-resource-manager/bicep/conditional-resource-deployment.md
+11-12Lines changed: 11 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
2
-
title: Conditional deployment with Bicep
3
-
description: Describes how to conditionally deploy a resource in Bicep.
2
+
title: Conditional deployments in Bicep with the if expression
3
+
description: Understand how to use the 'if' expression to conditionally deploy a resource in Bicep.
4
4
ms.topic: conceptual
5
5
ms.custom: devx-track-bicep
6
-
ms.date: 07/11/2024
6
+
ms.date: 03/25/2025
7
7
---
8
8
9
9
# Conditional deployments in Bicep with the if expression
@@ -19,7 +19,7 @@ If you would rather learn about conditions through step-by-step guidance, see [B
19
19
20
20
## Define condition for deployment
21
21
22
-
In Bicep, you can conditionally deploy a resource by passing in a parameter that specifies whether the resource is deployed. You test the condition with an `if` expression in the resource declaration. The following example shows the syntax for an `if` expression in a Bicep file. It conditionally deploys a DNS zone. When `deployZone` is `true`, it deploys the DNS zone. When `deployZone` is `false`, it skips deploying the DNS zone.
22
+
In Bicep, you can conditionally deploy a resource by passing in a parameter that specifies if the resource is deployed. You test the condition with an `if` expression in the resource declaration. The following example shows the syntax for an `if` expression in a Bicep file. It conditionally deploys a Domain Name System (DNS) zone. When `deployZone` is `true`, it deploys the DNS zone. When `deployZone` is `false`, it skips deploying the DNS zone.
23
23
24
24
```bicep
25
25
param deployZone bool
@@ -30,7 +30,7 @@ resource dnsZone 'Microsoft.Network/dnsZones@2023-07-01-preview' = if (deployZon
30
30
}
31
31
```
32
32
33
-
The next example conditionally deploys a module.
33
+
The following example conditionally deploys a module:
Conditions may be used with dependency declarations. For [explicit dependencies](resource-dependencies.md), Azure Resource Manager automatically removes it from the required dependencies when the resource isn't deployed. For implicit dependencies, referencing a property of a conditional resource is allowed but may produce a deployment error.
43
+
Conditions can be used with dependency declarations. For [explicit dependencies](resource-dependencies.md), Azure Resource Manager automatically removes them from the required dependencies when the resource isn't deployed. For implicit dependencies, referencing a property of a conditional resource is allowed but might produce a deployment error.
44
44
45
45
## New or existing resource
46
46
47
-
You can use conditional deployment to create a new resource or use an existing one. The following example shows how to either deploy a new storage account or use an existing storage account.
47
+
You can use conditional deployment to create a new resource or use an existing one. The following example shows how to deploy a new storage account or use an existing storage account.
48
48
49
49
```bicep
50
50
param storageAccountName string
@@ -72,16 +72,16 @@ resource saExisting 'Microsoft.Storage/storageAccounts@2023-04-01' existing = if
When the parameter `newOrExisting` is set to **new**, the condition evaluates to true. The storage account is deployed. Otherwise the existing storage account is used.
75
+
When the parameter `newOrExisting` is set to **new**, the condition evaluates to true. The storage account is deployed. Otherwise, the existing storage account is used.
76
76
77
77
> [!WARNING]
78
-
> If you reference a conditionally-deployed resource that is not deployed. You will get an error saying the resource is not defined in the template.
78
+
> If you reference a conditionallydeployed resource that isn't deployed, you'll get an error saying that the resource isn't defined in the template.
79
79
80
80
## Runtime functions
81
81
82
-
If you use a [reference](./bicep-functions-resource.md#reference) or [list](./bicep-functions-resource.md#list) function with a resource that is conditionally deployed, the function is evaluated even if the resource isn't deployed. You get an error if the function refers to a resource that doesn't exist.
82
+
If you use a [reference](./bicep-functions-resource.md#reference) or [list](./bicep-functions-resource.md#list) function with a resource that's conditionally deployed, the function is evaluated even if the resource isn't deployed. You get an error if the function refers to a resource that doesn't exist.
83
83
84
-
Use the [conditional expression ?:](./operators-logical.md#conditional-expression--) operator to make sure the function is only evaluated for conditions when the resource is deployed. The following example template shows how to use this function with expressions that are only conditionally valid.
84
+
Use the [conditional expression ?:](./operators-logical.md#conditional-expression--) operator to ensure that the function is only evaluated for conditions when the resource is deployed. The following example template shows how to use this function with expressions that are only conditionally valid.
* Review the Learn module [Build flexible Bicep templates by using conditions and loops](/training/modules/build-flexible-bicep-templates-conditions-loops/).
114
113
* For recommendations about creating Bicep files, see [Best practices for Bicep](best-practices.md).
115
114
* To create multiple instances of a resource, see [Iterative loops in Bicep](loops.md).
@@ -19,7 +19,7 @@ You need a Bicep file to deploy, and the file must be local. You also need the A
19
19
20
20
Samples for the Azure CLI are written for the `bash` shell. To run this sample in Windows PowerShell or Command Prompt (cmd), you might need to change elements of the script.
21
21
22
-
If you don't have the Azure CLI installed, you can use Azure Cloud Shell. For more information, see [Deploy Bicep files with Azure Cloud Shell](./deploy-cloud-shell.md).
22
+
If you don't have the Azure CLI installed, you can use Azure Cloud Shell. For more information, see [Deploy Bicep files from Azure Cloud Shell](./deploy-cloud-shell.md).
@@ -39,23 +39,23 @@ You can target your deployment to a resource group, subscription, management gro
39
39
az deployment sub create --location <location> --template-file <path-to-bicep>
40
40
```
41
41
42
-
For more information about subscription-level deployments, see [Use Bicep to deploy resources to subscription](deploy-to-subscription.md).
42
+
For more information about subscription-level deployments, see [Subscription deployments with Bicep files](deploy-to-subscription.md).
43
43
44
44
* To deploy to a **management group**, use [`az deployment mg create`](/cli/azure/deployment/mg#az-deployment-mg-create):
45
45
46
46
```azurecli-interactive
47
47
az deployment mg create --location <location> --template-file <path-to-bicep>
48
48
```
49
49
50
-
For more information about management-group-level deployments, see [Use Bicep to deploy resources to management group](deploy-to-management-group.md).
50
+
For more information about management-group-level deployments, see [Management group deployments with Bicep files](deploy-to-management-group.md).
51
51
52
52
* To deploy to a **tenant**, use [`az deployment tenant create`](/cli/azure/deployment/tenant#az-deployment-tenant-create):
53
53
54
54
```azurecli-interactive
55
55
az deployment tenant create --location <location> --template-file <path-to-bicep>
56
56
```
57
57
58
-
For more information about tenant-level deployments, see [Use Bicep to deploy resources to tenant](deploy-to-tenant.md).
58
+
For more information about tenant-level deployments, see [Tenant deployments with Bicep file](deploy-to-tenant.md).
59
59
60
60
## Deploy local Bicep file
61
61
@@ -170,7 +170,7 @@ The evaluation of parameters follows a sequential order, meaning that if a value
170
170
171
171
### Bicep parameters files
172
172
173
-
Rather than passing parameters as inline values in your script, you might find it easier to use a [Bicep parameters file](#bicep-parameters-files) or a [JSON parameters file](#json-parameters-files) that contains the parameter values. The parameters file must be a local file since the Azure CLI doesn't support external parameters files. For more information about parameters files, see [Create parameters files for Bicep deployment](./parameter-files.md).
173
+
Rather than passing parameters as inline values in your script, you might find it easier to use a [Bicep parameters file](#bicep-parameters-files) or a [JSON parameters file](#json-parameters-files) that contains the parameter values. The parameters file must be a local file since the Azure CLI doesn't support external parameters files. For more information about parameters files, see [Create a parameters file for Bicep deployment](./parameter-files.md).
174
174
175
175
You can use a Bicep parameters file to deploy a Bicep file with [Azure CLI](./install.md#azure-cli) version 2.53.0 or later and [Bicep CLI](./install.md#visual-studio-code-and-bicep-extension) version 0.22.X or later. With the `using` statement within the Bicep parameters file, there's no need to provide the `--template-file` switch when specifying a Bicep parameters file for the `--parameters` switch. Including the `--template-file` switch will prompt an, "Only a .bicep template is allowed with a .bicepparam file," error.
176
176
@@ -233,4 +233,4 @@ To avoid conflicts with concurrent deployments and to ensure unique entries in t
233
233
234
234
## Next steps
235
235
236
-
To understand how to define parameters in your file, see [Understand the structure and syntax of Bicep files](file.md).
236
+
To understand how to define parameters in your file, see [Bicep file structure and syntax](file.md).
To *conditionally deploy a module*, add an `if` expression. The use is similar to [conditionally deploying a resource](conditional-resource-deployment.md).
94
+
To *conditionally deploy a module*, add an `if` expression. This is similar to [conditionally deploying a resource](conditional-resource-deployment.md).
Like resources, modules are deployed in parallel unless they depend on other modules or resources. Typically, you don't need to set dependencies because they're determined implicitly. If you need to set an explicit dependency, add `dependsOn` to the module definition. To learn more about dependencies, see [Resource dependencies](resource-dependencies.md).
119
+
Like resources, modules are deployed in parallel unless they depend on other modules or resources. Typically, you don't need to set dependencies because they're determined implicitly. If you need to set an explicit dependency, add `dependsOn` to the module definition. To learn more about dependencies, see [Resource dependencies in Bicep](resource-dependencies.md).
120
120
121
121
```bicep
122
122
module <symbolic-name> '<path-to-file>' = {
@@ -136,7 +136,7 @@ The file for the module can be either a local file or an external file. The exte
136
136
137
137
### Local file
138
138
139
-
If the module is a *local file*, provide a relative path to that file. All paths in Bicep must be specified by using the forward slash (/) directory separator to ensure consistent compilation across platforms. The Windows backslash (\\) character is unsupported. Paths can contain spaces.
139
+
If the module is a *local file*, provide a relative path to that file. All paths in Bicep must be specified by the forward slash (/) directory separator to ensure consistent compilation across platforms. The Windows backslash (\\) character isn't supported. Paths can contain spaces.
140
140
141
141
For example, to deploy a file that's up one level in the directory from your main file, use:
142
142
@@ -160,7 +160,7 @@ There are public and private module registries.
160
160
161
161
[Azure Verified Modules](https://azure.github.io/Azure-Verified-Modules/) are prebuilt, pretested, and preverified modules that you can use to deploy resources on Azure. Microsoft employees created and own these modules. They were designed to simplify and accelerate the deployment process for common Azure resources and configurations. The modules also align to best practices like Azure Well-Architected Framework.
162
162
163
-
Browse to the [Azure Verified Modules Bicep Index](https://azure.github.io/Azure-Verified-Modules/indexes/bicep/) to see the list of modules that are available. Select the highlighted numbers in the following screenshot to go directly to that filtered view.
163
+
Browse [Bicep Modules](https://azure.github.io/Azure-Verified-Modules/indexes/bicep/) to see the list of modules that are available. Select the highlighted numbers in the following screenshot to go directly to that filtered view:
164
164
165
165
:::image type="content" source="./media/modules/bicep-azure-verified-modules-avm.png" alt-text="Screenshot that shows Azure Verified Modules.":::
166
166
@@ -174,9 +174,9 @@ To link to a public module, specify the module path with the following syntax:
When you reference a module in a registry, the Bicep extension in Visual Studio Code automatically calls [bicep restore](bicep-cli.md#restore) to copy the external module to the local cache. It takes a few moments to restore the external module. If IntelliSense for the module doesn't work immediately, wait for the restore to complete.
223
+
When you reference a module in a registry, the Bicep extension in Visual Studio Code automatically calls [`bicep restore`](bicep-cli.md#restore) to copy the external module to the local cache. It takes a few moments to restore the external module. If IntelliSense for the module doesn't work immediately, wait for the restore to complete.
224
224
225
225
The full path for a module in a registry can be long. Instead of providing the full path each time you want to use the module, [configure aliases in the bicepconfig.json file](bicep-config-modules.md#aliases-for-modules). The aliases make it easier to reference the module. For example, with an alias, you can shorten the path to:
Decorators are written in the format `@expression` and are placed above module declarations. The following table shows the available decorators for modules.
276
+
Decorators are written in the format `@expression` and are placed above module declarations. The following table shows the available decorators for modules:
Set the `scope` property to a valid scope object. If your Bicep file deploys a resource group, subscription, or management group, you can set the scope for a module to the symbolic name for that resource. Or you can use the scope functions to get a valid scope.
453
+
Set the `scope` property to a valid scope object. If your Bicep file deploys a resource group, subscription, or management group, you can set the scope for a module to the symbolic name for that resource. Or, you can use the scope functions to get a valid scope.
You can get values from a module and use them in the main Bicep file. To get an output value from a module, use the `outputs` property on the module object.
476
476
477
-
The first example creates a storage account and returns the primary endpoints.
477
+
The first example creates a storage account and returns the primary endpoints:
For more information, see [Deploy Bicep files by using Azure PowerShell](./deploy-powershell.md#parameters). To deploy `.bicep` files, you need Azure PowerShell version 5.6.0 or later.
476
+
For more information, see [Deploy Bicep files with Azure PowerShell](./deploy-powershell.md#parameters). To deploy `.bicep` files, you need Azure PowerShell version 5.6.0 or later.
0 commit comments