|
1 | 1 | ---
|
2 | 2 | title: Declare resources in Bicep
|
3 | 3 | description: Describes how to declare resources to deploy in Bicep.
|
4 |
| -author: mumian |
5 |
| -ms.author: jgao |
6 | 4 | ms.topic: conceptual
|
7 |
| -ms.date: 11/12/2021 |
| 5 | +ms.date: 02/04/2022 |
8 | 6 | ---
|
9 | 7 |
|
10 | 8 | # Resource declaration in Bicep
|
@@ -187,111 +185,8 @@ resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
|
187 | 185 | }
|
188 | 186 | ```
|
189 | 187 |
|
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 |
| - |
295 | 188 | ## Next steps
|
296 | 189 |
|
297 | 190 | - 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). |
0 commit comments