Skip to content

Commit 956cdad

Browse files
authored
Merge pull request #190215 from tfitzmac/0301accessor
add nested accessor to comparison
2 parents b1aea40 + c138d0b commit 956cdad

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

articles/azure-resource-manager/bicep/compare-template-syntax.md

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Compares Azure Resource Manager templates developed with JSON and B
44
author: mumian
55
ms.author: jgao
66
ms.topic: conceptual
7-
ms.date: 01/21/2022
7+
ms.date: 03/01/2022
88
---
99
# Comparing JSON and Bicep for templates
1010

@@ -37,62 +37,62 @@ func()
3737
To declare a parameter with a default value:
3838

3939
```bicep
40-
param demoParam string = 'Contoso'
40+
param orgName string = 'Contoso'
4141
```
4242

4343
```json
4444
"parameters": {
45-
"demoParam": {
45+
"orgName": {
4646
"type": "string",
4747
"defaultValue": "Contoso"
4848
}
4949
}
5050
```
5151

52-
To get a parameter value:
52+
To get a parameter value, use the name you defined:
5353

5454
```bicep
55-
demoParam
55+
name: orgName
5656
```
5757

5858
```json
59-
[parameters('demoParam'))]
59+
"name": "[parameters('orgName'))]"
6060
```
6161

6262
## Variables
6363

6464
To declare a variable:
6565

6666
```bicep
67-
var demoVar = 'example value'
67+
var description = 'example value'
6868
```
6969

7070
```json
7171
"variables": {
72-
"demoVar": "example value"
72+
"description": "example value"
7373
},
7474
```
7575

76-
To get a variable value:
76+
To get a variable value, use the name you defined:
7777

7878
```bicep
79-
demoVar
79+
workloadSetting: description
8080
```
8181

8282
```json
83-
[variables('demoVar'))]
83+
"workloadSetting": "[variables('demoVar'))]"
8484
```
8585

8686
## Strings
8787

8888
To concatenate strings:
8989

9090
```bicep
91-
'${namePrefix}-vm'
91+
name: '${namePrefix}-vm'
9292
```
9393

9494
```json
95-
[concat(parameters('namePrefix'), '-vm')]
95+
"name": "[concat(parameters('namePrefix'), '-vm')]"
9696
```
9797

9898
## Logical operators
@@ -134,7 +134,7 @@ targetScope = 'subscription'
134134
To declare a resource:
135135

136136
```bicep
137-
resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = {
137+
resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-06-01' = {
138138
...
139139
}
140140
```
@@ -152,7 +152,7 @@ resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = {
152152
To conditionally deploy a resource:
153153

154154
```bicep
155-
resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = if(deployVM) {
155+
resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-06-01' = if(deployVM) {
156156
...
157157
}
158158
```
@@ -193,15 +193,15 @@ nic1.id
193193
To iterate over items in an array or count:
194194

195195
```bicep
196-
[for storageName in storageAccounts: {
196+
[for storageName in storageAccountNames: {
197197
...
198198
}]
199199
```
200200

201201
```json
202202
"copy": {
203203
"name": "storagecopy",
204-
"count": "[length(parameters('storageAccounts'))]"
204+
"count": "[length(parameters('storageAccountNames'))]"
205205
},
206206
...
207207
```
@@ -213,7 +213,7 @@ For Bicep, you can set an explicit dependency but this approach isn't recommende
213213
The following shows a network interface with an implicit dependency on a network security group. It references the network security group with `nsg.id`.
214214

215215
```bicep
216-
resource nsg 'Microsoft.Network/networkSecurityGroups@2020-06-01' = {
216+
resource netSecurityGroup 'Microsoft.Network/networkSecurityGroups@2020-06-01' = {
217217
...
218218
}
219219
@@ -232,7 +232,7 @@ resource nic1 'Microsoft.Network/networkInterfaces@2020-06-01' = {
232232
If you must set an explicit dependence, use:
233233

234234
```bicep
235-
dependsOn: [ stg ]
235+
dependsOn: [ storageAccount ]
236236
```
237237

238238
```json
@@ -244,29 +244,41 @@ dependsOn: [ stg ]
244244
To get a property from a resource in the template:
245245

246246
```bicep
247-
diagsAccount.properties.primaryEndpoints.blob
247+
storageAccount.properties.primaryEndpoints.blob
248248
```
249249

250250
```json
251-
[reference(resourceId('Microsoft.Storage/storageAccounts', variables('diagStorageAccountName'))).primaryEndpoints.blob]
251+
[reference(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))).primaryEndpoints.blob]
252252
```
253253

254254
To get a property from an existing resource that isn't deployed in the template:
255255

256256
```bicep
257-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
257+
resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
258258
name: storageAccountName
259259
}
260260
261261
// use later in template as often as needed
262-
stg.properties.primaryEndpoints.blob
262+
storageAccount.properties.primaryEndpoints.blob
263263
```
264264

265265
```json
266266
// required every time the property is needed
267267
"[reference(resourceId('Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2019-06-01').primaryEndpoints.blob]"
268268
```
269269

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+
270282
## Outputs
271283

272284
To output a property from a resource in the template:
@@ -300,7 +312,7 @@ output hostname string = condition ? publicIP.properties.dnsSettings.fqdn : ''
300312
}
301313
```
302314

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.
304316

305317
## Code reuse
306318

0 commit comments

Comments
 (0)