Created
August 8, 2021 17:32
-
-
Save Ba4bes/0fe32d4eeb17995884afa2af45dc61f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// examples for https://4bes.nl/2021/08/08/use-for-loops-in-bicep/ | |
// A single storage account | |
resource oneSta 'Microsoft.Storage/storageAccounts@2021-04-01' = { | |
name: '4besuniquevalue' | |
location: resourceGroup().location | |
sku: { | |
name: 'Standard_LRS' | |
} | |
kind: 'StorageV2' | |
} | |
// A numbered loop | |
resource twoSta 'Microsoft.Storage/storageAccounts@2021-04-01' = [for i in range(0,2): { | |
name: '4besuniquevalue${i}' | |
location: resourceGroup().location | |
sku: { | |
name: 'Standard_LRS' | |
} | |
kind: 'StorageV2' | |
}] | |
// A numbered loop that starts with a higher index number | |
resource threeSta 'Microsoft.Storage/storageAccounts@2021-04-01' = [for i in range(2,3): { | |
name: '4besuniquevalue${padLeft(i,2,'0')}' | |
location: resourceGroup().location | |
sku: { | |
name: 'Standard_LRS' | |
} | |
kind: 'StorageV2' | |
}] | |
// A loop based on an array of strings | |
param storageAccountNames array = [ | |
'4besarraystorage' | |
'4besanotherstorage' | |
] | |
resource arrayStorageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [for storageAccountName in storageAccountNames: { | |
name: storageAccountName | |
location: resourceGroup().location | |
sku: { | |
name: 'Standard_LRS' | |
} | |
kind: 'StorageV2' | |
}] | |
// a loop based on an array of objects | |
param storageAccounts array = [ | |
{ | |
name: '4besstandardlrs' | |
skuName: 'Standard_LRS' | |
} | |
{ | |
name: '4bespremiumlrs' | |
skuName: 'Premium_LRS' | |
} | |
] | |
resource objectarrayStorageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [for storageAccount in storageAccounts: { | |
name: storageAccount.name | |
location: resourceGroup().location | |
sku: { | |
name: storageAccount.skuName | |
} | |
kind: 'StorageV2' | |
}] | |
// a loop based on an array of very extended objects | |
param completestorageAccounts array = [ | |
{ | |
name: '4besstwithproperties1' | |
location: 'westeurope' | |
sku: { | |
name: 'Standard_LRS' | |
} | |
kind: 'StorageV2' | |
properties: { | |
supportsHttpsTrafficOnly: true | |
minimumTlsVersion: 'TLS1_2' | |
} | |
} | |
{ | |
name: '4besstwithproperties2' | |
location: 'northeurope' | |
sku: { | |
name: 'Premium_LRS' | |
} | |
kind: 'StorageV2' | |
properties: { | |
supportsHttpsTrafficOnly: true | |
minimumTlsVersion: 'TLS1_0' | |
} | |
} | |
] | |
resource completearrayStorageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [for storageAccount in completestorageAccounts: { | |
name: storageAccount.name | |
location: storageAccount.location | |
sku: storageAccount.sku | |
kind: 'StorageV2' | |
properties: storageAccount.properties | |
}] | |
// A loop with both an array and indexnumbers | |
param storageAccountlocations array = [ | |
'west europe' | |
'north europe' | |
] | |
resource arraynumberStorageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [for (storageAccountlocation, i) in storageAccountlocations: { | |
name: '4besarrayandnr${i}' | |
location: storageAccountlocation | |
sku: { | |
name: 'Standard_LRS' | |
} | |
kind: 'StorageV2' | |
}] | |
// Refer to resources in a loop | |
// This vnet is to support the nic + pip loop example | |
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2021-02-01' = { | |
name: 'examplevnet' | |
location: resourceGroup().location | |
properties: { | |
addressSpace: { | |
addressPrefixes: [ | |
'10.0.0.0/16' | |
] | |
} | |
subnets: [ | |
{ | |
name: 'Subnet-1' | |
properties: { | |
addressPrefix: '10.0.0.0/24' | |
} | |
} | |
] | |
} | |
} | |
resource pips 'Microsoft.Network/publicIPAddresses@2021-02-01' = [for i in range(0, 2): { | |
name: 'pip${i}' | |
location: resourceGroup().location | |
}] | |
resource nics 'Microsoft.Network/networkInterfaces@2021-02-01' = [for i in range(0, 2): { | |
name: 'nic${i}' | |
location: resourceGroup().location | |
properties: { | |
ipConfigurations: [ | |
{ | |
name: 'ipconfig' | |
properties: { | |
publicIPAddress: { | |
id: pips[i].id | |
} | |
subnet: { | |
id: virtualNetwork.properties.subnets[0].id | |
} | |
} | |
} | |
] | |
} | |
}] | |
// Use a loop for properties | |
param nsgRules array = [ | |
{ | |
name: 'allow-rdp-yolo' | |
priority: 200 | |
destinationPortRange: '3389' | |
} | |
{ | |
name: 'allow-https' | |
priority: 210 | |
destinationPortRange: '443' | |
} | |
] | |
resource nsg 'Microsoft.Network/networkSecurityGroups@2021-02-01' = { | |
name: 'examplensg' | |
location: resourceGroup().location | |
properties: { | |
securityRules: [for nsgRule in nsgRules: { | |
name: nsgRule.name | |
properties: { | |
priority: nsgRule.priority | |
sourceAddressPrefix: '*' | |
protocol: 'Tcp' | |
destinationPortRange: nsgRule.destinationPortRange | |
access: 'Allow' | |
direction: 'Inbound' | |
sourcePortRange: '*' | |
destinationAddressPrefix: '*' | |
} | |
}] | |
} | |
} | |
// For loop in a variable | |
param nsgRules array = [ | |
{ | |
name: 'allow-rdp-yolo' | |
priority: 200 | |
destinationPortRange: '3389' | |
} | |
{ | |
name: 'allow-https' | |
priority: 210 | |
destinationPortRange: '443' | |
} | |
] | |
var nsgRulesObjects = [for nsgRule in nsgRules: { | |
name: nsgRule.name | |
properties: { | |
priority: nsgRule.priority | |
sourceAddressPrefix: '*' | |
protocol: 'Tcp' | |
destinationPortRange: nsgRule.destinationPortRange | |
access: 'Allow' | |
direction: 'Inbound' | |
sourcePortRange: '*' | |
destinationAddressPrefix: '*' | |
} | |
}] | |
resource nsg 'Microsoft.Network/networkSecurityGroups@2021-02-01' = { | |
name: 'examplensg' | |
location: resourceGroup().location | |
properties: { | |
securityRules: nsgRulesObjects | |
} | |
} | |
// For loop with a module | |
// Module file: https://github.com/Ba4bes/AzDo-Bicep/blob/main/Modules/storageAccount.bicep | |
module storageAccountsFromModule './storageAccount.bicep' = [for i in range(0, 3): { | |
name: 'module${i}-deploy' | |
params: { | |
storageAccountPrefix: 'mod${i}' | |
tagValues: { | |
'blog': '4bes.nl' | |
} | |
} | |
}] | |
// Output loop | |
output stas array = [for i in range(0,3): { | |
Id: storageAccountsFromModule[i].outputs.staid | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment