main.tf 4.4 KB

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