github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/examples/azure-spark-and-cassandra-on-centos/main.tf (about) 1 # provider "azurerm" { 2 # subscription_id = "${var.subscription_id}" 3 # client_id = "${var.client_id}" 4 # client_secret = "${var.client_secret}" 5 # tenant_id = "${var.tenant_id}" 6 # } 7 8 resource "azurerm_resource_group" "rg" { 9 name = "${var.resource_group}" 10 location = "${var.location}" 11 } 12 13 # ********************** NETWORK SECURITY GROUPS ********************** # 14 resource "azurerm_network_security_group" "master" { 15 name = "${var.nsg_spark_master_name}" 16 resource_group_name = "${azurerm_resource_group.rg.name}" 17 location = "${azurerm_resource_group.rg.location}" 18 19 security_rule { 20 name = "ssh" 21 description = "Allow SSH" 22 priority = 100 23 direction = "Inbound" 24 access = "Allow" 25 protocol = "Tcp" 26 source_port_range = "*" 27 destination_port_range = "22" 28 source_address_prefix = "Internet" 29 destination_address_prefix = "*" 30 } 31 32 security_rule { 33 name = "http_webui_spark" 34 description = "Allow Web UI Access to Spark" 35 priority = 101 36 direction = "Inbound" 37 access = "Allow" 38 protocol = "Tcp" 39 source_port_range = "*" 40 destination_port_range = "8080" 41 source_address_prefix = "Internet" 42 destination_address_prefix = "*" 43 } 44 45 security_rule { 46 name = "http_rest_spark" 47 description = "Allow REST API Access to Spark" 48 priority = 102 49 direction = "Inbound" 50 access = "Allow" 51 protocol = "Tcp" 52 source_port_range = "*" 53 destination_port_range = "6066" 54 source_address_prefix = "Internet" 55 destination_address_prefix = "*" 56 } 57 } 58 59 resource "azurerm_network_security_group" "slave" { 60 name = "${var.nsg_spark_slave_name}" 61 resource_group_name = "${azurerm_resource_group.rg.name}" 62 location = "${azurerm_resource_group.rg.location}" 63 64 security_rule { 65 name = "ssh" 66 description = "Allow SSH" 67 priority = 100 68 direction = "Inbound" 69 access = "Allow" 70 protocol = "Tcp" 71 source_port_range = "*" 72 destination_port_range = "22" 73 source_address_prefix = "Internet" 74 destination_address_prefix = "*" 75 } 76 } 77 78 resource "azurerm_network_security_group" "cassandra" { 79 name = "${var.nsg_cassandra_name}" 80 resource_group_name = "${azurerm_resource_group.rg.name}" 81 location = "${azurerm_resource_group.rg.location}" 82 83 security_rule { 84 name = "ssh" 85 description = "Allow SSH" 86 priority = 100 87 direction = "Inbound" 88 access = "Allow" 89 protocol = "Tcp" 90 source_port_range = "*" 91 destination_port_range = "22" 92 source_address_prefix = "Internet" 93 destination_address_prefix = "*" 94 } 95 } 96 97 # ********************** VNET / SUBNETS ********************** # 98 resource "azurerm_virtual_network" "spark" { 99 name = "vnet-spark" 100 resource_group_name = "${azurerm_resource_group.rg.name}" 101 location = "${azurerm_resource_group.rg.location}" 102 address_space = ["${var.vnet_spark_prefix}"] 103 } 104 105 resource "azurerm_subnet" "subnet1" { 106 name = "${var.vnet_spark_subnet1_name}" 107 virtual_network_name = "${azurerm_virtual_network.spark.name}" 108 resource_group_name = "${azurerm_resource_group.rg.name}" 109 address_prefix = "${var.vnet_spark_subnet1_prefix}" 110 network_security_group_id = "${azurerm_network_security_group.master.id}" 111 depends_on = ["azurerm_virtual_network.spark"] 112 } 113 114 resource "azurerm_subnet" "subnet2" { 115 name = "${var.vnet_spark_subnet2_name}" 116 virtual_network_name = "${azurerm_virtual_network.spark.name}" 117 resource_group_name = "${azurerm_resource_group.rg.name}" 118 address_prefix = "${var.vnet_spark_subnet2_prefix}" 119 } 120 121 resource "azurerm_subnet" "subnet3" { 122 name = "${var.vnet_spark_subnet3_name}" 123 virtual_network_name = "${azurerm_virtual_network.spark.name}" 124 resource_group_name = "${azurerm_resource_group.rg.name}" 125 address_prefix = "${var.vnet_spark_subnet3_prefix}" 126 } 127 128 # ********************** PUBLIC IP ADDRESSES ********************** # 129 resource "azurerm_public_ip" "master" { 130 name = "${var.public_ip_master_name}" 131 location = "${azurerm_resource_group.rg.location}" 132 resource_group_name = "${azurerm_resource_group.rg.name}" 133 public_ip_address_allocation = "Static" 134 } 135 136 resource "azurerm_public_ip" "slave" { 137 name = "${var.public_ip_slave_name_prefix}${count.index}" 138 location = "${azurerm_resource_group.rg.location}" 139 resource_group_name = "${azurerm_resource_group.rg.name}" 140 public_ip_address_allocation = "Static" 141 count = "${var.vm_number_of_slaves}" 142 } 143 144 resource "azurerm_public_ip" "cassandra" { 145 name = "${var.public_ip_cassandra_name}" 146 location = "${azurerm_resource_group.rg.location}" 147 resource_group_name = "${azurerm_resource_group.rg.name}" 148 public_ip_address_allocation = "Static" 149 } 150 151 # ********************** NETWORK INTERFACE ********************** # 152 resource "azurerm_network_interface" "master" { 153 name = "${var.nic_master_name}" 154 location = "${azurerm_resource_group.rg.location}" 155 resource_group_name = "${azurerm_resource_group.rg.name}" 156 network_security_group_id = "${azurerm_network_security_group.master.id}" 157 depends_on = ["azurerm_virtual_network.spark", "azurerm_public_ip.master", "azurerm_network_security_group.master"] 158 159 ip_configuration { 160 name = "ipconfig1" 161 subnet_id = "${azurerm_subnet.subnet1.id}" 162 private_ip_address_allocation = "Static" 163 private_ip_address = "${var.nic_master_node_ip}" 164 public_ip_address_id = "${azurerm_public_ip.master.id}" 165 } 166 } 167 168 resource "azurerm_network_interface" "slave" { 169 name = "${var.nic_slave_name_prefix}${count.index}" 170 location = "${azurerm_resource_group.rg.location}" 171 resource_group_name = "${azurerm_resource_group.rg.name}" 172 network_security_group_id = "${azurerm_network_security_group.slave.id}" 173 count = "${var.vm_number_of_slaves}" 174 depends_on = ["azurerm_virtual_network.spark", "azurerm_public_ip.slave", "azurerm_network_security_group.slave"] 175 176 ip_configuration { 177 name = "ipconfig1" 178 subnet_id = "${azurerm_subnet.subnet2.id}" 179 private_ip_address_allocation = "Static" 180 private_ip_address = "${var.nic_slave_node_ip_prefix}${5 + count.index}" 181 public_ip_address_id = "${element(azurerm_public_ip.slave.*.id, count.index)}" 182 } 183 } 184 185 resource "azurerm_network_interface" "cassandra" { 186 name = "${var.nic_cassandra_name}" 187 location = "${azurerm_resource_group.rg.location}" 188 resource_group_name = "${azurerm_resource_group.rg.name}" 189 network_security_group_id = "${azurerm_network_security_group.cassandra.id}" 190 depends_on = ["azurerm_virtual_network.spark", "azurerm_public_ip.cassandra", "azurerm_network_security_group.cassandra"] 191 192 ip_configuration { 193 name = "ipconfig1" 194 subnet_id = "${azurerm_subnet.subnet3.id}" 195 private_ip_address_allocation = "Static" 196 private_ip_address = "${var.nic_cassandra_node_ip}" 197 public_ip_address_id = "${azurerm_public_ip.cassandra.id}" 198 } 199 } 200 201 # ********************** AVAILABILITY SET ********************** # 202 resource "azurerm_availability_set" "slave" { 203 name = "${var.availability_slave_name}" 204 location = "${azurerm_resource_group.rg.location}" 205 resource_group_name = "${azurerm_resource_group.rg.name}" 206 platform_update_domain_count = 5 207 platform_fault_domain_count = 2 208 } 209 210 # ********************** STORAGE ACCOUNTS ********************** # 211 resource "azurerm_storage_account" "master" { 212 name = "master${var.unique_prefix}" 213 resource_group_name = "${azurerm_resource_group.rg.name}" 214 location = "${azurerm_resource_group.rg.location}" 215 account_type = "${var.storage_master_type}" 216 } 217 218 resource "azurerm_storage_container" "master" { 219 name = "${var.vm_master_storage_account_container_name}" 220 resource_group_name = "${azurerm_resource_group.rg.name}" 221 storage_account_name = "${azurerm_storage_account.master.name}" 222 container_access_type = "private" 223 depends_on = ["azurerm_storage_account.master"] 224 } 225 226 resource "azurerm_storage_account" "slave" { 227 name = "slave${var.unique_prefix}${count.index}" 228 resource_group_name = "${azurerm_resource_group.rg.name}" 229 location = "${azurerm_resource_group.rg.location}" 230 count = "${var.vm_number_of_slaves}" 231 account_type = "${var.storage_slave_type}" 232 } 233 234 resource "azurerm_storage_container" "slave" { 235 name = "${var.vm_slave_storage_account_container_name}${count.index}" 236 resource_group_name = "${azurerm_resource_group.rg.name}" 237 storage_account_name = "${element(azurerm_storage_account.slave.*.name, count.index)}" 238 container_access_type = "private" 239 depends_on = ["azurerm_storage_account.slave"] 240 } 241 242 resource "azurerm_storage_account" "cassandra" { 243 name = "cassandra${var.unique_prefix}" 244 resource_group_name = "${azurerm_resource_group.rg.name}" 245 location = "${azurerm_resource_group.rg.location}" 246 account_type = "${var.storage_cassandra_type}" 247 } 248 249 resource "azurerm_storage_container" "cassandra" { 250 name = "${var.vm_cassandra_storage_account_container_name}" 251 resource_group_name = "${azurerm_resource_group.rg.name}" 252 storage_account_name = "${azurerm_storage_account.cassandra.name}" 253 container_access_type = "private" 254 depends_on = ["azurerm_storage_account.cassandra"] 255 } 256 257 # ********************** MASTER VIRTUAL MACHINE ********************** # 258 resource "azurerm_virtual_machine" "master" { 259 name = "${var.vm_master_name}" 260 resource_group_name = "${azurerm_resource_group.rg.name}" 261 location = "${azurerm_resource_group.rg.location}" 262 vm_size = "${var.vm_master_vm_size}" 263 network_interface_ids = ["${azurerm_network_interface.master.id}"] 264 depends_on = ["azurerm_storage_account.master", "azurerm_network_interface.master", "azurerm_storage_container.master"] 265 266 storage_image_reference { 267 publisher = "${var.os_image_publisher}" 268 offer = "${var.os_image_offer}" 269 sku = "${var.os_version}" 270 version = "latest" 271 } 272 273 storage_os_disk { 274 name = "${var.vm_master_os_disk_name}" 275 vhd_uri = "http://${azurerm_storage_account.master.name}.blob.core.windows.net/${azurerm_storage_container.master.name}/${var.vm_master_os_disk_name}.vhd" 276 create_option = "FromImage" 277 caching = "ReadWrite" 278 } 279 280 os_profile { 281 computer_name = "${var.vm_master_name}" 282 admin_username = "${var.vm_admin_username}" 283 admin_password = "${var.vm_admin_password}" 284 } 285 286 os_profile_linux_config { 287 disable_password_authentication = false 288 } 289 290 connection { 291 type = "ssh" 292 host = "${azurerm_public_ip.master.ip_address}" 293 user = "${var.vm_admin_username}" 294 password = "${var.vm_admin_password}" 295 } 296 297 provisioner "remote-exec" { 298 inline = [ 299 "wget ${var.artifacts_location}${var.script_spark_provisioner_script_file_name}", 300 "echo ${var.vm_admin_password} | sudo -S sh ./${var.script_spark_provisioner_script_file_name} -runas=master -master=${var.nic_master_node_ip}", 301 ] 302 } 303 } 304 305 # ********************** SLAVE VIRTUAL MACHINES ********************** # 306 resource "azurerm_virtual_machine" "slave" { 307 name = "${var.vm_slave_name_prefix}${count.index}" 308 resource_group_name = "${azurerm_resource_group.rg.name}" 309 location = "${azurerm_resource_group.rg.location}" 310 vm_size = "${var.vm_slave_vm_size}" 311 network_interface_ids = ["${element(azurerm_network_interface.slave.*.id, count.index)}"] 312 count = "${var.vm_number_of_slaves}" 313 availability_set_id = "${azurerm_availability_set.slave.id}" 314 depends_on = ["azurerm_storage_account.slave", "azurerm_network_interface.slave", "azurerm_storage_container.slave"] 315 316 317 storage_image_reference { 318 publisher = "${var.os_image_publisher}" 319 offer = "${var.os_image_offer}" 320 sku = "${var.os_version}" 321 version = "latest" 322 } 323 324 325 storage_os_disk { 326 name = "${var.vm_slave_os_disk_name_prefix}${count.index}" 327 vhd_uri = "http://${element(azurerm_storage_account.slave.*.name, count.index)}.blob.core.windows.net/${element(azurerm_storage_container.slave.*.name, count.index)}/${var.vm_slave_os_disk_name_prefix}.vhd" 328 create_option = "FromImage" 329 caching = "ReadWrite" 330 } 331 332 333 os_profile { 334 computer_name = "${var.vm_slave_name_prefix}${count.index}" 335 admin_username = "${var.vm_admin_username}" 336 admin_password = "${var.vm_admin_password}" 337 } 338 339 340 os_profile_linux_config { 341 disable_password_authentication = false 342 } 343 344 connection { 345 type = "ssh" 346 host = "${element(azurerm_public_ip.slave.*.ip_address, count.index)}" 347 user = "${var.vm_admin_username}" 348 password = "${var.vm_admin_password}" 349 } 350 351 provisioner "remote-exec" { 352 inline = [ 353 "wget ${var.artifacts_location}${var.script_spark_provisioner_script_file_name}", 354 "echo ${var.vm_admin_password} | sudo -S sh ./${var.script_spark_provisioner_script_file_name} -runas=slave -master=${var.nic_master_node_ip}", 355 ] 356 } 357 } 358 359 # ********************** CASSANDRA VIRTUAL MACHINE ********************** # 360 resource "azurerm_virtual_machine" "cassandra" { 361 name = "${var.vm_cassandra_name}" 362 resource_group_name = "${azurerm_resource_group.rg.name}" 363 location = "${azurerm_resource_group.rg.location}" 364 vm_size = "${var.vm_cassandra_vm_size}" 365 network_interface_ids = ["${azurerm_network_interface.cassandra.id}"] 366 depends_on = ["azurerm_storage_account.cassandra", "azurerm_network_interface.cassandra", "azurerm_storage_container.cassandra"] 367 368 storage_image_reference { 369 publisher = "${var.os_image_publisher}" 370 offer = "${var.os_image_offer}" 371 sku = "${var.os_version}" 372 version = "latest" 373 } 374 375 storage_os_disk { 376 name = "${var.vm_cassandra_os_disk_name}" 377 vhd_uri = "http://${azurerm_storage_account.cassandra.name}.blob.core.windows.net/${azurerm_storage_container.cassandra.name}/${var.vm_cassandra_os_disk_name}.vhd" 378 create_option = "FromImage" 379 caching = "ReadWrite" 380 } 381 382 os_profile { 383 computer_name = "${var.vm_cassandra_name}" 384 admin_username = "${var.vm_admin_username}" 385 admin_password = "${var.vm_admin_password}" 386 } 387 388 os_profile_linux_config { 389 disable_password_authentication = false 390 } 391 392 connection { 393 type = "ssh" 394 host = "${azurerm_public_ip.cassandra.ip_address}" 395 user = "${var.vm_admin_username}" 396 password = "${var.vm_admin_password}" 397 } 398 399 provisioner "remote-exec" { 400 inline = [ 401 "wget ${var.artifacts_location}${var.script_cassandra_provisioner_script_file_name}", 402 "echo ${var.vm_admin_password} | sudo -S sh ./${var.script_cassandra_provisioner_script_file_name}", 403 ] 404 } 405 }