diff --git a/prepare-vms/terraform/azure/main.tf b/prepare-vms/terraform/azure/main.tf new file mode 100644 index 00000000..59dedd20 --- /dev/null +++ b/prepare-vms/terraform/azure/main.tf @@ -0,0 +1,71 @@ +resource "azurerm_resource_group" "_" { + name = var.prefix + location = var.location +} + +resource "azurerm_public_ip" "_" { + count = var.how_many_nodes + name = format("%s-%04d", var.prefix, count.index + 1) + location = azurerm_resource_group._.location + resource_group_name = azurerm_resource_group._.name + allocation_method = "Dynamic" +} + +resource "azurerm_network_interface" "_" { + count = var.how_many_nodes + name = format("%s-%04d", var.prefix, count.index + 1) + location = azurerm_resource_group._.location + resource_group_name = azurerm_resource_group._.name + + ip_configuration { + name = "internal" + subnet_id = azurerm_subnet._.id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip._[count.index].id + } +} + +resource "azurerm_linux_virtual_machine" "_" { + count = var.how_many_nodes + name = format("%s-%04d", var.prefix, count.index + 1) + resource_group_name = azurerm_resource_group._.name + location = azurerm_resource_group._.location + size = var.size + admin_username = "ubuntu" + network_interface_ids = [ + azurerm_network_interface._[count.index].id, + ] + + admin_ssh_key { + username = "ubuntu" + public_key = local.authorized_keys + } + + os_disk { + caching = "ReadWrite" + storage_account_type = "Standard_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "18.04-LTS" # FIXME + version = "latest" + } +} + +# The public IP address only gets allocated when the address actually gets +# attached to the virtual machine. So we need to do this extra indrection +# to retrieve the IP addresses. Otherwise the IP addresses show up as blank. +# See: https://github.com/hashicorp/terraform-provider-azurerm/issues/310#issuecomment-335479735 + +data "azurerm_public_ip" "_" { + count = var.how_many_nodes + name = format("%s-%04d", var.prefix, count.index + 1) + resource_group_name = azurerm_resource_group._.name + depends_on = [azurerm_linux_virtual_machine._] +} + +output "ip_addresses" { + value = join("", formatlist("%s\n", data.azurerm_public_ip._.*.ip_address)) +} diff --git a/prepare-vms/terraform/azure/network.tf b/prepare-vms/terraform/azure/network.tf new file mode 100644 index 00000000..343287e4 --- /dev/null +++ b/prepare-vms/terraform/azure/network.tf @@ -0,0 +1,13 @@ +resource "azurerm_virtual_network" "_" { + name = "tf-vnet" + address_space = ["10.10.0.0/16"] + location = azurerm_resource_group._.location + resource_group_name = azurerm_resource_group._.name +} + +resource "azurerm_subnet" "_" { + name = "tf-subnet" + resource_group_name = azurerm_resource_group._.name + virtual_network_name = azurerm_virtual_network._.name + address_prefixes = ["10.10.0.0/20"] +} diff --git a/prepare-vms/terraform/azure/provider.tf b/prepare-vms/terraform/azure/provider.tf new file mode 100644 index 00000000..a2781afe --- /dev/null +++ b/prepare-vms/terraform/azure/provider.tf @@ -0,0 +1,13 @@ +terraform { + required_version = ">= 1" + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "=3.33.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/prepare-vms/terraform/azure/variables.tf b/prepare-vms/terraform/azure/variables.tf new file mode 100644 index 00000000..128c1baa --- /dev/null +++ b/prepare-vms/terraform/azure/variables.tf @@ -0,0 +1,32 @@ +variable "prefix" { + type = string + default = "provisioned-with-terraform" +} + +variable "how_many_nodes" { + type = number + default = 2 +} + +locals { + authorized_keys = file("~/.ssh/id_rsa.pub") +} + +/* +Available sizes: +"Standard_D11_v2" # CPU=2 RAM=14 +"Standard_F4s_v2" # CPU=4 RAM=8 +"Standard_D1_v2" # CPU=1 RAM=3.5 +"Standard_B1ms" # CPU=1 RAM=2 +"Standard_B2s" # CPU=2 RAM=4 +*/ + +variable "size" { + type = string + default = "Standard_F4s_v2" +} + +variable "location" { + type = string + default = "South Africa North" +}