From 0f69898fdc8a17f3c9e172de92c2df7f48386813 Mon Sep 17 00:00:00 2001 From: "Prekshith D J (Persistent Systems Inc)" Date: Thu, 9 Oct 2025 14:17:15 +0530 Subject: [PATCH] Updated the network module --- infra/main.bicep | 125 ++++++- infra/modules/network.bicep | 251 ------------- infra/modules/network/bastionHost.bicep | 104 ------ infra/modules/network/jumpbox.bicep | 155 -------- infra/modules/network/network-resources.bicep | 104 ------ infra/modules/network/virtualNetwork.bicep | 157 -------- infra/modules/virtualNetwork.bicep | 346 ++++++++++++++++++ 7 files changed, 452 insertions(+), 790 deletions(-) delete mode 100644 infra/modules/network.bicep delete mode 100644 infra/modules/network/bastionHost.bicep delete mode 100644 infra/modules/network/jumpbox.bicep delete mode 100644 infra/modules/network/network-resources.bicep delete mode 100644 infra/modules/network/virtualNetwork.bicep create mode 100644 infra/modules/virtualNetwork.bicep diff --git a/infra/main.bicep b/infra/main.bicep index aab96329b..60eefd36b 100644 --- a/infra/main.bicep +++ b/infra/main.bicep @@ -387,22 +387,109 @@ module sqlUserAssignedIdentity 'br/public:avm/res/managed-identity/user-assigned } } -// ========== Network Module ========== // -module network 'modules/network.bicep' = if (enablePrivateNetworking) { - name: take('network-${solutionSuffix}-deployment', 64) +// ========== Virtual Network and Networking Components ========== // + +// Virtual Network with NSGs and Subnets +module virtualNetwork 'modules/virtualNetwork.bicep' = if (enablePrivateNetworking) { + name: take('module.virtualNetwork.${solutionSuffix}', 64) params: { - resourcesName: solutionSuffix - // logAnalyticsWorkSpaceResourceId: logAnalyticsWorkspace.outputs.resourceId - logAnalyticsWorkSpaceResourceId: logAnalyticsWorkspaceResourceId - vmAdminUsername: vmAdminUsername ?? 'JumpboxAdminUser' - vmAdminPassword: vmAdminPassword ?? 'JumpboxAdminP@ssw0rd1234!' - vmSize: vmSize ?? 'Standard_DS2_v2' // Default VM size +name: 'vnet-${solutionSuffix}' + addressPrefixes: ['10.0.0.0/20'] // 4096 addresses (enough for 8 /23 subnets or 16 /24) location: solutionLocation tags: allTags + logAnalyticsWorkspaceId: logAnalyticsWorkspaceResourceId + resourceSuffix: solutionSuffix + enableTelemetry: enableTelemetry + } +} +// Azure Bastion Host +var bastionHostName = 'bas-${solutionSuffix}' +module bastionHost 'br/public:avm/res/network/bastion-host:0.6.1' = if (enablePrivateNetworking) { + name: take('avm.res.network.bastion-host.${bastionHostName}', 64) + params: { + name: bastionHostName + skuName: 'Standard' + location: solutionLocation + virtualNetworkResourceId: virtualNetwork!.outputs.resourceId + diagnosticSettings: [ + { + name: 'bastionDiagnostics' + workspaceResourceId: logAnalyticsWorkspaceResourceId + logCategoriesAndGroups: [ + { + categoryGroup: 'allLogs' + enabled: true + } + ] + } + ] + tags: tags enableTelemetry: enableTelemetry + publicIPAddressObject: { + name: 'pip-${bastionHostName}' + zones: [] + } } } +// Jumpbox Virtual Machine +var jumpboxVmName = take('vm-jumpbox-${solutionSuffix}', 15) +module jumpboxVM 'br/public:avm/res/compute/virtual-machine:0.15.0' = if (enablePrivateNetworking) { + name: take('avm.res.compute.virtual-machine.${jumpboxVmName}', 64) + params: { + name: take(jumpboxVmName, 15) // Shorten VM name to 15 characters to avoid Azure limits + vmSize: vmSize ?? 'Standard_DS2_v2' + location: solutionLocation + adminUsername: vmAdminUsername ?? 'JumpboxAdminUser' + adminPassword: vmAdminPassword ?? 'JumpboxAdminP@ssw0rd1234!' + tags: tags + zone: 0 + imageReference: { + offer: 'WindowsServer' + publisher: 'MicrosoftWindowsServer' + sku: '2019-datacenter' + version: 'latest' + } + osType: 'Windows' + osDisk: { + name: 'osdisk-${jumpboxVmName}' + managedDisk: { + storageAccountType: 'Standard_LRS' + } + } + encryptionAtHost: false // Some Azure subscriptions do not support encryption at host + nicConfigurations: [ + { + name: 'nic-${jumpboxVmName}' + ipConfigurations: [ + { + name: 'ipconfig1' + subnetResourceId: virtualNetwork!.outputs.jumpboxSubnetResourceId + } + ] + diagnosticSettings: [ + { + name: 'jumpboxDiagnostics' + workspaceResourceId: logAnalyticsWorkspaceResourceId + logCategoriesAndGroups: [ + { + categoryGroup: 'allLogs' + enabled: true + } + ] + metricCategories: [ + { + category: 'AllMetrics' + enabled: true + } + ] + } + ] + } + ] + enableTelemetry: enableTelemetry + } +} // ========== Private DNS Zones ========== // var privateDnsZones = [ 'privatelink.cognitiveservices.azure.com' @@ -456,8 +543,8 @@ module avmPrivateDnsZones 'br/public:avm/res/network/private-dns-zone:0.7.1' = [ enableTelemetry: enableTelemetry virtualNetworkLinks: [ { - name: take('vnetlink-${network!.outputs.vnetName}-${split(zone, '.')[1]}', 80) - virtualNetworkResourceId: network!.outputs.vnetResourceId + name: take('vnetlink-${virtualNetwork!.outputs.name}-${split(zone, '.')[1]}', 80) + virtualNetworkResourceId: virtualNetwork!.outputs.resourceId } ] } @@ -497,7 +584,7 @@ module keyvault 'br/public:avm/res/key-vault/vault:0.12.1' = { ] } service: 'vault' - subnetResourceId: network!.outputs.subnetPrivateEndpointsResourceId + subnetResourceId: virtualNetwork!.outputs.pepsSubnetResourceId } ] : [] @@ -638,7 +725,7 @@ module aiFoundryAiServices 'modules/ai-services.bicep' = if (aiFoundryAIservices { name: 'pep-${aiFoundryAiServicesResourceName}' customNetworkInterfaceName: 'nic-${aiFoundryAiServicesResourceName}' - subnetResourceId: network!.outputs.subnetPrivateEndpointsResourceId + subnetResourceId: virtualNetwork!.outputs.pepsSubnetResourceId privateDnsZoneGroup: { privateDnsZoneGroupConfigs: [ { @@ -745,7 +832,7 @@ module cosmosDb 'br/public:avm/res/document-db/database-account:0.15.0' = { ] } service: 'Sql' - subnetResourceId: network!.outputs.subnetPrivateEndpointsResourceId + subnetResourceId: virtualNetwork!.outputs.pepsSubnetResourceId } ] : [] @@ -818,7 +905,7 @@ module avmStorageAccount 'br/public:avm/res/storage/storage-account:0.20.0' = { } ] } - subnetResourceId: network!.outputs.subnetPrivateEndpointsResourceId + subnetResourceId: virtualNetwork!.outputs.pepsSubnetResourceId service: 'blob' } { @@ -831,7 +918,7 @@ module avmStorageAccount 'br/public:avm/res/storage/storage-account:0.20.0' = { } ] } - subnetResourceId: network!.outputs.subnetPrivateEndpointsResourceId + subnetResourceId: virtualNetwork!.outputs.pepsSubnetResourceId service: 'queue' } ] @@ -940,7 +1027,7 @@ module sqlDBModule 'br/public:avm/res/sql/server:0.20.1' = { ] } service: 'sqlServer' - subnetResourceId: network!.outputs.subnetPrivateEndpointsResourceId + subnetResourceId: virtualNetwork!.outputs.pepsSubnetResourceId tags: tags } ] @@ -1063,7 +1150,7 @@ module webSite 'modules/web-sites.bicep' = { // WAF aligned configuration for Private Networking vnetRouteAllEnabled: enablePrivateNetworking ? true : false vnetImagePullEnabled: enablePrivateNetworking ? true : false - virtualNetworkSubnetId: enablePrivateNetworking ? network!.outputs.subnetWebResourceId : null + virtualNetworkSubnetId: enablePrivateNetworking ? virtualNetwork!.outputs.webSubnetResourceId : null publicNetworkAccess: 'Enabled' } } @@ -1145,7 +1232,7 @@ module searchService 'br/public:avm/res/search/search-service:0.11.1' = { ] } service: 'searchService' - subnetResourceId: network!.outputs.subnetPrivateEndpointsResourceId + subnetResourceId: virtualNetwork!.outputs.pepsSubnetResourceId } ] : [] diff --git a/infra/modules/network.bicep b/infra/modules/network.bicep deleted file mode 100644 index ace0139f1..000000000 --- a/infra/modules/network.bicep +++ /dev/null @@ -1,251 +0,0 @@ -@description('Required. Named used for all resource naming.') -param resourcesName string - -@description('Required. Resource ID of the Log Analytics Workspace for monitoring and diagnostics.') -param logAnalyticsWorkSpaceResourceId string - -@minLength(3) -@description('Required. Azure region for all services.') -param location string - -@description('Optional. Tags to be applied to the resources.') -param tags object = {} - -@description('Optional. Enable/Disable usage telemetry for module.') -param enableTelemetry bool = true - -@description('Required. Admin username for the VM.') -@secure() -param vmAdminUsername string - -@description('Required. Admin password for the VM.') -@secure() -param vmAdminPassword string - -@description('Required. VM size for the Jumpbox VM.') -param vmSize string - - -// VM Size Notes: -// 1 B-series VMs (like Standard_B2ms) do not support accelerated networking. -// 2 Pick a VM size that does support accelerated networking (the usual jump-box candidates): -// Standard_DS2_v2 (2 vCPU, 7 GiB RAM, Premium SSD) // The most broadly available (it’s a legacy SKU supported in virtually every region). -// Standard_D2s_v3 (2 vCPU, 8 GiB RAM, Premium SSD) // next most common -// Standard_D2s_v4 (2 vCPU, 8 GiB RAM, Premium SSD) // Newest, so fewer regions availabl - - -// Subnet Classless Inter-Doman Routing (CIDR) Sizing Reference Table (Best Practices) -// | CIDR | # of Addresses | # of /24s | Notes | -// |-----------|---------------|-----------|----------------------------------------| -// | /24 | 256 | 1 | Smallest recommended for Azure subnets | -// | /23 | 512 | 2 | Good for 1-2 workloads per subnet | -// | /22 | 1024 | 4 | Good for 2-4 workloads per subnet | -// | /21 | 2048 | 8 | | -// | /20 | 4096 | 16 | Used for default VNet in this solution | -// | /19 | 8192 | 32 | | -// | /18 | 16384 | 64 | | -// | /17 | 32768 | 128 | | -// | /16 | 65536 | 256 | | -// | /15 | 131072 | 512 | | -// | /14 | 262144 | 1024 | | -// | /13 | 524288 | 2048 | | -// | /12 | 1048576 | 4096 | | -// | /11 | 2097152 | 8192 | | -// | /10 | 4194304 | 16384 | | -// | /9 | 8388608 | 32768 | | -// | /8 | 16777216 | 65536 | | -// -// Best Practice Notes: -// - Use /24 as the minimum subnet size for Azure (smaller subnets are not supported for most services). -// - Plan for future growth: allocate larger address spaces (e.g., /20 or /21 for VNets) to allow for new subnets. -// - Avoid overlapping address spaces with on-premises or other VNets. -// - Use contiguous, non-overlapping ranges for subnets. -// - Document subnet usage and purpose in code comments. -// - For AVM modules, ensure only one delegation per subnet and leave delegations empty if not required. - -module network 'network/network-resources.bicep' = { - name: take('network-${resourcesName}-create', 64) - params: { - resourcesName: resourcesName - location: location - logAnalyticsWorkSpaceResourceId: logAnalyticsWorkSpaceResourceId - tags: tags - addressPrefixes: ['10.0.0.0/20'] // 4096 addresses (enough for 8 /23 subnets or 16 /24) - subnets: [ - // Only one delegation per subnet is supported by the AVM module as of June 2025. - // For subnets that do not require delegation, leave the value empty. - { - name: 'web' - addressPrefixes: ['10.0.0.0/23'] // /23 (10.0.0.0 - 10.0.1.255), 512 addresses - networkSecurityGroup: { - name: 'nsg-web' - securityRules: [ - { - name: 'AllowHttpsInbound' - properties: { - access: 'Allow' - direction: 'Inbound' - priority: 100 - protocol: 'Tcp' - sourcePortRange: '*' - destinationPortRange: '443' - sourceAddressPrefixes: ['0.0.0.0/0'] - destinationAddressPrefixes: ['10.0.0.0/23'] - } - } - { - name: 'AllowIntraSubnetTraffic' - properties: { - access: 'Allow' - direction: 'Inbound' - priority: 200 - protocol: '*' - sourcePortRange: '*' - destinationPortRange: '*' - sourceAddressPrefixes: ['10.0.0.0/23'] // From same subnet - destinationAddressPrefixes: ['10.0.0.0/23'] // To same subnet - } - } - { - name: 'AllowAzureLoadBalancer' - properties: { - access: 'Allow' - direction: 'Inbound' - priority: 300 - protocol: '*' - sourcePortRange: '*' - destinationPortRange: '*' - sourceAddressPrefix: 'AzureLoadBalancer' - destinationAddressPrefix: '10.0.0.0/23' - } - } - ] - } - delegation: 'Microsoft.Web/serverFarms' - } - { - name: 'peps' - addressPrefixes: ['10.0.2.0/23'] // /23 (10.0.2.0 - 10.0.3.255), 512 addresses - privateEndpointNetworkPolicies: 'Disabled' - privateLinkServiceNetworkPolicies: 'Disabled' - networkSecurityGroup: { - name: 'nsg-peps' - securityRules: [] - } - } - ] - bastionConfiguration: { - name: 'bas-${resourcesName}' - subnet: { - name: 'AzureBastionSubnet' - addressPrefixes: ['10.0.10.0/26'] - networkSecurityGroup: { - name: 'nsg-AzureBastionSubnet' - securityRules: [ - { - name: 'AllowGatewayManager' - properties: { - access: 'Allow' - direction: 'Inbound' - priority: 2702 - protocol: '*' - sourcePortRange: '*' - destinationPortRange: '443' - sourceAddressPrefix: 'GatewayManager' - destinationAddressPrefix: '*' - } - } - { - name: 'AllowHttpsInBound' - properties: { - access: 'Allow' - direction: 'Inbound' - priority: 2703 - protocol: '*' - sourcePortRange: '*' - destinationPortRange: '443' - sourceAddressPrefix: 'Internet' - destinationAddressPrefix: '*' - } - } - { - name: 'AllowSshRdpOutbound' - properties: { - access: 'Allow' - direction: 'Outbound' - priority: 100 - protocol: '*' - sourcePortRange: '*' - destinationPortRanges: ['22', '3389'] - sourceAddressPrefix: '*' - destinationAddressPrefix: 'VirtualNetwork' - } - } - { - name: 'AllowAzureCloudOutbound' - properties: { - access: 'Allow' - direction: 'Outbound' - priority: 110 - protocol: 'Tcp' - sourcePortRange: '*' - destinationPortRange: '443' - sourceAddressPrefix: '*' - destinationAddressPrefix: 'AzureCloud' - } - } - ] - } - } - } - jumpboxConfiguration: { - name: 'vm-jumpbox-${resourcesName}' - size: vmSize - username: vmAdminUsername - password: vmAdminPassword - subnet: { - name: 'jumpbox' - addressPrefixes: ['10.0.12.0/23'] // /23 (10.0.12.0 - 10.0.13.255), 512 addresses - networkSecurityGroup: { - name: 'nsg-jumbox' - securityRules: [ - { - name: 'AllowRdpFromBastion' - properties: { - access: 'Allow' - direction: 'Inbound' - priority: 100 - protocol: 'Tcp' - sourcePortRange: '*' - destinationPortRange: '3389' - sourceAddressPrefixes: [ - '10.0.10.0/26' // Azure Bastion subnet - ] - destinationAddressPrefixes: ['10.0.12.0/23'] - } - } - ] - } - } - } - enableTelemetry: enableTelemetry - } -} - -@description('Name of the Virtual Network resource.') -output vnetName string = network.outputs.vnetName - -@description('Resource ID of the Virtual Network.') -output vnetResourceId string = network.outputs.vnetResourceId - -@description('Resource ID of the "web" subnet.') -output subnetWebResourceId string = first(filter(network.outputs.subnets, s => s.name == 'web')).?resourceId ?? '' - -@description('Resource ID of the "peps" subnet for Private Endpoints.') -output subnetPrivateEndpointsResourceId string = first(filter(network.outputs.subnets, s => s.name == 'peps')).?resourceId ?? '' - -@description('Resource ID of the Bastion Host.') -output bastionResourceId string = network.outputs.bastionHostId - -@description('Resource ID of the Jumpbox VM.') -output jumpboxResourceId string = network.outputs.jumpboxResourceId diff --git a/infra/modules/network/bastionHost.bicep b/infra/modules/network/bastionHost.bicep deleted file mode 100644 index cc1987e5f..000000000 --- a/infra/modules/network/bastionHost.bicep +++ /dev/null @@ -1,104 +0,0 @@ -// /****************************************************************************************************************************/ -// Create Azure Bastion Subnet and Azure Bastion Host -// /****************************************************************************************************************************/ - -@description('Name of the Azure Bastion Host resource.') -param name string - -@description('Azure region to deploy resources.') -param location string = resourceGroup().location - -@description('Resource ID of the Virtual Network where the Azure Bastion Host will be deployed.') -param vnetId string - -@description('Name of the Virtual Network where the Azure Bastion Host will be deployed.') -param vnetName string - -@description('Resource ID of the Log Analytics Workspace for monitoring and diagnostics.') -param logAnalyticsWorkspaceId string - -@description('Optional. Tags to apply to the resources.') -param tags object = {} - -@description('Optional. Enable/Disable usage telemetry for module.') -param enableTelemetry bool = true - -import { subnetType } from 'virtualNetwork.bicep' -@description('Optional. Subnet configuration for the Jumpbox VM.') -param subnet subnetType? - -// 1. Create AzureBastionSubnet NSG -// using AVM Network Security Group module -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/network-security-group -module nsg 'br/public:avm/res/network/network-security-group:0.5.1' = if (!empty(subnet)) { - name: '${vnetName}-${subnet.?networkSecurityGroup.name}' - params: { - name: '${subnet.?networkSecurityGroup.name}-${vnetName}' - location: location - securityRules: subnet.?networkSecurityGroup.securityRules - tags: tags - enableTelemetry: enableTelemetry - } -} - -// 2. Create Azure Bastion Host using AVM Subnet Module with special config for Azure Bastion Subnet -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/virtual-network/subnet -module bastionSubnet 'br/public:avm/res/network/virtual-network/subnet:0.1.2' = if (!empty(subnet)) { - name: take('bastionSubnet-${vnetName}', 64) - params: { - virtualNetworkName: vnetName - name: 'AzureBastionSubnet' // this name required as is for Azure Bastion Host subnet - addressPrefixes: subnet.?addressPrefixes - networkSecurityGroupResourceId: nsg.outputs.resourceId - enableTelemetry: enableTelemetry - } -} - -// 3. Create Azure Bastion Host in AzureBastionsubnetSubnet using AVM Bastion Host module -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/bastion-host - -module bastionHost 'br/public:avm/res/network/bastion-host:0.6.1' = { - name: take('bastionHost-${vnetName}-${name}', 64) - params: { - name: name - skuName: 'Standard' - location: location - virtualNetworkResourceId: vnetId - diagnosticSettings: [ - { - name: 'bastionDiagnostics' - workspaceResourceId: logAnalyticsWorkspaceId - logCategoriesAndGroups: [ - { - categoryGroup: 'allLogs' - enabled: true - } - ] - } - ] - tags: tags - enableTelemetry: enableTelemetry - publicIPAddressObject: { - name: 'pip-${name}' - zones: [] - } - } - dependsOn: [ - bastionSubnet - ] -} - -output resourceId string = bastionHost.outputs.resourceId -output name string = bastionHost.outputs.name -output subnetId string = bastionSubnet.outputs.resourceId -output subnetName string = bastionSubnet.outputs.name - -@export() -@description('Custom type definition for establishing Bastion Host for remote connection.') -type bastionHostConfigurationType = { - @description('The name of the Bastion Host resource.') - name: string - - @description('Optional. Subnet configuration for the Jumpbox VM.') - subnet: subnetType? -} diff --git a/infra/modules/network/jumpbox.bicep b/infra/modules/network/jumpbox.bicep deleted file mode 100644 index 29f7d3e2f..000000000 --- a/infra/modules/network/jumpbox.bicep +++ /dev/null @@ -1,155 +0,0 @@ -// /****************************************************************************************************************************/ -// Create Jumpbox NSG and Jumpbox Subnet, then create Jumpbox VM -// /****************************************************************************************************************************/ - -@description('Name of the Jumpbox Virtual Machine.') -param name string - -@description('Azure region to deploy resources.') -param location string = resourceGroup().location - -@description('Name of the Virtual Network where the Jumpbox VM will be deployed.') -param vnetName string - -@description('Size of the Jumpbox Virtual Machine.') -param size string - -import { subnetType } from 'virtualNetwork.bicep' -@description('Optional. Subnet configuration for the Jumpbox VM.') -param subnet subnetType? - -@description('Username to access the Jumpbox VM.') -param username string - -@secure() -@description('Password to access the Jumpbox VM.') -param password string - -@description('Optional. Tags to apply to the resources.') -param tags object = {} - -@description('Log Analytics Workspace Resource ID for VM diagnostics.') -param logAnalyticsWorkspaceId string - -@description('Optional. Enable/Disable usage telemetry for module.') -param enableTelemetry bool = true - -// 1. Create Jumpbox NSG -// using AVM Network Security Group module -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/network-security-group -module nsg 'br/public:avm/res/network/network-security-group:0.5.1' = if (!empty(subnet)) { - name: '${vnetName}-${subnet.?networkSecurityGroup.name}' - params: { - name: '${subnet.?networkSecurityGroup.name}-${vnetName}' - location: location - securityRules: subnet.?networkSecurityGroup.securityRules - tags: tags - enableTelemetry: enableTelemetry - } -} - -// 2. Create Jumpbox subnet as part of the existing VNet -// using AVM Virtual Network Subnet module -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/virtual-network/subnet -module subnetResource 'br/public:avm/res/network/virtual-network/subnet:0.1.2' = if (!empty(subnet)) { - name: subnet.?name ?? '${vnetName}-jumpbox-subnet' - params: { - virtualNetworkName: vnetName - name: subnet.?name ?? '' - addressPrefixes: subnet.?addressPrefixes - networkSecurityGroupResourceId: nsg.outputs.resourceId - enableTelemetry: enableTelemetry - } -} - -// 3. Create Jumpbox VM -// using AVM Virtual Machine module -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/compute/virtual-machine -var vmName = take(name, 15) // Shorten VM name to 15 characters to avoid Azure limits - -module vm 'br/public:avm/res/compute/virtual-machine:0.15.0' = { - name: take('${vmName}-jumpbox', 64) - params: { - name: vmName - vmSize: size - location: location - adminUsername: username - adminPassword: password - tags: tags - zone: 0 - imageReference: { - offer: 'WindowsServer' - publisher: 'MicrosoftWindowsServer' - sku: '2019-datacenter' - version: 'latest' - } - osType: 'Windows' - osDisk: { - name: 'osdisk-${vmName}' - managedDisk: { - storageAccountType: 'Standard_LRS' - } - } - encryptionAtHost: false // Some Azure subscriptions do not support encryption at host - nicConfigurations: [ - { - name: 'nic-${vmName}' - ipConfigurations: [ - { - name: 'ipconfig1' - subnetResourceId: subnetResource.outputs.resourceId - } - ] - networkSecurityGroupResourceId: nsg.outputs.resourceId - diagnosticSettings: [ - { - name: 'jumpboxDiagnostics' - workspaceResourceId: logAnalyticsWorkspaceId - logCategoriesAndGroups: [ - { - categoryGroup: 'allLogs' - enabled: true - } - ] - metricCategories: [ - { - category: 'AllMetrics' - enabled: true - } - ] - } - ] - } - ] - enableTelemetry: enableTelemetry - } -} - -output resourceId string = vm.outputs.resourceId -output name string = vm.outputs.name -output location string = vm.outputs.location - -output subnetId string = subnetResource.outputs.resourceId -output subnetName string = subnetResource.outputs.name -output nsgId string = nsg.outputs.resourceId -output nsgName string = nsg.outputs.name - -@export() -@description('Custom type definition for establishing Jumpbox Virtual Machine and its associated resources.') -type jumpBoxConfigurationType = { - @description('The name of the Virtual Machine.') - name: string - - @description('The size of the VM.') - size: string? - - @description('Username to access VM.') - username: string - - @secure() - @description('Password to access VM.') - password: string - - @description('Optional. Subnet configuration for the Jumpbox VM.') - subnet: subnetType? -} diff --git a/infra/modules/network/network-resources.bicep b/infra/modules/network/network-resources.bicep deleted file mode 100644 index 7e2e86587..000000000 --- a/infra/modules/network/network-resources.bicep +++ /dev/null @@ -1,104 +0,0 @@ -@minLength(6) -@maxLength(25) -@description('Name used for naming all network resources.') -param resourcesName string - -@minLength(3) -@description('Azure region for all services.') -param location string - -@description('Resource ID of the Log Analytics Workspace for monitoring and diagnostics.') -param logAnalyticsWorkSpaceResourceId string - -@description('Networking address prefix for the VNET.') -param addressPrefixes array - -import { subnetType } from 'virtualNetwork.bicep' -@description('Array of subnets to be created within the VNET.') -param subnets subnetType[] - -import { jumpBoxConfigurationType } from 'jumpbox.bicep' -@description('Optional. Configuration for the Jumpbox VM. Leave null to omit Jumpbox creation.') -param jumpboxConfiguration jumpBoxConfigurationType? - -import { bastionHostConfigurationType } from 'bastionHost.bicep' -@description('Optional. Configuration for the Azure Bastion Host. Leave null to omit Bastion creation.') -param bastionConfiguration bastionHostConfigurationType? - -@description('Optional. Tags to be applied to the resources.') -param tags object = {} - -@description('Optional. Enable/Disable usage telemetry for module.') -param enableTelemetry bool = true - -// /****************************************************************************************************************************/ -// Networking - NSGs, VNET and Subnets. Each subnet has its own NSG -// /****************************************************************************************************************************/ - -module virtualNetwork 'virtualNetwork.bicep' = { - name: '${resourcesName}-virtualNetwork' - params: { - name: 'vnet-${resourcesName}' - addressPrefixes: addressPrefixes - subnets: subnets - location: location - tags: tags - logAnalyticsWorkspaceId: logAnalyticsWorkSpaceResourceId - enableTelemetry: enableTelemetry - } -} - -// /****************************************************************************************************************************/ -// // Create Azure Bastion Subnet and Azure Bastion Host -// /****************************************************************************************************************************/ - -module bastionHost 'bastionHost.bicep' = if (!empty(bastionConfiguration)) { - name: '${resourcesName}-bastionHost' - params: { - name: bastionConfiguration.?name ?? 'bas-${resourcesName}' - vnetId: virtualNetwork.outputs.resourceId - vnetName: virtualNetwork.outputs.name - location: location - logAnalyticsWorkspaceId: logAnalyticsWorkSpaceResourceId - subnet: bastionConfiguration.?subnet - tags: tags - enableTelemetry: enableTelemetry - } -} - -// /****************************************************************************************************************************/ -// // create Jumpbox NSG and Jumpbox Subnet, then create Jumpbox VM -// /****************************************************************************************************************************/ - -module jumpbox 'jumpbox.bicep' = if (!empty(jumpboxConfiguration)) { - name: '${resourcesName}-jumpbox' - params: { - name: jumpboxConfiguration.?name ?? 'vm-jumpbox-${resourcesName}' - vnetName: virtualNetwork.outputs.name - size: jumpboxConfiguration.?size ?? 'Standard_D2s_v3' - logAnalyticsWorkspaceId: logAnalyticsWorkSpaceResourceId - location: location - subnet: jumpboxConfiguration.?subnet - username: jumpboxConfiguration.?username ?? '' // required - password: jumpboxConfiguration.?password ?? '' // required - enableTelemetry: enableTelemetry - tags: tags - } -} - -output vnetName string = virtualNetwork.outputs.name -output vnetResourceId string = virtualNetwork.outputs.resourceId - -import { subnetOutputType } from 'virtualNetwork.bicep' -output subnets subnetOutputType[] = virtualNetwork.outputs.subnets // This one holds critical info for subnets, including NSGs - -output bastionSubnetId string = bastionHost.outputs.subnetId -output bastionSubnetName string = bastionHost.outputs.subnetName -output bastionHostId string = bastionHost.outputs.resourceId -output bastionHostName string = bastionHost.outputs.name - -output jumpboxSubnetName string = jumpbox.outputs.subnetName -output jumpboxSubnetId string = jumpbox.outputs.subnetId -output jumpboxName string = jumpbox.outputs.name -output jumpboxResourceId string = jumpbox.outputs.resourceId - diff --git a/infra/modules/network/virtualNetwork.bicep b/infra/modules/network/virtualNetwork.bicep deleted file mode 100644 index 6b5029740..000000000 --- a/infra/modules/network/virtualNetwork.bicep +++ /dev/null @@ -1,157 +0,0 @@ -/****************************************************************************************************************************/ -// Networking - NSGs, VNET and Subnets. Each subnet has its own NSG -/****************************************************************************************************************************/ -@description('Name of the virtual network.') -param name string - -@description('Azure region to deploy resources.') -param location string = resourceGroup().location - -@description('Required. An Array of 1 or more IP Address Prefixes OR the resource ID of the IPAM pool to be used for the Virtual Network. When specifying an IPAM pool resource ID you must also set a value for the parameter called `ipamPoolNumberOfIpAddresses`.') -param addressPrefixes array - -@description('An array of subnets to be created within the virtual network. Each subnet can have its own configuration and associated Network Security Group (NSG).') -param subnets subnetType[] - -@description('Optional. Tags to be applied to the resources.') -param tags object = {} - -@description('Optional. The resource ID of the Log Analytics Workspace to send diagnostic logs to.') -param logAnalyticsWorkspaceId string - -@description('Optional. Enable/Disable usage telemetry for module.') -param enableTelemetry bool = true - -// 1. Create NSGs for subnets -// using AVM Network Security Group module -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/network-security-group - -@batchSize(1) -module nsgs 'br/public:avm/res/network/network-security-group:0.5.1' = [ - for (subnet, i) in subnets: if (!empty(subnet.?networkSecurityGroup)) { - name: take('${name}-${subnet.?networkSecurityGroup.name}-networksecuritygroup', 64) - params: { - name: '${subnet.?networkSecurityGroup.name}-${name}' - location: location - securityRules: subnet.?networkSecurityGroup.securityRules - tags: tags - enableTelemetry: enableTelemetry - } - } -] - -// 2. Create VNet and subnets, with subnets associated with corresponding NSGs -// using AVM Virtual Network module -// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/virtual-network - -module virtualNetwork 'br/public:avm/res/network/virtual-network:0.7.0' = { - name: take('${name}-virtualNetwork', 64) - params: { - name: name - location: location - addressPrefixes: addressPrefixes - subnets: [ - for (subnet, i) in subnets: { - name: subnet.name - addressPrefixes: subnet.?addressPrefixes - networkSecurityGroupResourceId: !empty(subnet.?networkSecurityGroup) ? nsgs[i].outputs.resourceId : null - privateEndpointNetworkPolicies: subnet.?privateEndpointNetworkPolicies - privateLinkServiceNetworkPolicies: subnet.?privateLinkServiceNetworkPolicies - delegation: subnet.?delegation - } - ] - diagnosticSettings: [ - { - name: 'vnetDiagnostics' - workspaceResourceId: logAnalyticsWorkspaceId - logCategoriesAndGroups: [ - { - categoryGroup: 'allLogs' - enabled: true - } - ] - metricCategories: [ - { - category: 'AllMetrics' - enabled: true - } - ] - } - ] - tags: tags - enableTelemetry: enableTelemetry - } -} - -output name string = virtualNetwork.outputs.name -output resourceId string = virtualNetwork.outputs.resourceId - -// combined output array that holds subnet details along with NSG information -output subnets subnetOutputType[] = [ - for (subnet, i) in subnets: { - name: subnet.name - resourceId: virtualNetwork.outputs.subnetResourceIds[i] - nsgName: !empty(subnet.?networkSecurityGroup) ? subnet.?networkSecurityGroup.name : null - nsgResourceId: !empty(subnet.?networkSecurityGroup) ? nsgs[i].outputs.resourceId : null - } -] - -@export() -@description('Custom type definition for subnet resource information as output') -type subnetOutputType = { - @description('The name of the subnet.') - name: string - - @description('The resource ID of the subnet.') - resourceId: string - - @description('The name of the associated network security group, if any.') - nsgName: string? - - @description('The resource ID of the associated network security group, if any.') - nsgResourceId: string? -} - -@export() -@description('Custom type definition for subnet configuration') -type subnetType = { - @description('Required. The Name of the subnet resource.') - name: string - - @description('Required. Prefixes for the subnet.') // Required to ensure at least one prefix is provided - addressPrefixes: string[] - - @description('Optional. The delegation to enable on the subnet.') - delegation: string? - - @description('Optional. enable or disable apply network policies on private endpoint in the subnet.') - privateEndpointNetworkPolicies: ('Disabled' | 'Enabled' | 'NetworkSecurityGroupEnabled' | 'RouteTableEnabled')? - - @description('Optional. Enable or disable apply network policies on private link service in the subnet.') - privateLinkServiceNetworkPolicies: ('Disabled' | 'Enabled')? - - @description('Optional. Network Security Group configuration for the subnet.') - networkSecurityGroup: networkSecurityGroupType? - - @description('Optional. The resource ID of the route table to assign to the subnet.') - routeTableResourceId: string? - - @description('Optional. An array of service endpoint policies.') - serviceEndpointPolicies: object[]? - - @description('Optional. The service endpoints to enable on the subnet.') - serviceEndpoints: string[]? - - @description('Optional. Set this property to false to disable default outbound connectivity for all VMs in the subnet. This property can only be set at the time of subnet creation and cannot be updated for an existing subnet.') - defaultOutboundAccess: bool? -} - -@export() -@description('Custom type definition for network security group configuration') -type networkSecurityGroupType = { - @description('Required. The name of the network security group.') - name: string - - @description('Required. The security rules for the network security group.') - securityRules: object[] -} diff --git a/infra/modules/virtualNetwork.bicep b/infra/modules/virtualNetwork.bicep new file mode 100644 index 000000000..996b4a9cc --- /dev/null +++ b/infra/modules/virtualNetwork.bicep @@ -0,0 +1,346 @@ +/****************************************************************************************************************************/ +// Networking - NSGs, VNET and Subnets. Each subnet has its own NSG +/****************************************************************************************************************************/ +@description('Name of the virtual network.') +param name string + +@description('Azure region to deploy resources.') +param location string = resourceGroup().location + +@description('Required. An Array of 1 or more IP Address Prefixes for the Virtual Network.') +param addressPrefixes array + +@description('An array of subnets to be created within the virtual network. Each subnet can have its own configuration and associated Network Security Group (NSG).') +param subnets subnetType[] = [ + { + name: 'web' + addressPrefixes: ['10.0.0.0/23'] // /23 (10.0.0.0 - 10.0.1.255), 512 addresses + delegation: 'Microsoft.Web/serverFarms' + networkSecurityGroup: { + name: 'nsg-web' + securityRules: [ + { + name: 'AllowHttpsInbound' + properties: { + access: 'Allow' + direction: 'Inbound' + priority: 100 + protocol: 'Tcp' + sourcePortRange: '*' + destinationPortRange: '443' + sourceAddressPrefixes: ['0.0.0.0/0'] + destinationAddressPrefixes: ['10.0.0.0/23'] + } + } + { + name: 'AllowIntraSubnetTraffic' + properties: { + access: 'Allow' + direction: 'Inbound' + priority: 200 + protocol: '*' + sourcePortRange: '*' + destinationPortRange: '*' + sourceAddressPrefixes: ['10.0.0.0/23'] + destinationAddressPrefixes: ['10.0.0.0/23'] + } + } + { + name: 'AllowAzureLoadBalancer' + properties: { + access: 'Allow' + direction: 'Inbound' + priority: 300 + protocol: '*' + sourcePortRange: '*' + destinationPortRange: '*' + sourceAddressPrefix: 'AzureLoadBalancer' + destinationAddressPrefix: '10.0.0.0/23' + } + } + ] + } + } + { + name: 'peps' + addressPrefixes: ['10.0.2.0/23'] // /23 (10.0.2.0 - 10.0.3.255), 512 addresses + privateEndpointNetworkPolicies: 'Disabled' + privateLinkServiceNetworkPolicies: 'Disabled' + networkSecurityGroup: { + name: 'nsg-peps' + securityRules: [] + } + } + { + name: 'AzureBastionSubnet' // Required name for Azure Bastion + addressPrefixes: ['10.0.10.0/26'] + networkSecurityGroup: { + name: 'nsg-bastion' + securityRules: [ + { + name: 'AllowGatewayManager' + properties: { + access: 'Allow' + direction: 'Inbound' + priority: 2702 + protocol: '*' + sourcePortRange: '*' + destinationPortRange: '443' + sourceAddressPrefix: 'GatewayManager' + destinationAddressPrefix: '*' + } + } + { + name: 'AllowHttpsInBound' + properties: { + access: 'Allow' + direction: 'Inbound' + priority: 2703 + protocol: '*' + sourcePortRange: '*' + destinationPortRange: '443' + sourceAddressPrefix: 'Internet' + destinationAddressPrefix: '*' + } + } + { + name: 'AllowSshRdpOutbound' + properties: { + access: 'Allow' + direction: 'Outbound' + priority: 100 + protocol: '*' + sourcePortRange: '*' + destinationPortRanges: ['22', '3389'] + sourceAddressPrefix: '*' + destinationAddressPrefix: 'VirtualNetwork' + } + } + { + name: 'AllowAzureCloudOutbound' + properties: { + access: 'Allow' + direction: 'Outbound' + priority: 110 + protocol: 'Tcp' + sourcePortRange: '*' + destinationPortRange: '443' + sourceAddressPrefix: '*' + destinationAddressPrefix: 'AzureCloud' + } + } + ] + } + } + { + name: 'jumpbox' + addressPrefixes: ['10.0.12.0/23'] // /23 (10.0.12.0 - 10.0.13.255), 512 addresses + networkSecurityGroup: { + name: 'nsg-jumpbox' + securityRules: [ + { + name: 'AllowRdpFromBastion' + properties: { + access: 'Allow' + direction: 'Inbound' + priority: 100 + protocol: 'Tcp' + sourcePortRange: '*' + destinationPortRange: '3389' + sourceAddressPrefixes: ['10.0.10.0/26'] // Azure Bastion subnet + destinationAddressPrefixes: ['10.0.12.0/23'] + } + } + ] + } + } +] + +@description('Optional. Tags to be applied to the resources.') +param tags object = {} + +@description('Optional. The resource ID of the Log Analytics Workspace to send diagnostic logs to.') +param logAnalyticsWorkspaceId string + +@description('Optional. Enable/Disable usage telemetry for module.') +param enableTelemetry bool = true + +@description('Required. Suffix for resource naming.') +param resourceSuffix string + +// VM Size Notes: +// 1 B-series VMs (like Standard_B2ms) do not support accelerated networking. +// 2 Pick a VM size that does support accelerated networking (the usual jump-box candidates): +// Standard_DS2_v2 (2 vCPU, 7 GiB RAM, Premium SSD) // The most broadly available (it’s a legacy SKU supported in virtually every region). +// Standard_D2s_v3 (2 vCPU, 8 GiB RAM, Premium SSD) // next most common +// Standard_D2s_v4 (2 vCPU, 8 GiB RAM, Premium SSD) // Newest, so fewer regions availabl + + +// Subnet Classless Inter-Doman Routing (CIDR) Sizing Reference Table (Best Practices) +// | CIDR | # of Addresses | # of /24s | Notes | +// |-----------|---------------|-----------|----------------------------------------| +// | /24 | 256 | 1 | Smallest recommended for Azure subnets | +// | /23 | 512 | 2 | Good for 1-2 workloads per subnet | +// | /22 | 1024 | 4 | Good for 2-4 workloads per subnet | +// | /21 | 2048 | 8 | | +// | /20 | 4096 | 16 | Used for default VNet in this solution | +// | /19 | 8192 | 32 | | +// | /18 | 16384 | 64 | | +// | /17 | 32768 | 128 | | +// | /16 | 65536 | 256 | | +// | /15 | 131072 | 512 | | +// | /14 | 262144 | 1024 | | +// | /13 | 524288 | 2048 | | +// | /12 | 1048576 | 4096 | | +// | /11 | 2097152 | 8192 | | +// | /10 | 4194304 | 16384 | | +// | /9 | 8388608 | 32768 | | +// | /8 | 16777216 | 65536 | | +// +// Best Practice Notes: +// - Use /24 as the minimum subnet size for Azure (smaller subnets are not supported for most services). +// - Plan for future growth: allocate larger address spaces (e.g., /20 or /21 for VNets) to allow for new subnets. +// - Avoid overlapping address spaces with on-premises or other VNets. +// - Use contiguous, non-overlapping ranges for subnets. +// - Document subnet usage and purpose in code comments. +// - For AVM modules, ensure only one delegation per subnet and leave delegations empty if not required. + +// 1. Create NSGs for subnets +// using AVM Network Security Group module +// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/network-security-group + +@batchSize(1) +module nsgs 'br/public:avm/res/network/network-security-group:0.5.1' = [ + for (subnet, i) in subnets: if (!empty(subnet.?networkSecurityGroup)) { + name: take('avm.res.network.network-security-group.${subnet.?networkSecurityGroup.name}.${resourceSuffix}', 64) + params: { + name: '${subnet.?networkSecurityGroup.name}-${resourceSuffix}' + location: location + securityRules: subnet.?networkSecurityGroup.securityRules + tags: tags + enableTelemetry: enableTelemetry + } + } +] + +// 2. Create VNet and subnets, with subnets associated with corresponding NSGs +// using AVM Virtual Network module +// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/network/virtual-network + +module virtualNetwork 'br/public:avm/res/network/virtual-network:0.7.0' = { + name: take('avm.res.network.virtual-network.${name}', 64) + params: { + name: name + location: location + addressPrefixes: addressPrefixes + subnets: [ + for (subnet, i) in subnets: { + name: subnet.name + addressPrefixes: subnet.?addressPrefixes + networkSecurityGroupResourceId: !empty(subnet.?networkSecurityGroup) ? nsgs[i]!.outputs.resourceId : null + privateEndpointNetworkPolicies: subnet.?privateEndpointNetworkPolicies + privateLinkServiceNetworkPolicies: subnet.?privateLinkServiceNetworkPolicies + delegation: subnet.?delegation + } + ] + diagnosticSettings: [ + { + name: 'vnetDiagnostics' + workspaceResourceId: logAnalyticsWorkspaceId + logCategoriesAndGroups: [ + { + categoryGroup: 'allLogs' + enabled: true + } + ] + metricCategories: [ + { + category: 'AllMetrics' + enabled: true + } + ] + } + ] + tags: tags + enableTelemetry: enableTelemetry + } +} + +output name string = virtualNetwork.outputs.name +output resourceId string = virtualNetwork.outputs.resourceId + +// combined output array that holds subnet details along with NSG information +output subnets subnetOutputType[] = [ + for (subnet, i) in subnets: { + name: subnet.name + resourceId: virtualNetwork.outputs.subnetResourceIds[i] + nsgName: !empty(subnet.?networkSecurityGroup) ? subnet.?networkSecurityGroup.name : null + nsgResourceId: !empty(subnet.?networkSecurityGroup) ? nsgs[i]!.outputs.resourceId : null + } +] + +// Dynamic outputs for individual subnets for backward compatibility +output webSubnetResourceId string = contains(map(subnets, subnet => subnet.name), 'web') ? virtualNetwork.outputs.subnetResourceIds[indexOf(map(subnets, subnet => subnet.name), 'web')] : '' +output pepsSubnetResourceId string = contains(map(subnets, subnet => subnet.name), 'peps') ? virtualNetwork.outputs.subnetResourceIds[indexOf(map(subnets, subnet => subnet.name), 'peps')] : '' +output bastionSubnetResourceId string = contains(map(subnets, subnet => subnet.name), 'AzureBastionSubnet') ? virtualNetwork.outputs.subnetResourceIds[indexOf(map(subnets, subnet => subnet.name), 'AzureBastionSubnet')] : '' +output jumpboxSubnetResourceId string = contains(map(subnets, subnet => subnet.name), 'jumpbox') ? virtualNetwork.outputs.subnetResourceIds[indexOf(map(subnets, subnet => subnet.name), 'jumpbox')] : '' + +@export() +@description('Custom type definition for subnet resource information as output') +type subnetOutputType = { + @description('The name of the subnet.') + name: string + + @description('The resource ID of the subnet.') + resourceId: string + + @description('The name of the associated network security group, if any.') + nsgName: string? + + @description('The resource ID of the associated network security group, if any.') + nsgResourceId: string? +} + +@export() +@description('Custom type definition for subnet configuration') +type subnetType = { + @description('Required. The Name of the subnet resource.') + name: string + + @description('Required. Prefixes for the subnet.') // Required to ensure at least one prefix is provided + addressPrefixes: string[] + + @description('Optional. The delegation to enable on the subnet.') + delegation: string? + + @description('Optional. enable or disable apply network policies on private endpoint in the subnet.') + privateEndpointNetworkPolicies: ('Disabled' | 'Enabled' | 'NetworkSecurityGroupEnabled' | 'RouteTableEnabled')? + + @description('Optional. Enable or disable apply network policies on private link service in the subnet.') + privateLinkServiceNetworkPolicies: ('Disabled' | 'Enabled')? + + @description('Optional. Network Security Group configuration for the subnet.') + networkSecurityGroup: networkSecurityGroupType? + + @description('Optional. The resource ID of the route table to assign to the subnet.') + routeTableResourceId: string? + + @description('Optional. An array of service endpoint policies.') + serviceEndpointPolicies: object[]? + + @description('Optional. The service endpoints to enable on the subnet.') + serviceEndpoints: string[]? + + @description('Optional. Set this property to false to disable default outbound connectivity for all VMs in the subnet. This property can only be set at the time of subnet creation and cannot be updated for an existing subnet.') + defaultOutboundAccess: bool? +} + +@export() +@description('Custom type definition for network security group configuration') +type networkSecurityGroupType = { + @description('Required. The name of the network security group.') + name: string + + @description('Required. The security rules for the network security group.') + securityRules: object[] +}