Skip to content

Commit 3e63470

Browse files
Merge pull request #186344 from tfitzmac/0126concepts
move bicep reference
2 parents 802677e + 68c120b commit 3e63470

16 files changed

+290
-269
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ For more information about Bicep variables, see [Variables in Bicep](variables.m
9292

9393
* 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.
9494

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.
95+
* Instead of passing property values around through outputs, use the [existing keyword](existing-resource.md) 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.
9696

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

articles/azure-resource-manager/bicep/conditional-resource-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module dnsZone 'dnszones.bicep' = if (deployZone) {
4242
}
4343
```
4444

45-
Conditions may be used with dependency declarations. For [explicit dependencies](resource-declaration.md#dependencies), 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.
45+
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.
4646

4747
## New or existing resource
4848

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Reference existing resource in Bicep
3+
description: Describes how to reference a resource that already exists.
4+
ms.topic: conceptual
5+
ms.date: 02/04/2022
6+
---
7+
8+
# Existing resources in Bicep
9+
10+
To reference an existing resource that isn't deployed in your current Bicep file, declare the resource with the `existing` keyword. Use the `existing` keyword when you're deploying a resource that needs to get a value from an existing resource. You access the existing resource's properties through its symbolic name.
11+
12+
The resource isn't redeployed when referenced with the `existing` keyword.
13+
14+
## Same scope
15+
16+
The following example gets an existing storage account in the same resource group as the current deployment. Notice that you provide only the name of the existing resource. The properties are available through the symbolic name.
17+
18+
```bicep
19+
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
20+
name: 'examplestorage'
21+
}
22+
23+
output blobEndpoint string = stg.properties.primaryEndpoints.blob
24+
```
25+
26+
## Different scope
27+
28+
Set the `scope` property to access a resource in a different scope. The following example references an existing storage account in a different resource group.
29+
30+
```bicep
31+
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
32+
name: 'examplestorage'
33+
scope: resourceGroup(exampleRG)
34+
}
35+
36+
output blobEndpoint string = stg.properties.primaryEndpoints.blob
37+
```
38+
39+
For more information about setting the scope, see [Scope functions for Bicep](bicep-functions-scope.md).
40+
41+
## Troubleshooting
42+
43+
If you attempt to reference a resource that doesn't exist, you get the `NotFound` error and your deployment fails. Check the name and scope of the resource you're trying to reference.
44+
45+
## Next steps
46+
47+
For the syntax to deploy a resource, see [Resource declaration in Bicep](resource-declaration.md).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,4 @@ resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2021-06-01
340340

341341
## Next steps
342342

343-
- To set dependencies on resources that are created in a loop, see [Resource dependencies](./resource-declaration.md#dependencies).
343+
- To set dependencies on resources that are created in a loop, see [Resource dependencies](resource-dependencies.md).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ To deploy **more than one instance** of a module, add the `for` expression. You
5959

6060
::: code language="bicep" source="~/azure-docs-bicep-samples/syntax-samples/modules/iterative-definition.bicep" highlight="3" :::
6161

62-
Like resources, modules are deployed in parallel unless they depend on other modules or resources. Typically, you don't need to set dependencies as they're determined implicitly. If you need to set an explicit dependency, you can add `dependsOn` to the module definition. To learn more about dependencies, see [Resource dependencies](resource-declaration.md#dependencies).
62+
Like resources, modules are deployed in parallel unless they depend on other modules or resources. Typically, you don't need to set dependencies as they're determined implicitly. If you need to set an explicit dependency, you can add `dependsOn` to the module definition. To learn more about dependencies, see [Resource dependencies](resource-dependencies.md).
6363

6464
::: code language="bicep" source="~/azure-docs-bicep-samples/syntax-samples/modules/dependsOn-definition.bicep" highlight="6-8" :::
6565

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

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
---
22
title: Declare resources in Bicep
33
description: Describes how to declare resources to deploy in Bicep.
4-
author: mumian
5-
ms.author: jgao
64
ms.topic: conceptual
7-
ms.date: 11/12/2021
5+
ms.date: 02/04/2022
86
---
97

108
# Resource declaration in Bicep
@@ -187,111 +185,8 @@ resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
187185
}
188186
```
189187

190-
## Dependencies
191-
192-
When deploying resources, you may need to make sure some resources exist before other resources. For example, you need a logical SQL server before deploying a database. You establish this relationship by marking one resource as dependent on the other resource. Order of resource deployment can be influenced in two ways: [implicit dependency](#implicit-dependency) and [explicit dependency](#explicit-dependency)
193-
194-
Azure Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. When resources aren't dependent on each other, Resource Manager deploys them in parallel. You only need to define dependencies for resources that are deployed in the same Bicep file.
195-
196-
### Implicit dependency
197-
198-
An implicit dependency is created when one resource declaration references another resource in the same deployment. For example, *dnsZone* is referenced by the second resource definition in the following example:
199-
200-
```bicep
201-
resource dnsZone 'Microsoft.Network/dnszones@2018-05-01' = {
202-
name: 'myZone'
203-
location: 'global'
204-
}
205-
206-
resource otherResource 'Microsoft.Example/examples@2020-06-01' = {
207-
name: 'exampleResource'
208-
properties: {
209-
// get read-only DNS zone property
210-
nameServers: dnsZone.properties.nameServers
211-
}
212-
}
213-
```
214-
215-
A nested resource also has an implicit dependency on its containing resource.
216-
217-
```bicep
218-
resource myParent 'My.Rp/parentType@2020-01-01' = {
219-
name: 'myParent'
220-
location: 'West US'
221-
222-
// depends on 'myParent' implicitly
223-
resource myChild 'childType' = {
224-
name: 'myChild'
225-
}
226-
}
227-
```
228-
229-
When an implicit dependency exists, **don't add an explicit dependency**.
230-
231-
For more information about nested resources, see [Set name and type for child resources in Bicep](./child-resource-name-type.md).
232-
233-
### Explicit dependency
234-
235-
An explicit dependency is declared with the `dependsOn` property. The property accepts an array of resource identifiers, so you can specify more than one dependency.
236-
237-
The following example shows a DNS zone named `otherZone` that depends on a DNS zone named `dnsZone`:
238-
239-
```bicep
240-
resource dnsZone 'Microsoft.Network/dnszones@2018-05-01' = {
241-
name: 'demoeZone1'
242-
location: 'global'
243-
}
244-
245-
resource otherZone 'Microsoft.Network/dnszones@2018-05-01' = {
246-
name: 'demoZone2'
247-
location: 'global'
248-
dependsOn: [
249-
dnsZone
250-
]
251-
}
252-
```
253-
254-
While you may be inclined to use `dependsOn` to map relationships between your resources, it's important to understand why you're doing it. For example, to document how resources are interconnected, `dependsOn` isn't the right approach. You can't query which resources were defined in the `dependsOn` element after deployment. Setting unnecessary dependencies slows deployment time because Resource Manager can't deploy those resources in parallel.
255-
256-
Even though explicit dependencies are sometimes required, the need for them is rare. In most cases, you can use a symbolic name to imply the dependency between resources. If you find yourself setting explicit dependencies, you should consider if there's a way to remove it.
257-
258-
### Visualize dependencies
259-
260-
Visual Studio Code provides a tool for visualizing the dependencies. Open a Bicep file in Visual Studio Code, and then select the visualizer button on the upper left corner. The following screenshot shows the dependencies of a virtual machine.
261-
262-
:::image type="content" source="./media/resource-declaration/bicep-resource-visualizer.png" alt-text="Screenshot of Visual Studio Code Bicep resource visualizer":::
263-
264-
## Existing resources
265-
266-
To reference a resource that's outside of the current Bicep file, use the `existing` keyword in a resource declaration.
267-
268-
When using the `existing` keyword, provide the `name` of the resource. The following example gets an existing storage account in the same resource group as the current deployment.
269-
270-
```bicep
271-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
272-
name: 'examplestorage'
273-
}
274-
275-
output blobEndpoint string = stg.properties.primaryEndpoints.blob
276-
```
277-
278-
Optionally, you can set the `scope` property to access a resource in a different scope. The following example references an existing storage account in a different resource group.
279-
280-
```bicep
281-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
282-
name: 'examplestorage'
283-
scope: resourceGroup(exampleRG)
284-
}
285-
286-
output blobEndpoint string = stg.properties.primaryEndpoints.blob
287-
```
288-
289-
If you attempt to reference a resource that doesn't exist, you get the `NotFound` error and your deployment fails.
290-
291-
For more information about setting the scope, see [Scope functions for Bicep](bicep-functions-scope.md).
292-
293-
The preceding examples don't deploy the storage account. Instead, you can access properties on the existing resource by using the symbolic name.
294-
295188
## Next steps
296189

297190
- To conditionally deploy a resource, see [Conditional deployment in Bicep](./conditional-resource-deployment.md).
191+
- To reference an existing resource, see [Existing resources in Bicep](existing-resource.md).
192+
- To learn about how deployment order is determined, see [Resource dependencies in Bicep](resource-dependencies.md).
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: Set resource dependencies in Bicep
3+
description: Describes how to specify the order resources are deployed.
4+
ms.topic: conceptual
5+
ms.date: 02/04/2022
6+
---
7+
8+
# Resource dependencies in Bicep
9+
10+
When deploying resources, you may need to make sure some resources are deployed before other resources. For example, you need a logical SQL server before deploying a database. You establish this relationship by marking one resource as dependent on the other resource. The order of resource deployment is determined in two ways: [implicit dependency](#implicit-dependency) and [explicit dependency](#explicit-dependency)
11+
12+
Azure Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. When resources aren't dependent on each other, Resource Manager deploys them in parallel. You only need to define dependencies for resources that are deployed in the same Bicep file.
13+
14+
## Implicit dependency
15+
16+
An implicit dependency is created when one resource declaration references another resource in the same deployment. In the following example, `otherResource` gets a property from `exampleDnsZone`. The resource named `otherResource` is implicitly dependent on `exampleDnsZone`.
17+
18+
```bicep
19+
resource exampleDnsZone 'Microsoft.Network/dnszones@2018-05-01' = {
20+
name: 'myZone'
21+
location: 'global'
22+
}
23+
24+
resource otherResource 'Microsoft.Example/examples@2020-06-01' = {
25+
name: 'exampleResource'
26+
properties: {
27+
// get read-only DNS zone property
28+
nameServers: exampleDnsZone.properties.nameServers
29+
}
30+
}
31+
```
32+
33+
A nested resource also has an implicit dependency on its containing resource.
34+
35+
```bicep
36+
resource myParent 'My.Rp/parentType@2020-01-01' = {
37+
name: 'myParent'
38+
location: 'West US'
39+
40+
// implicit dependency on 'myParent'
41+
resource myChild 'childType' = {
42+
name: 'myChild'
43+
}
44+
}
45+
```
46+
47+
When an implicit dependency exists, **don't add an explicit dependency**.
48+
49+
For more information about nested resources, see [Set name and type for child resources in Bicep](./child-resource-name-type.md).
50+
51+
## Explicit dependency
52+
53+
An explicit dependency is declared with the `dependsOn` property. The property accepts an array of resource identifiers, so you can specify more than one dependency.
54+
55+
The following example shows a DNS zone named `otherZone` that depends on a DNS zone named `dnsZone`:
56+
57+
```bicep
58+
resource dnsZone 'Microsoft.Network/dnszones@2018-05-01' = {
59+
name: 'demoeZone1'
60+
location: 'global'
61+
}
62+
63+
resource otherZone 'Microsoft.Network/dnszones@2018-05-01' = {
64+
name: 'demoZone2'
65+
location: 'global'
66+
dependsOn: [
67+
dnsZone
68+
]
69+
}
70+
```
71+
72+
While you may be inclined to use `dependsOn` to map relationships between your resources, it's important to understand why you're doing it. For example, to document how resources are interconnected, `dependsOn` isn't the right approach. You can't query which resources were defined in the `dependsOn` element after deployment. Setting unnecessary dependencies slows deployment time because Resource Manager can't deploy those resources in parallel.
73+
74+
Even though explicit dependencies are sometimes required, the need for them is rare. In most cases, you can use a symbolic name to imply the dependency between resources. If you find yourself setting explicit dependencies, you should consider if there's a way to remove it.
75+
76+
## Visualize dependencies
77+
78+
Visual Studio Code provides a tool for visualizing the dependencies. Open a Bicep file in Visual Studio Code, and select the visualizer button on the upper left corner. The following screenshot shows the dependencies of a virtual machine.
79+
80+
:::image type="content" source="./media/resource-dependencies/bicep-resource-visualizer.png" alt-text="Screenshot of Visual Studio Code Bicep resource visualizer":::
81+
82+
## Next steps
83+
84+
For the syntax to deploy a resource, see [Resource declaration in Bicep](resource-declaration.md).

articles/azure-resource-manager/bicep/scenarios-secrets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ When you deploy your Azure resources by using a pipeline, you need to take care
9393
- [Azure Key Vault](../../key-vault/general/overview.md)
9494
- Bicep features
9595
- [Secure parameters](parameters.md#secure-parameters)
96-
- [Referencing existing resources](resource-declaration.md#existing-resources)
96+
- [Referencing existing resources](existing-resource.md)
9797
- [`getSecret` function](bicep-functions-resource.md#getsecret)
9898
- Quickstart templates
9999
- [Create a user-assigned managed identity and role assignments](https://github.com/Azure/azure-quickstart-templates/tree/master/modules/Microsoft.ManagedIdentity/user-assigned-identity-role-assignment/1.0)

articles/azure-resource-manager/bicep/scenarios-virtual-networks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ When you redeploy the same Bicep file, the same deployment sequence occurs. Howe
3131

3232
### Access subnet resource IDs
3333

34-
You often need to refer to a subnet's resource ID. When you use the `subnets` property to define your subnet, [you can use the `existing` keyword](resource-declaration.md#existing-resources) to also obtain a strongly typed reference to the subnet, and then access the subnet's `id` property:
34+
You often need to refer to a subnet's resource ID. When you use the `subnets` property to define your subnet, [you can use the `existing` keyword](existing-resource.md) to also obtain a strongly typed reference to the subnet, and then access the subnet's `id` property:
3535

3636
::: code language="bicep" source="~/azure-docs-bicep-samples/samples/scenarios-virtual-networks/vnet.bicep" range="7-42" highlight="26-28, 30-32, 35-36" :::
3737

0 commit comments

Comments
 (0)