Terraform: Create Virtual Network Peering and VMs


Sample “Virtual network peering” with 2 VMs with terraform deployment

TF file: https://github.com/fujute/m18h/tree/master/tf/virtual-network-peering

  • main.tf
  • variables.tf

Building VNET peering with terraform

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
terraform plan -out main-vnet.tfplan
terraform apply "main-vnet.tfplan"
terraform plan -out main-vnet.tfplan terraform apply "main-vnet.tfplan"
terraform plan -out main-vnet.tfplan
terraform apply "main-vnet.tfplan"

Sample screenshot to access fx1-vm1 in fx1-network1 via jump host fx1-vm2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh azureuser@10.0.2.4
adminuser@fx1-vm2:~$ hostname
fx1-vm2
adminuser@fx1-vm2:~$ ssh azureuser@10.0.2.4
azureuser@10.0.2.4's password:
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-1080-azure x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Thu Jun 2 10:38:38 UTC 2022
System load: 0.33 Processes: 131
Usage of /: 6.5% of 28.90GB Users logged in: 0
Memory usage: 3% IP address for eth0: 10.0.2.4
Swap usage: 0%
0 updates can be applied immediately.
New release '20.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Thu Jun 2 10:37:25 2022 from 192.168.2.4
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
azureuser@fx1-vm1:~$
ssh azureuser@10.0.2.4 adminuser@fx1-vm2:~$ hostname fx1-vm2 adminuser@fx1-vm2:~$ ssh azureuser@10.0.2.4 azureuser@10.0.2.4's password: Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-1080-azure x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Thu Jun 2 10:38:38 UTC 2022 System load: 0.33 Processes: 131 Usage of /: 6.5% of 28.90GB Users logged in: 0 Memory usage: 3% IP address for eth0: 10.0.2.4 Swap usage: 0% 0 updates can be applied immediately. New release '20.04.4 LTS' available. Run 'do-release-upgrade' to upgrade to it. Last login: Thu Jun 2 10:37:25 2022 from 192.168.2.4 To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. azureuser@fx1-vm1:~$
ssh azureuser@10.0.2.4

adminuser@fx1-vm2:~$ hostname
fx1-vm2
adminuser@fx1-vm2:~$ ssh azureuser@10.0.2.4
azureuser@10.0.2.4's password:
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-1080-azure x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Jun  2 10:38:38 UTC 2022

  System load:  0.33              Processes:           131
  Usage of /:   6.5% of 28.90GB   Users logged in:     0
  Memory usage: 3%                IP address for eth0: 10.0.2.4
  Swap usage:   0%


0 updates can be applied immediately.

New release '20.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Thu Jun  2 10:37:25 2022 from 192.168.2.4
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

azureuser@fx1-vm1:~$

delete the deployment with terraform destroy

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
terraform plan -destroy -out main-vnet.destroy.tfplan
terraform apply main-vnet.destroy.tfplan
terraform plan -destroy -out main-vnet.destroy.tfplan terraform apply main-vnet.destroy.tfplan
terraform plan -destroy -out main-vnet.destroy.tfplan
terraform apply main-vnet.destroy.tfplan

Optional Tasks:

  • Adding Private Endpoint for Azure Blob Storage and Private DNS Zone

Reference command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
az vm image list-skus --location eastasia --offer WindowsServer --publisher MicrosoftWindowsServer
az vm image list-skus --location eastasia --offer UbuntuServer --publisher Canonical
az vm list-skus -l southeastasia --resource-type virtualMachines --output table | grep Standard_D2ds_v4
az account set --subscription "xxxxxxxxxxxxxxx"
az vm list-usage --location southeastasia -o table | grep -E -w -i 'DSv4|FSv2|ESv4'
az vm image list-skus --location eastasia --offer WindowsServer --publisher MicrosoftWindowsServer az vm image list-skus --location eastasia --offer UbuntuServer --publisher Canonical az vm list-skus -l southeastasia --resource-type virtualMachines --output table | grep Standard_D2ds_v4 az account set --subscription "xxxxxxxxxxxxxxx" az vm list-usage --location southeastasia -o table | grep -E -w -i 'DSv4|FSv2|ESv4'
az vm image list-skus --location eastasia --offer WindowsServer --publisher MicrosoftWindowsServer
az vm image list-skus --location eastasia --offer UbuntuServer --publisher Canonical
az vm list-skus -l southeastasia  --resource-type virtualMachines  --output table | grep Standard_D2ds_v4

az account set --subscription "xxxxxxxxxxxxxxx"
az vm list-usage --location southeastasia -o table | grep -E -w -i  'DSv4|FSv2|ESv4'
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
declare -a subscrptionsID=(
"12345-12342134-12342134-1234"
"12345-12342134-12342134-1235"
"12345-12342134-12342134-1236"
"12345-12342134-12342134-1237"
)
echo "${subscrptionsID[@]}"
for mySubscrptionsID in "${subscrptionsID[@]}"
do
az account set --subscription “$mySubscrptionsID
az vm list-usage --location southeastasia -o table | grep -E -w -i 'DSv4|FSv2|ESv4|DSv3'
done
#!/bin/bash declare -a subscrptionsID=( "12345-12342134-12342134-1234" "12345-12342134-12342134-1235" "12345-12342134-12342134-1236" "12345-12342134-12342134-1237" ) echo "${subscrptionsID[@]}" for mySubscrptionsID in "${subscrptionsID[@]}" do az account set --subscription “$mySubscrptionsID” az vm list-usage --location southeastasia -o table | grep -E -w -i 'DSv4|FSv2|ESv4|DSv3' done
#!/bin/bash
declare -a subscrptionsID=(
"12345-12342134-12342134-1234"
"12345-12342134-12342134-1235"
"12345-12342134-12342134-1236"
"12345-12342134-12342134-1237"
)

echo "${subscrptionsID[@]}"

for mySubscrptionsID in "${subscrptionsID[@]}" 
do
   az account set --subscription  “$mySubscrptionsID”
   az vm list-usage --location southeastasia -o table | grep -E -w -i  'DSv4|FSv2|ESv4|DSv3'
done
, ,