@@ -4,7 +4,7 @@ description: Compares Azure Resource Manager templates developed with JSON and B
4
4
author : mumian
5
5
ms.author : jgao
6
6
ms.topic : conceptual
7
- ms.date : 01/21 /2022
7
+ ms.date : 03/01 /2022
8
8
---
9
9
# Comparing JSON and Bicep for templates
10
10
@@ -37,62 +37,62 @@ func()
37
37
To declare a parameter with a default value:
38
38
39
39
``` bicep
40
- param demoParam string = 'Contoso'
40
+ param orgName string = 'Contoso'
41
41
```
42
42
43
43
``` json
44
44
"parameters" : {
45
- "demoParam " : {
45
+ "orgName " : {
46
46
"type" : " string" ,
47
47
"defaultValue" : " Contoso"
48
48
}
49
49
}
50
50
```
51
51
52
- To get a parameter value:
52
+ To get a parameter value, use the name you defined :
53
53
54
54
``` bicep
55
- demoParam
55
+ name: orgName
56
56
```
57
57
58
58
``` json
59
- [parameters('demoParam ')) ]
59
+ "name" : " [parameters('orgName '))]"
60
60
```
61
61
62
62
## Variables
63
63
64
64
To declare a variable:
65
65
66
66
``` bicep
67
- var demoVar = 'example value'
67
+ var description = 'example value'
68
68
```
69
69
70
70
``` json
71
71
"variables" : {
72
- "demoVar " : " example value"
72
+ "description " : " example value"
73
73
},
74
74
```
75
75
76
- To get a variable value:
76
+ To get a variable value, use the name you defined :
77
77
78
78
``` bicep
79
- demoVar
79
+ workloadSetting: description
80
80
```
81
81
82
82
``` json
83
- [variables('demoVar')) ]
83
+ "workloadSetting" : " [variables('demoVar'))]"
84
84
```
85
85
86
86
## Strings
87
87
88
88
To concatenate strings:
89
89
90
90
``` bicep
91
- '${namePrefix}-vm'
91
+ name: '${namePrefix}-vm'
92
92
```
93
93
94
94
``` json
95
- [concat(parameters('namePrefix'), '-vm') ]
95
+ "name" : " [concat(parameters('namePrefix'), '-vm')]"
96
96
```
97
97
98
98
## Logical operators
@@ -134,7 +134,7 @@ targetScope = 'subscription'
134
134
To declare a resource:
135
135
136
136
``` bicep
137
- resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = {
137
+ resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-06-01' = {
138
138
...
139
139
}
140
140
```
@@ -152,7 +152,7 @@ resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = {
152
152
To conditionally deploy a resource:
153
153
154
154
``` bicep
155
- resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = if(deployVM) {
155
+ resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-06-01' = if(deployVM) {
156
156
...
157
157
}
158
158
```
@@ -193,15 +193,15 @@ nic1.id
193
193
To iterate over items in an array or count:
194
194
195
195
``` bicep
196
- [for storageName in storageAccounts : {
196
+ [for storageName in storageAccountNames : {
197
197
...
198
198
}]
199
199
```
200
200
201
201
``` json
202
202
"copy" : {
203
203
"name" : " storagecopy" ,
204
- "count" : " [length(parameters('storageAccounts '))]"
204
+ "count" : " [length(parameters('storageAccountNames '))]"
205
205
},
206
206
...
207
207
```
@@ -213,7 +213,7 @@ For Bicep, you can set an explicit dependency but this approach isn't recommende
213
213
The following shows a network interface with an implicit dependency on a network security group. It references the network security group with ` nsg.id ` .
214
214
215
215
``` bicep
216
- resource nsg 'Microsoft.Network/networkSecurityGroups@2020-06-01' = {
216
+ resource netSecurityGroup 'Microsoft.Network/networkSecurityGroups@2020-06-01' = {
217
217
...
218
218
}
219
219
@@ -232,7 +232,7 @@ resource nic1 'Microsoft.Network/networkInterfaces@2020-06-01' = {
232
232
If you must set an explicit dependence, use:
233
233
234
234
``` bicep
235
- dependsOn: [ stg ]
235
+ dependsOn: [ storageAccount ]
236
236
```
237
237
238
238
``` json
@@ -244,29 +244,41 @@ dependsOn: [ stg ]
244
244
To get a property from a resource in the template:
245
245
246
246
``` bicep
247
- diagsAccount .properties.primaryEndpoints.blob
247
+ storageAccount .properties.primaryEndpoints.blob
248
248
```
249
249
250
250
``` json
251
- [reference(resourceId('Microsoft.Storage/storageAccounts', variables('diagStorageAccountName '))).primaryEndpoints.blob ]
251
+ [reference(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName '))).primaryEndpoints.blob ]
252
252
```
253
253
254
254
To get a property from an existing resource that isn't deployed in the template:
255
255
256
256
``` bicep
257
- resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
257
+ resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
258
258
name: storageAccountName
259
259
}
260
260
261
261
// use later in template as often as needed
262
- stg .properties.primaryEndpoints.blob
262
+ storageAccount .properties.primaryEndpoints.blob
263
263
```
264
264
265
265
``` json
266
266
// required every time the property is needed
267
267
" [reference(resourceId('Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2019-06-01').primaryEndpoints.blob]"
268
268
```
269
269
270
+ In Bicep, use the [ nested accessor] ( operators-access.md#nested-resource-accessor ) (` :: ` ) to get a property on a resource nested within a parent resource:
271
+
272
+ ``` bicep
273
+ VNet1::Subnet1.properties.addressPrefix
274
+ ```
275
+
276
+ For JSON, use reference function:
277
+
278
+ ``` json
279
+ [reference(resourceId('Microsoft.Network/virtualNetworks/subnets', variables('subnetName'))).properties.addressPrefix ]
280
+ ```
281
+
270
282
## Outputs
271
283
272
284
To output a property from a resource in the template:
@@ -300,7 +312,7 @@ output hostname string = condition ? publicIP.properties.dnsSettings.fqdn : ''
300
312
}
301
313
```
302
314
303
- The Bicep ternary operator is the equivalent to the [ ` if ` function] ( ../templates/template-functions-logical.md#if ) in an ARM template JSON, not the condition property. The ternary syntax has to evaluate to one value or the other. If the condition is false in the preceding samples, Bicep outputs a hostname with an empty string, but JSON outputs no values.
315
+ The Bicep ternary operator is the equivalent to the [ if function] ( ../templates/template-functions-logical.md#if ) in an ARM template JSON, not the condition property. The ternary syntax has to evaluate to one value or the other. If the condition is false in the preceding samples, Bicep outputs a hostname with an empty string, but JSON outputs no values.
304
316
305
317
## Code reuse
306
318
0 commit comments