From 97bfc889b1e9ce2bff96a415d50877081c0ce9ae Mon Sep 17 00:00:00 2001 From: Alex Gonzalez Date: Thu, 2 Dec 2021 23:40:10 +1100 Subject: [PATCH] Added cross sub peer sample. --- deploy_crossSubPeer/README.md | 35 +++++++++++++++++++++++++ deploy_crossSubPeer/src/deploy.ps1 | 29 ++++++++++++++++++++ deploy_crossSubPeer/src/peer/main.bicep | 28 ++++++++++++++++++++ deploy_crossSubPeer/src/peer/peer.bicep | 23 ++++++++++++++++ deploy_crossSubPeer/src/vnet/main.bicep | 32 ++++++++++++++++++++++ deploy_crossSubPeer/src/vnet/vnet.bicep | 28 ++++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 deploy_crossSubPeer/README.md create mode 100644 deploy_crossSubPeer/src/deploy.ps1 create mode 100644 deploy_crossSubPeer/src/peer/main.bicep create mode 100644 deploy_crossSubPeer/src/peer/peer.bicep create mode 100644 deploy_crossSubPeer/src/vnet/main.bicep create mode 100644 deploy_crossSubPeer/src/vnet/vnet.bicep diff --git a/deploy_crossSubPeer/README.md b/deploy_crossSubPeer/README.md new file mode 100644 index 0000000..5874aec --- /dev/null +++ b/deploy_crossSubPeer/README.md @@ -0,0 +1,35 @@ +# Sample + +## Getting started + +This sample uses [Bicep](https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview) to deploy a cross region and cross subscription vnet peering. + +### Install + +1. Install the Azure CLI by following the [docs](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) article. + +1. Install Bicep from within the Azure CLI: + +``` +az bicep install + +az bicep upgrade +``` + +### Login + +1. Login and select your subscription + +``` +az login +``` + +### Deploy + +1. Run the `deploy.ps1` script from the `src` directory of this sample: + +``` +cd deploy_crossSubPeer/src/ + +./deploy.ps1 -Subscription1 -Subscription2 +``` diff --git a/deploy_crossSubPeer/src/deploy.ps1 b/deploy_crossSubPeer/src/deploy.ps1 new file mode 100644 index 0000000..c5149eb --- /dev/null +++ b/deploy_crossSubPeer/src/deploy.ps1 @@ -0,0 +1,29 @@ +[CmdletBinding()] +param ( + [Parameter(Mandatory=$true, Position=0)] + [string] + $Subscription1, + + [Parameter(Mandatory=$true, Position=1)] + [string] + $Subscription2 +) + +$aue = 'australiaeast' +$ase = 'australiasoutheast' + +Write-Output "deploying first vnet" +$aueVnetDep = az deployment sub create --subscription $Subscription1 --location $aue --template-file vnet/main.bicep --name xregionvnet-aue +$aueVnetId = ($aueVnetDep | ConvertFrom-Json).properties.outputs.vnetId.value; $aueVnetId + +Write-Output "deploying second vnet" +$aseVnetDep = az deployment sub create --subscription $Subscription2 --location $ase --template-file vnet/main.bicep --name xregionvnet-ase +$aseVnetId = ($aseVnetDep | ConvertFrom-Json).properties.outputs.vnetId.value; $aseVnetId + +Write-Output "peering first vnet to second vent" +$auePeerDep = az deployment sub create --subscription $Subscription1 --location $aue --template-file peer/main.bicep --name xregionpeer-aue --parameters vnetId=$aueVnetId remoteVnetId=$aseVnetId +$auePeerId = ($auePeerDep | ConvertFrom-Json).properties.outputs.peerId.value; $auePeerId + +Write-Output "peering second vnet to first vent" +$asePeerDep = az deployment sub create --subscription $Subscription2 --location $ase --template-file peer/main.bicep --name xregionpeer-ase --parameters vnetId=$aseVnetId remoteVnetId=$aueVnetId +$asePeerId = ($asePeerDep | ConvertFrom-Json).properties.outputs.peerId.value; $asePeerId \ No newline at end of file diff --git a/deploy_crossSubPeer/src/peer/main.bicep b/deploy_crossSubPeer/src/peer/main.bicep new file mode 100644 index 0000000..1acbadc --- /dev/null +++ b/deploy_crossSubPeer/src/peer/main.bicep @@ -0,0 +1,28 @@ +targetScope = 'subscription' + +param vnetId string +param remoteVnetId string + +var location = deployment().location + +var regionCodeLookup = { + australiaeast: 'aue' + australiasoutheast: 'ase' +} +var shortLocation = regionCodeLookup[location] + +resource rg_xRegionPeer 'Microsoft.Resources/resourceGroups@2021-04-01' = { + location: location + name: '${shortLocation}-xRegionPeer' +} + +module dep_peer 'peer.bicep' = { + name: '${shortLocation}-vnet' + scope: rg_xRegionPeer + params: { + vnetId: vnetId + remoteVnetId: remoteVnetId + } +} + +output peerId string = dep_peer.outputs.peerId diff --git a/deploy_crossSubPeer/src/peer/peer.bicep b/deploy_crossSubPeer/src/peer/peer.bicep new file mode 100644 index 0000000..d30c47d --- /dev/null +++ b/deploy_crossSubPeer/src/peer/peer.bicep @@ -0,0 +1,23 @@ +param vnetId string +param remoteVnetId string + +var vnetName = last(split(vnetId, '/')) +var remoteVnetName = last(split(remoteVnetId, '/')) + +resource vnet 'Microsoft.Network/virtualNetworks@2021-03-01' existing = { + name: vnetName +} + +resource peer 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings@2021-03-01' = { + name: '${vnetName}-TO-${remoteVnetName}' + parent: vnet + properties: { + remoteVirtualNetwork: { + id: remoteVnetId + } + allowForwardedTraffic: true + allowVirtualNetworkAccess: true + } +} + +output peerId string = peer.id diff --git a/deploy_crossSubPeer/src/vnet/main.bicep b/deploy_crossSubPeer/src/vnet/main.bicep new file mode 100644 index 0000000..17cbe86 --- /dev/null +++ b/deploy_crossSubPeer/src/vnet/main.bicep @@ -0,0 +1,32 @@ +targetScope = 'subscription' + +var location = deployment().location + +var regionCodeLookup = { + australiaeast: 'aue' + australiasoutheast: 'ase' +} +var shortLocation = regionCodeLookup[location] + +var regionPrefixLookup = { + australiaeast: '10.101.0.0/16' + australiasoutheast: '10.102.0.0/16' +} +var regionAddressPrefix = regionPrefixLookup[location] + +resource rg_xRegionPeer 'Microsoft.Resources/resourceGroups@2021-04-01' = { + location: location + name: '${shortLocation}-xRegionPeer' +} + +module dep_vnet 'vnet.bicep' = { + name: '${shortLocation}-vnet' + scope: rg_xRegionPeer + params: { + location: location + shortLocation: shortLocation + regionAddressPrefix: regionAddressPrefix + } +} + +output vnetId string = dep_vnet.outputs.vnetId diff --git a/deploy_crossSubPeer/src/vnet/vnet.bicep b/deploy_crossSubPeer/src/vnet/vnet.bicep new file mode 100644 index 0000000..d9344ae --- /dev/null +++ b/deploy_crossSubPeer/src/vnet/vnet.bicep @@ -0,0 +1,28 @@ +param location string +param shortLocation string +param regionAddressPrefix string + +var octet1 = int(split(regionAddressPrefix, '.')[0]) +var octet2 = int(split(regionAddressPrefix, '.')[1]) + +resource net_vnet 'Microsoft.Network/virtualNetworks@2021-03-01' = { + name: '${shortLocation}-vnet' + location: location + properties: { + addressSpace: { + addressPrefixes: [ + regionAddressPrefix + ] + } + subnets: [ + { + name: 'VmSubnet' + properties: { + addressPrefix: '${octet1}.${octet2}.0.0/28' + } + } + ] + } +} + +output vnetId string = net_vnet.id