Skip to content

Instantly share code, notes, and snippets.

@adrianbumbas
Last active January 6, 2020 09:38
Show Gist options
  • Save adrianbumbas/976e76a6ee9f4c84b01b86ace99fa185 to your computer and use it in GitHub Desktop.
Save adrianbumbas/976e76a6ee9f4c84b01b86ace99fa185 to your computer and use it in GitHub Desktop.
resource "azurerm_virtual_machine" "vm" {
...
zones = ["1"]
availability_set_id = 1
...
}
resource "azurerm_availability_set" "set" {
count = 2
name = "availabilitySet${count.index}"
location = azurerm_resource_group.myterraformgroup.location
resource_group_name = azurerm_resource_group.myterraformgroup.name
managed = true
platform_update_domain_count = 20
platform_fault_domain_count = 3
}
esource "azurerm_virtual_machine" "vm" {
for_each = local.zones
name = "vm_az${each.value}"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
network_interface_ids = ["${azurerm_network_interface.myterraformnic[each.value].id}"]
vm_size = "Standard_B1ls"
zones = [each.value]
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myOsDisk${each.value}"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
locals {
# The eastus region has 3 availability zones
zones = toset(["1", "2", "3"])
}
resource "azurerm_network_interface" "myterraformnic" {
count = 3
name = "myNIC${count.index}"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
ip_configuration {
name = "myNicConfiguration${count.index}"
subnet_id = azurerm_subnet.myterraformsubnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_resource_group" "myterraformgroup" {
name = "vmharg"
location = "eastus"
}
resource "azurerm_virtual_network" "myterraformnetwork" {
name = "myVnet"
address_space = ["10.0.0.0/16"]
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
}
resource "azurerm_subnet" "myterraformsubnet" {
name = "mySubnet"
resource_group_name = azurerm_resource_group.myterraformgroup.name
virtual_network_name = azurerm_virtual_network.myterraformnetwork.name
address_prefix = "10.0.2.0/24"
}
resource "azurerm_virtual_machine" "vm" {
count = 3
name = "vm_az${count.index}"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
network_interface_ids = ["${azurerm_network_interface.myterraformnic[count.index].id}"]
vm_size = "Standard_B1ls"
availability_set_id = count.index == 3 ? azurerm_availability_set.set[2].id : azurerm_availability_set.set[1].id
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myOsDisk${count.index}"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment