Loop over created resources #5326
-
Here's a summary of what I'm hoping to achieve:
Here is the code I'm successfully using to :
param location string = resourceGroup().location
// reference existing resources
resource logWorkspace 'Microsoft.OperationalInsights/workspaces@2021-06-01' existing = {
name: 'log-testing-dev'
}
resource serverFarm 'Microsoft.Web/serverfarms@2021-02-01' existing = {
name: 'plan-testing-dev'
}
// deploy resources
resource appServiceApp 'Microsoft.Web/sites@2021-02-01' = {
name: 'app-testing${count}-dev'
kind: 'linux'
location: location
properties: {
httpsOnly: true
reserved: true
serverFarmId: serverFarm.id
siteConfig: {
appSettings: [
{
name: 'WEBSITE_HTTPLOGGING_RETENTION_DAYS'
value: '30'
}
]
httpLoggingEnabled: true
}
}
}
resource appServiceAppDiags 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
scope: appServiceApp
name: 'default'
properties: {
workspaceId: logWorkspace.id
logs: [
{
categoryGroup: 'audit'
enabled: true
}
]
}
} I then add a loop based on a count integer to deploy n copies of the same App Service resource: param location string = resourceGroup().location
param appServiceCount int = 2
// reference existing resources
resource logWorkspace 'Microsoft.OperationalInsights/workspaces@2021-06-01' existing = {
name: 'log-testing-dev'
}
resource serverFarm 'Microsoft.Web/serverfarms@2021-02-01' existing = {
name: 'plan-testing-dev'
}
// deploy resources
resource appServiceApp 'Microsoft.Web/sites@2021-02-01' = [for count in range(1, appServiceCount): {
name: 'app-testing${count}-dev'
kind: 'linux'
location: location
properties: {
httpsOnly: true
reserved: true
serverFarmId: serverFarm.id
siteConfig: {
appSettings: [
{
name: 'WEBSITE_HTTPLOGGING_RETENTION_DAYS'
value: '30'
}
]
httpLoggingEnabled: true
}
}
}]
resource appServiceAppDiags 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
scope: appServiceApp
name: 'default'
properties: {
workspaceId: logWorkspace.id
logs: [
{
categoryGroup: 'audit'
enabled: true
}
]
}
} This then returns the following errors in vscode:
I understand that the problem is due to the A workaround of sorts is to loop resource appServiceAppDiags 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [for count in range(0, appServiceCount): {
scope: appServiceApp[count]
name: 'default'
properties: {
workspaceId: logWorkspace.id
logs: [
{
categoryGroup: 'audit'
enabled: true
}
]
}
}] Any and all help would be very gratefully received, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
While it is possible to just pass in X number of resources, it's not very practical to do that.
Another way to think about this is as follows
A few features here:
There is even another way to approach this which would be as follows:
Here is a sample for now:param location string = resourceGroup().location
param WebSites array = [
{
name: 'prod'
farm: 'prod'
installDiags: true
}
{
name: 'dev'
farm: 'dev'
installDiags: false
}
]
// reference existing resources
resource logWorkspace 'Microsoft.OperationalInsights/workspaces@2021-06-01' existing = [for (ws, index) in WebSites: {
name: 'log-testing-${ws.name}'
}]
resource serverFarm 'Microsoft.Web/serverfarms@2021-02-01' existing = [for (ws, index) in WebSites: {
name: 'plan-testing-${ws.farm}'
}]
// deploy resources
resource appServiceApp 'Microsoft.Web/sites@2021-02-01' = [for (ws, index) in WebSites: {
name: 'app-testing-${ws.name}'
kind: 'linux'
location: location
properties: {
httpsOnly: true
reserved: true
serverFarmId: serverFarm[index].id
siteConfig: {
appSettings: [
{
name: 'WEBSITE_HTTPLOGGING_RETENTION_DAYS'
value: '30'
}
]
httpLoggingEnabled: true
}
}
}]
resource appServiceAppDiags 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [for (ws, index) in WebSites: if (ws.installDiags) {
scope: appServiceApp[index]
name: 'default'
properties: {
workspaceId: logWorkspace[index].id
logs: [
{
categoryGroup: 'audit'
enabled: true
}
]
}
}] |
Beta Was this translation helpful? Give feedback.
-
I went back over what I needed to do and it was actually much simpler to move the resources into a module (which was the plan anyway) and then loop through the The module then outputs the URL which is collated in an array that I'll use later to populate Application Gateway pools. This array could be used for anything, like deploying other resources/modules based on ouputs or simply the number of resources deployed. // File: main.bicep
param appServiceCount int = 2
param diagsEnabled bool = true
param location string = resourceGroup().location
// reference existing resources
resource logWorkspace 'Microsoft.OperationalInsights/workspaces@2021-06-01' existing = {
name: 'log-testing-dev'
}
resource serverFarm 'Microsoft.Web/serverfarms@2021-02-01' existing = {
name: 'plan-testing-dev'
}
// deploy module n times
module appServiceModule 'appService.bicep' = [for count in range(1, appServiceCount): {
name: 'appServiceDeploy${count}'
params: {
appServiceName: 'app-testing${count}-dev'
appServiceFarmId: serverFarm.id
appServiceKind: appServiceParams.kind
diagsEnabled: diagsEnabled
logWorkspaceId: logWorkspace.id
}
}]
// create array of appServiceModule outputs for n module deployments
output appServiceApps array = [for index in (range(0, appServiceCount)): {
'appServiceUrl': appServiceModule[index].outputs.appServiceUrl
}]
// create outputs from main.bicep
output logWorkspaceId string = logWorkspace.id
output namespaceId string = hubNamespace.id
output serverFarmId string = serverFarm.id Here's the module: // File: appService.bicep
param appServiceName string
param appServiceFarmId string
param appServiceKind string = 'linux'
param diagsEnabled bool = false
param location string = resourceGroup().location
param logWorkspaceId string = ''
resource appServiceApp 'Microsoft.Web/sites@2021-02-01' = {
name: appServiceName
kind: appServiceKind
identity: {
type: 'SystemAssigned'
}
location: location
properties: {
httpsOnly: true
reserved: true
serverFarmId: appServiceFarmId
siteConfig: {
appSettings: [
{
name: 'WEBSITE_HTTPLOGGING_RETENTION_DAYS'
value: '30'
}
]
ftpsState: 'Disabled'
httpLoggingEnabled: true
}
}
}
resource appServiceAppDiags 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = if (diagsEnabled) {
scope: appServiceApp
name: 'default'
properties: {
workspaceId: logWorkspaceId
logs: [
{
categoryGroup: 'audit'
enabled: true
}
]
}
}
output appServiceUrl string = appServiceApp.properties.hostNames[0] Outputs: {
"outputs": {
"appServiceApps": {
"type": "Array",
"value": [
{
"appServiceUrl": "app-testing1-dev.azurewebsites.net"
},
{
"appServiceUrl": "app-testing2-dev.azurewebsites.net"
}
]
},
"logWorkspaceId": {
"type": "String",
"value": "/subscriptions/*******-****-****-****-************/resourceGroups/rg-log-dev/providers/Microsoft.OperationalInsights/workspaces/log-log-dev"
},
"serverFarmId": {
"type": "String",
"value": "/subscriptions/*******-****-****-****-************/resourceGroups/rg-app-dev/providers/Microsoft.Web/serverfarms/plan-testing-dev"
}
}
} |
Beta Was this translation helpful? Give feedback.
-
An older post, I know, but just came across this and wanted to share my example. I deploy a bunch of Private DNS Zones and then private dns zone links. Items in arrays in bicep seem to always be in the same order so I iterate this way: var privateDnsZonesList = [
'privatelink.monitor.azure.com'
'privatelink.oms.opinsights.azure.com'
'privatelink.ods.opinsights.azure.com'
'privatelink.agentsvc.azure-automation.net'
'privatelink.blob.core.windows.net'
'privatelink.vault.azure.net'
'privatelink.azurewebsites.net'
'privatelink.scm.azurewebsites.net'
]
@batchSize(1)
resource privateDnsZones 'Microsoft.Network/privateDnsZones@2020-06-01' = [
for zone in privateDnsZonesList: {
name: zone
location: resourceGroup().location
}
]
resource privateDnaZonesVirtualNetworkLink 'Microsoft.Network/privateDnsZones/virtualNetworkLinks@2020-06-01' = [
for i in range(0, length(privateDnsZonesList)): {
name: guid('${vnet.name}-${privateDnsZones[i].name}-vnl')
parent: privateDnsZones[i]
location: 'global'
properties: {
registrationEnabled: false
virtualNetwork: {
id: vnet.id
}
}
}
] I hope this helps someone. The ability to iterate through a list of deployment objects would be nice anyway. |
Beta Was this translation helpful? Give feedback.
While it is possible to just pass in X number of resources, it's not very practical to do that.
Another way to think about this is as follows
range()
A few features here:
There is even another way to approach this which would be as follows: