|
| 1 | +provider "azurerm" { |
| 2 | + version = "2.0.0" |
| 3 | + features {} |
| 4 | +} |
| 5 | + |
| 6 | +resource "azurerm_resource_group" "azure-terraform" { |
| 7 | + name = "acctestrg" |
| 8 | + location = "West US 2" |
| 9 | +} |
| 10 | + |
| 11 | +resource "azurerm_virtual_network" "azure-terraform" { |
| 12 | + name = "acctvn" |
| 13 | + address_space = ["10.0.0.0/16"] |
| 14 | + location = azurerm_resource_group.azure-terraform.location |
| 15 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 16 | +} |
| 17 | + |
| 18 | +resource "azurerm_subnet" "azure-terraform" { |
| 19 | + name = "acctsub" |
| 20 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 21 | + virtual_network_name = azurerm_virtual_network.azure-terraform.name |
| 22 | + address_prefix = "10.0.2.0/24" |
| 23 | +} |
| 24 | + |
| 25 | +resource "azurerm_public_ip" "master" { |
| 26 | + count = var.master_count |
| 27 | + name = "accpublicIP-master${count.index}" |
| 28 | + location = azurerm_resource_group.azure-terraform.location |
| 29 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 30 | + allocation_method = "Dynamic" |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +resource "azurerm_public_ip" "worker" { |
| 35 | + count = var.worker_count |
| 36 | + name = "accpublicIP-worker${count.index}" |
| 37 | + location = azurerm_resource_group.azure-terraform.location |
| 38 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 39 | + allocation_method = "Dynamic" |
| 40 | +} |
| 41 | + |
| 42 | +resource "azurerm_network_interface" "master" { |
| 43 | + count = var.master_count |
| 44 | + name = "acctni-master${count.index}" |
| 45 | + location = azurerm_resource_group.azure-terraform.location |
| 46 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 47 | + |
| 48 | + ip_configuration { |
| 49 | + name = "testConfiguration" |
| 50 | + subnet_id = azurerm_subnet.azure-terraform.id |
| 51 | + private_ip_address_allocation = "dynamic" |
| 52 | + public_ip_address_id = azurerm_public_ip.master[count.index].id |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +resource "azurerm_network_interface" "worker" { |
| 57 | + count = var.worker_count |
| 58 | + name = "acctni-worker${count.index}" |
| 59 | + location = azurerm_resource_group.azure-terraform.location |
| 60 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 61 | + |
| 62 | + ip_configuration { |
| 63 | + name = "testConfiguration" |
| 64 | + subnet_id = azurerm_subnet.azure-terraform.id |
| 65 | + private_ip_address_allocation = "dynamic" |
| 66 | + public_ip_address_id = azurerm_public_ip.worker[count.index].id |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +resource "azurerm_managed_disk" "worker" { |
| 71 | + count = var.worker_count |
| 72 | + name = "datadisk_existing-worker_${count.index}" |
| 73 | + location = azurerm_resource_group.azure-terraform.location |
| 74 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 75 | + storage_account_type = "Standard_LRS" |
| 76 | + create_option = "Empty" |
| 77 | + disk_size_gb = "1023" |
| 78 | +} |
| 79 | + |
| 80 | +resource "azurerm_managed_disk" "master" { |
| 81 | + count = var.master_count |
| 82 | + name = "datadisk_existing-master_${count.index}" |
| 83 | + location = azurerm_resource_group.azure-terraform.location |
| 84 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 85 | + storage_account_type = "Standard_LRS" |
| 86 | + create_option = "Empty" |
| 87 | + disk_size_gb = "1023" |
| 88 | +} |
| 89 | + |
| 90 | +# Create (and display) an SSH key |
| 91 | +resource "tls_private_key" "example_ssh" { |
| 92 | + algorithm = "RSA" |
| 93 | + rsa_bits = 4096 |
| 94 | +} |
| 95 | + |
| 96 | +resource "azurerm_linux_virtual_machine" "master" { |
| 97 | + count = var.master_count |
| 98 | + name = "acctvm-master${count.index}" |
| 99 | + location = azurerm_resource_group.azure-terraform.location |
| 100 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 101 | + network_interface_ids = azurerm_network_interface.master.*.id |
| 102 | + size = "Standard_B2s" |
| 103 | + |
| 104 | + # Uncomment this line to delete the OS disk automatically when deleting the VM |
| 105 | + # delete_os_disk_on_termination = true |
| 106 | + |
| 107 | + # Uncomment this line to delete the data disks automatically when deleting the VM |
| 108 | + # delete_data_disks_on_termination = true |
| 109 | + |
| 110 | + source_image_reference { |
| 111 | + publisher = "Canonical" |
| 112 | + offer = "UbuntuServer" |
| 113 | + sku = "18.04-LTS" |
| 114 | + version = "latest" |
| 115 | + } |
| 116 | + |
| 117 | + os_disk { |
| 118 | + name = "accdisk-master-${count.index}" |
| 119 | + caching = "ReadWrite" |
| 120 | + storage_account_type = "Premium_LRS" |
| 121 | + } |
| 122 | + |
| 123 | + disable_password_authentication = true |
| 124 | + admin_username = "azureuser" |
| 125 | + |
| 126 | + admin_ssh_key { |
| 127 | + username = "azureuser" |
| 128 | + public_key = tls_private_key.example_ssh.public_key_openssh |
| 129 | + } |
| 130 | + |
| 131 | + tags = { |
| 132 | + environment = "master" |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +resource "azurerm_linux_virtual_machine" "worker" { |
| 137 | + count = var.worker_count |
| 138 | + name = "acctvm-worker${count.index}" |
| 139 | + location = azurerm_resource_group.azure-terraform.location |
| 140 | + resource_group_name = azurerm_resource_group.azure-terraform.name |
| 141 | + network_interface_ids = [element(azurerm_network_interface.worker.*.id, count.index)] |
| 142 | + size = "Standard_DS1_v2" |
| 143 | + |
| 144 | + # Uncomment this line to delete the OS disk automatically when deleting the VM |
| 145 | + # delete_os_disk_on_termination = true |
| 146 | + |
| 147 | + # Uncomment this line to delete the data disks automatically when deleting the VM |
| 148 | + # delete_data_disks_on_termination = true |
| 149 | + |
| 150 | + source_image_reference { |
| 151 | + publisher = "Canonical" |
| 152 | + offer = "UbuntuServer" |
| 153 | + sku = "18.04-LTS" |
| 154 | + version = "latest" |
| 155 | + } |
| 156 | + |
| 157 | + os_disk { |
| 158 | + name = "accdisk-worker-${count.index}" |
| 159 | + caching = "ReadWrite" |
| 160 | + storage_account_type = "Premium_LRS" |
| 161 | + } |
| 162 | + |
| 163 | + disable_password_authentication = true |
| 164 | + admin_username = "azureuser" |
| 165 | + |
| 166 | + admin_ssh_key { |
| 167 | + username = "azureuser" |
| 168 | + public_key = tls_private_key.example_ssh.public_key_openssh |
| 169 | + } |
| 170 | + |
| 171 | + tags = { |
| 172 | + environment = "worker" |
| 173 | + } |
| 174 | +} |
| 175 | + |
0 commit comments