Skip to content

Commit 0f8303d

Browse files
Merge pull request #237969 from mumian/0512-condition-existing
update the condition sample
2 parents 5799818 + 3b15ade commit 0f8303d

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: mumian
66
ms.author: jgao
77
ms.topic: conceptual
88
ms.custom: devx-track-bicep
9-
ms.date: 07/30/2021
9+
ms.date: 05/12/2023
1010
---
1111

1212
# Conditional deployment in Bicep
@@ -59,21 +59,23 @@ param location string = resourceGroup().location
5959
])
6060
param newOrExisting string = 'new'
6161
62-
resource sa 'Microsoft.Storage/storageAccounts@2019-06-01' = if (newOrExisting == 'new') {
62+
resource saNew 'Microsoft.Storage/storageAccounts@2022-09-01' = if (newOrExisting == 'new') {
6363
name: storageAccountName
6464
location: location
6565
sku: {
6666
name: 'Standard_LRS'
67-
tier: 'Standard'
6867
}
6968
kind: 'StorageV2'
70-
properties: {
71-
accessTier: 'Hot'
72-
}
7369
}
70+
71+
resource saExisting 'Microsoft.Storage/storageAccounts@2022-09-01' existing = if (newOrExisting == 'existing') {
72+
name: storageAccountName
73+
}
74+
75+
output storageAccountId string = ((newOrExisting == 'new') ? saNew.id : saExisting.id)
7476
```
7577

76-
When the parameter `newOrExisting` is set to **new**, the condition evaluates to true. The storage account is deployed. However, when `newOrExisting` is set to **existing**, the condition evaluates to false and the storage account isn't deployed.
78+
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.
7779

7880
## Runtime functions
7981

@@ -86,7 +88,7 @@ param vmName string
8688
param location string
8789
param logAnalytics string = ''
8890
89-
resource vmName_omsOnboarding 'Microsoft.Compute/virtualMachines/extensions@2017-03-30' = if (!empty(logAnalytics)) {
91+
resource vmName_omsOnboarding 'Microsoft.Compute/virtualMachines/extensions@2023-03-01'' = if (!empty(logAnalytics)) {
9092
name: '${vmName}/omsOnboarding'
9193
location: location
9294
properties: {
@@ -95,10 +97,10 @@ resource vmName_omsOnboarding 'Microsoft.Compute/virtualMachines/extensions@2017
9597
typeHandlerVersion: '1.0'
9698
autoUpgradeMinorVersion: true
9799
settings: {
98-
workspaceId: ((!empty(logAnalytics)) ? reference(logAnalytics, '2015-11-01-preview').customerId : null)
100+
workspaceId: ((!empty(logAnalytics)) ? reference(logAnalytics, '2022-10-01').customerId : null)
99101
}
100102
protectedSettings: {
101-
workspaceKey: ((!empty(logAnalytics)) ? listKeys(logAnalytics, '2015-11-01-preview').primarySharedKey : null)
103+
workspaceKey: ((!empty(logAnalytics)) ? listKeys(logAnalytics, '2022-10-01').primarySharedKey : null)
102104
}
103105
}
104106
}

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Conditional deployment with templates
33
description: Describes how to conditionally deploy a resource in an Azure Resource Manager template (ARM template).
44
ms.topic: conceptual
55
ms.custom: devx-track-arm-template
6-
ms.date: 01/19/2022
6+
ms.date: 05/12/2023
77
---
88

99
# Conditional deployment in ARM templates
@@ -69,28 +69,36 @@ You can use conditional deployment to create a new resource or use an existing o
6969
]
7070
}
7171
},
72-
"functions": [],
73-
"resources": [
74-
{
72+
"resources": {
73+
"saNew": {
7574
"condition": "[equals(parameters('newOrExisting'), 'new')]",
7675
"type": "Microsoft.Storage/storageAccounts",
77-
"apiVersion": "2019-06-01",
76+
"apiVersion": "2022-09-01",
7877
"name": "[parameters('storageAccountName')]",
7978
"location": "[parameters('location')]",
8079
"sku": {
81-
"name": "Standard_LRS",
82-
"tier": "Standard"
80+
"name": "Standard_LRS"
8381
},
84-
"kind": "StorageV2",
85-
"properties": {
86-
"accessTier": "Hot"
87-
}
82+
"kind": "StorageV2"
83+
},
84+
"saExisting": {
85+
"condition": "[equals(parameters('newOrExisting'), 'existing')]",
86+
"existing": true,
87+
"type": "Microsoft.Storage/storageAccounts",
88+
"apiVersion": "2022-09-01",
89+
"name": "[parameters('storageAccountName')]"
8890
}
89-
]
91+
},
92+
"outputs": {
93+
"storageAccountId": {
94+
"type": "string",
95+
"value": "[if(equals(parameters('newOrExisting'), 'new'), resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')))]"
96+
}
97+
}
9098
}
9199
```
92100

93-
When the parameter `newOrExisting` is set to **new**, the condition evaluates to true. The storage account is deployed. However, when `newOrExisting` is set to **existing**, the condition evaluates to false and the storage account isn't deployed.
101+
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.
94102

95103
For a complete example template that uses the `condition` element, see [VM with a new or existing Virtual Network, Storage, and Public IP](https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/vm-new-or-existing-conditions).
96104

0 commit comments

Comments
 (0)