main.tf 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # cybergav.in - 4th July 2021
  2. # USE-CASE: Test network latency between endpoints in different VNet across VNet peering connections. The VM SKU and settings will have a bearing on network performance.
  3. #
  4. #########################################################################################################################################
  5. #
  6. # Terraform Provider Configuration
  7. #
  8. terraform {
  9. required_version = ">= 0.15"
  10. required_providers {
  11. azurerm = {
  12. source = "hashicorp/azurerm"
  13. version = "=2.58.0"
  14. }
  15. }
  16. }
  17. provider "azurerm" {
  18. features {}
  19. }
  20. #
  21. # Locals
  22. #
  23. locals {
  24. custom_data = <<CUSTOM_DATA
  25. #!/bin/bash
  26. sudo dnf -y install qperf
  27. sudo systemctl stop firewalld
  28. sudo systemctl disable firewalld
  29. CUSTOM_DATA
  30. }
  31. #
  32. # Resource Group
  33. #
  34. resource "azurerm_resource_group" "rg" {
  35. name = "${var.prefix}-rg"
  36. location = var.location
  37. }
  38. #
  39. # Virtual Networks and Subnets
  40. #
  41. resource "azurerm_virtual_network" "vnet1" {
  42. name = "${var.prefix}-vnet-1"
  43. address_space = ["10.100.0.0/16"]
  44. location = var.location
  45. resource_group_name = azurerm_resource_group.rg.name
  46. subnet {
  47. name = "${var.prefix}-snet-1"
  48. address_prefix = "10.100.0.0/24"
  49. }
  50. }
  51. resource "azurerm_virtual_network" "vnet2" {
  52. name = "${var.prefix}-vnet-2"
  53. address_space = ["10.200.0.0/16"]
  54. location = var.location
  55. resource_group_name = azurerm_resource_group.rg.name
  56. subnet {
  57. name = "${var.prefix}-snet-2"
  58. address_prefix = "10.200.0.0/24"
  59. }
  60. }
  61. #
  62. # Virtual Network Peerings
  63. #
  64. resource "azurerm_virtual_network_peering" "peer1" {
  65. name = "cg-peering-vnet1-to-vnet2"
  66. resource_group_name = azurerm_resource_group.rg.name
  67. virtual_network_name = azurerm_virtual_network.vnet1.name
  68. remote_virtual_network_id = azurerm_virtual_network.vnet2.id
  69. }
  70. resource "azurerm_virtual_network_peering" "peer2" {
  71. name = "cg-peering-vnet2-to-vnet1"
  72. resource_group_name = azurerm_resource_group.rg.name
  73. virtual_network_name = azurerm_virtual_network.vnet2.name
  74. remote_virtual_network_id = azurerm_virtual_network.vnet1.id
  75. }
  76. #
  77. # Public IPs
  78. #
  79. resource "azurerm_public_ip" "pip1" {
  80. name = "${var.prefix}-pip1"
  81. resource_group_name = azurerm_resource_group.rg.name
  82. location = var.location
  83. allocation_method = "Dynamic"
  84. }
  85. resource "azurerm_public_ip" "pip2" {
  86. name = "${var.prefix}-pip2"
  87. resource_group_name = azurerm_resource_group.rg.name
  88. location = var.location
  89. allocation_method = "Dynamic"
  90. }
  91. #
  92. # Virtual Machine NICs
  93. #
  94. resource "azurerm_network_interface" "vm1_nic" {
  95. name = "${var.prefix}-vm1-nic"
  96. location = var.location
  97. resource_group_name = azurerm_resource_group.rg.name
  98. ip_configuration {
  99. name = "internal"
  100. subnet_id = azurerm_virtual_network.vnet1.subnet.*.id[0]
  101. private_ip_address_allocation = "Dynamic"
  102. public_ip_address_id = azurerm_public_ip.pip1.id
  103. }
  104. }
  105. resource "azurerm_network_interface" "vm2_nic" {
  106. name = "${var.prefix}-vm2-nic"
  107. location = var.location
  108. resource_group_name = azurerm_resource_group.rg.name
  109. ip_configuration {
  110. name = "internal"
  111. subnet_id = azurerm_virtual_network.vnet2.subnet.*.id[0]
  112. private_ip_address_allocation = "Dynamic"
  113. public_ip_address_id = azurerm_public_ip.pip2.id
  114. }
  115. }
  116. #
  117. # Virtual Machines
  118. #
  119. resource "azurerm_linux_virtual_machine" "vm1" {
  120. name = "${var.prefix}-vm1"
  121. resource_group_name = azurerm_resource_group.rg.name
  122. location = var.location
  123. size = "Standard_B1s"
  124. admin_username = var.admin_username
  125. admin_password = var.admin_password
  126. disable_password_authentication = false
  127. custom_data = base64encode(local.custom_data)
  128. network_interface_ids = [
  129. azurerm_network_interface.vm1_nic.id,
  130. ]
  131. os_disk {
  132. caching = "ReadWrite"
  133. storage_account_type = "Standard_LRS"
  134. }
  135. source_image_reference {
  136. publisher = "oracle"
  137. offer = "oracle-linux"
  138. sku = "ol84-lvm-gen2"
  139. version = "latest"
  140. }
  141. }
  142. resource "azurerm_linux_virtual_machine" "vm2" {
  143. name = "${var.prefix}-vm2"
  144. resource_group_name = azurerm_resource_group.rg.name
  145. location = var.location
  146. size = "Standard_B1s"
  147. admin_username = var.admin_username
  148. admin_password = var.admin_password
  149. disable_password_authentication = false
  150. custom_data = base64encode(local.custom_data)
  151. network_interface_ids = [
  152. azurerm_network_interface.vm2_nic.id,
  153. ]
  154. os_disk {
  155. caching = "ReadWrite"
  156. storage_account_type = "Standard_LRS"
  157. }
  158. source_image_reference {
  159. publisher = "oracle"
  160. offer = "oracle-linux"
  161. sku = "ol84-lvm-gen2"
  162. version = "latest"
  163. }
  164. }