Guest User

Untitled

a guest
Jul 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. provider "azurerm" {
  2. }
  3.  
  4. resource "azurerm_resource_group" "tf_rg" {
  5. name = "${var.resource_group_name}"
  6. location = "eastus"
  7.  
  8. tags {
  9. environment = "${var.resource_group_name}"
  10. }
  11. }
  12.  
  13. resource "azurerm_virtual_network" "tf_network" {
  14. name = "network-${random_id.tf_random_id.hex}"
  15. address_space = ["10.0.0.0/16"]
  16. location = "eastus"
  17. resource_group_name = "${azurerm_resource_group.tf_rg.name}"
  18.  
  19. tags {
  20. environment = "${random_id.tf_random_id.hex}"
  21. }
  22. }
  23.  
  24. resource "azurerm_subnet" "tf_subnet" {
  25. name = "subnet-${random_id.tf_random_id.hex}"
  26. resource_group_name = "${azurerm_resource_group.tf_rg.name}"
  27. virtual_network_name = "${azurerm_virtual_network.tf_network.name}"
  28. address_prefix = "10.0.1.0/24"
  29. }
  30.  
  31. resource "azurerm_public_ip" "tf_publicip" {
  32. name = "ip-${random_id.tf_random_id.hex}"
  33. location = "eastus"
  34. resource_group_name = "${azurerm_resource_group.tf_rg.name}"
  35. public_ip_address_allocation = "dynamic"
  36.  
  37. tags {
  38. environment = "${random_id.tf_random_id.hex}"
  39. }
  40. }
  41.  
  42. resource "azurerm_network_security_group" "tf_nsg" {
  43. name = "nsg-${random_id.tf_random_id.hex}"
  44. location = "eastus"
  45. resource_group_name = "${azurerm_resource_group.tf_rg.name}"
  46.  
  47. security_rule {
  48. name = "SSH"
  49. priority = 1001
  50. direction = "Inbound"
  51. access = "Allow"
  52. protocol = "Tcp"
  53. source_port_range = "*"
  54. destination_port_range = "22"
  55. source_address_prefix = "*"
  56. destination_address_prefix = "*"
  57. }
  58.  
  59. security_rule {
  60. name = "Vault"
  61. priority = 1002
  62. direction = "Inbound"
  63. access = "Allow"
  64. protocol = "Tcp"
  65. source_port_range = "*"
  66. destination_port_range = "8200"
  67. source_address_prefix = "*"
  68. destination_address_prefix = "*"
  69. }
  70.  
  71. security_rule {
  72. name = "Consul"
  73. priority = 1003
  74. direction = "Inbound"
  75. access = "Allow"
  76. protocol = "Tcp"
  77. source_port_range = "*"
  78. destination_port_range = "8500"
  79. source_address_prefix = "*"
  80. destination_address_prefix = "*"
  81. }
  82.  
  83. tags {
  84. environment = "${random_id.tf_random_id.hex}"
  85. }
  86. }
  87.  
  88. resource "azurerm_network_interface" "tf_nic" {
  89. name = "nic-${random_id.tf_random_id.hex}"
  90. location = "eastus"
  91. resource_group_name = "${azurerm_resource_group.tf_rg.name}"
  92. network_security_group_id = "${azurerm_network_security_group.tf_nsg.id}"
  93.  
  94. ip_configuration {
  95. name = "nic-${random_id.tf_random_id.hex}"
  96. subnet_id = "${azurerm_subnet.tf_subnet.id}"
  97. private_ip_address_allocation = "dynamic"
  98. public_ip_address_id = "${azurerm_public_ip.tf_publicip.id}"
  99. }
  100.  
  101. tags {
  102. environment = "${random_id.tf_random_id.hex}"
  103. }
  104. }
  105.  
  106. resource "random_id" "tf_random_id" {
  107. keepers = {
  108. # Generate a new ID only when a new resource group is defined
  109. resource_group = "${azurerm_resource_group.tf_rg.name}"
  110. }
  111.  
  112. byte_length = 8
  113. }
  114.  
  115. resource "azurerm_storage_account" "tf_storageaccount" {
  116. name = "sa${random_id.tf_random_id.hex}"
  117. resource_group_name = "${azurerm_resource_group.tf_rg.name}"
  118. location = "eastus"
  119. account_tier = "Standard"
  120. account_replication_type = "LRS"
  121.  
  122. tags {
  123. environment = "${random_id.tf_random_id.hex}"
  124. }
  125. }
  126.  
  127. data "template_file" "setup" {
  128. template = "${file("${path.module}/setup.tpl")}"
  129.  
  130. vars = {
  131. resource_group_name = "${var.resource_group_name}"
  132. vm_name = "${var.vm_name}"
  133. vault_download_url = "${var.vault_download_url}"
  134. tenant_id = "${var.tenant_id}"
  135. subscription_id = "${var.subscription_id}"
  136. client_id = "${var.client_id}"
  137. client_secret = "${var.client_secret}"
  138. }
  139. }
  140.  
  141. # Create virtual machine
  142. resource "azurerm_virtual_machine" "tf_vm" {
  143. name = "${var.vm_name}"
  144. location = "eastus"
  145. resource_group_name = "${azurerm_resource_group.tf_rg.name}"
  146. network_interface_ids = ["${azurerm_network_interface.tf_nic.id}"]
  147. vm_size = "Standard_DS1_v2"
  148.  
  149. identity = {
  150. type = "SystemAssigned"
  151. }
  152.  
  153. storage_os_disk {
  154. name = "OsDisk"
  155. caching = "ReadWrite"
  156. create_option = "FromImage"
  157. managed_disk_type = "Premium_LRS"
  158. }
  159.  
  160. storage_image_reference {
  161. publisher = "Canonical"
  162. offer = "UbuntuServer"
  163. sku = "16.04.0-LTS"
  164. version = "latest"
  165. }
  166.  
  167. os_profile {
  168. computer_name = "${var.vm_name}"
  169. admin_username = "azureuser"
  170. custom_data = "${data.template_file.setup.rendered}"
  171. }
  172.  
  173. os_profile_linux_config {
  174. disable_password_authentication = true
  175. ssh_keys {
  176. path = "/home/azureuser/.ssh/authorized_keys"
  177. key_data = "${var.public_key}"
  178. }
  179. }
  180.  
  181. boot_diagnostics {
  182. enabled = "true"
  183. storage_uri = "${azurerm_storage_account.tf_storageaccount.primary_blob_endpoint}"
  184. }
  185.  
  186. tags {
  187. environment = "${random_id.tf_random_id.hex}"
  188. }
  189. }
Add Comment
Please, Sign In to add comment