Scope function in resource declaration with loop #6598
-
|
Background: We are preparing some modules for an elite project. We would like to know an approach to deploy multiple resources within its corresponding resource group while looping in the resource module. We want to declare the resource group along with the other resource parameters in a Json file. How the module works: ASG Bicep file has a loop that iterates 2 times for creating 2 ASGs. This gets called in the main.bicep and with the given input parameter of 2 ASGs, its deploying 2 ASGs. But the issue is, its getting deployed to the same RG1 from the Resource deployment command that we executed. Command: New-AzResourceGroupDeployment -ResourceGroupName "RG1" -TemplateFile "main.bicep" -TemplateParameterFile "mainparam.json" Requirement: ASG1 and ASG2 must be deployed to 2 different RGs - RG11 and RG22. How can we achieve this WITHOUT MODIFYING MY ASG.BICEP file? Would really appreciate your support on this as we have many modules and modifying them would be tedious task. Modules: sharing below a ASG bicep, main bicep and a parameter file. ASG Bicep file resource applicationSecurityGroup 'Microsoft.Network/applicationSecurityGroups@2021-05-01' = [for asgName in asgNames: { Main Bicep file module asgloop 'az-asg/ASG.bicep' = { Param json file |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
|
You can create your base Modules for any Resource kind. Most of the time this will be a Resource Group scoped deployment. Then you can layer another module on top of that which can start as a Subscription Scoped deployment. However when you call the first module, you scope it to the specific resource group that you want to target. Here is an example of a Subscription scoped deployment. https://github.com/brwilkinson/AzureDeploymentFramework/blob/main/ADF/bicep/sub-RBAC.bicep#L54 module UAI 'sub-RBAC-ALL.bicep' = [for (uai, index) in uaiinfo: if (bool(Stage.UAI) && contains(uai,'RBAC')) {
name: 'dp-rbac-uai-${Prefix}-${uai.name}'
params: {
Deployment: deployment
Prefix: Prefix
rgName: rg
Enviro: enviro
Global: Global
roleInfo: uai
providerPath: 'Microsoft.ManagedIdentity/userAssignedIdentities'
namePrefix: '-uai'
providerAPI: '2018-11-30'
principalType: 'ServicePrincipal'
}
}]This loops through the above and calls the following Module, which then redirects to a specific Resource Group https://github.com/brwilkinson/AzureDeploymentFramework/blob/main/ADF/bicep/sub-RBAC-ALL.bicep#L83 module RBACRARG 'sub-RBAC-ALL-RA-RG.bicep' = [for (rbac, index) in roleAssignment: if (Enviro != 'G0' && Enviro != 'M0') {
name: replace('dp-rbac-all-ra-${roleInfo.name}-${index}','@','_')
scope: resourceGroup(rbac.DestSubscriptionID,'${rbac.DestPrefix}-${Global.OrgName}-${rbac.DestApp}-RG-${rbac.DestRG}')
params:{
description: roleInfo.name
name: rbac.GUID
roledescription: rbac.RoleName
roleDefinitionId: '${rbac.DestSubscription}/providers/Microsoft.Authorization/roleDefinitions/${rbac.RoleID}'
principalType: rbac.principalType
principalId: providerPath == 'guid' ? roleInfo.name : length(providerPath) == 0 ? objectIdLookup[roleInfo.name] : /*
*/ reference('${rbac.DestSubscription}/resourceGroups/${rbac.SourceRG}/providers/${providerPath}/${Deployment}${namePrefix}${roleInfo.Name}',providerAPI).principalId
}
}]Here is another similar example. https://github.com/brwilkinson/AzureDeploymentFramework/blob/main/ADF/bicep/sub-RG.bicep#L62 module UAI 'sub-RG-UAI.bicep' = [for (uai, index) in identity: if (uai.match && bool(Stage.UAI)) {
name: 'dp-uai-${uai.name}'
scope: RG
params: {
uai: uai
deployment: deployment
}
}]You you can switch from sub scope to rg scope. |
Beta Was this translation helpful? Give feedback.
You can create your base Modules for any Resource kind. Most of the time this will be a Resource Group scoped deployment.
Then you can layer another module on top of that which can start as a Subscription Scoped deployment.
However when you call the first module, you scope it to the specific resource group that you want to target.
Here is an example of a Subscription scoped deployment.
https://github.com/brwilkinson/AzureDeploymentFramework/blob/main/ADF/bicep/sub-RBAC.bicep#L54