diff --git a/quickstart/101-vm-cluster-linux/main.tf b/quickstart/101-vm-cluster-linux/main.tf new file mode 100644 index 00000000..45033d41 --- /dev/null +++ b/quickstart/101-vm-cluster-linux/main.tf @@ -0,0 +1,145 @@ +resource "random_pet" "rg_name" { + prefix = var.resource_group_name_prefix +} + +resource "azurerm_resource_group" "rg" { + name = random_pet.rg_name.id + location = var.resource_group_location +} + +resource "random_pet" "azurerm_virtual_network_name" { + prefix = "vnet" +} + +resource "azurerm_virtual_network" "test" { + name = random_pet.azurerm_virtual_network_name.id + address_space = ["10.0.0.0/16"] + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name +} + +resource "random_pet" "azurerm_subnet_name" { + prefix = "sub" +} + +resource "azurerm_subnet" "test" { + name = random_pet.azurerm_subnet_name.id + resource_group_name = azurerm_resource_group.rg.name + virtual_network_name = azurerm_virtual_network.test.name + address_prefixes = ["10.0.2.0/24"] +} + +resource "azurerm_public_ip" "test" { + name = "publicIPForLB" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + allocation_method = "Static" +} + +resource "azurerm_lb" "test" { + name = "loadBalancer" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + + frontend_ip_configuration { + name = "publicIPAddress" + public_ip_address_id = azurerm_public_ip.test.id + } +} + +resource "azurerm_lb_backend_address_pool" "test" { + loadbalancer_id = azurerm_lb.test.id + name = "BackEndAddressPool" +} + +resource "azurerm_network_interface" "test" { + count = 2 + name = "acctni${count.index}" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + + ip_configuration { + name = "testConfiguration" + subnet_id = azurerm_subnet.test.id + private_ip_address_allocation = "Dynamic" + } +} + +resource "azurerm_availability_set" "avset" { + name = "avset" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + platform_fault_domain_count = 2 + platform_update_domain_count = 2 + managed = true +} + +resource "random_pet" "azurerm_linux_virtual_machine_name" { + prefix = "vm" +} + +resource "random_password" "password" { + count = var.password == null ? 1 : 0 + length = 20 + special = true + min_numeric = 1 + min_upper = 1 + min_lower = 1 + min_special = 1 +} + +locals { + password = try(random_password.password[0].result, var.password) +} + +resource "azurerm_linux_virtual_machine" "test" { + count = 2 + name = "${random_pet.azurerm_linux_virtual_machine_name.id}${count.index}" + location = azurerm_resource_group.rg.location + availability_set_id = azurerm_availability_set.avset.id + resource_group_name = azurerm_resource_group.rg.name + network_interface_ids = [azurerm_network_interface.test[count.index].id] + size = "Standard_DS1_v2" + + # Uncomment this line to delete the OS disk automatically when deleting the VM + # delete_os_disk_on_termination = true + + # Uncomment this line to delete the data disks automatically when deleting the VM + # delete_data_disks_on_termination = true + + source_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" + version = "latest" + } + + os_disk { + caching = "ReadWrite" + storage_account_type = "Standard_LRS" + name = "myosdisk${count.index}" + } + + computer_name = "hostname" + admin_username = var.user_name + admin_password = local.password + disable_password_authentication = false +} + +resource "azurerm_managed_disk" "test" { + count = 2 + name = "datadisk_existing_${count.index}" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + storage_account_type = "Standard_LRS" + create_option = "Empty" + disk_size_gb = "1024" +} + +resource "azurerm_virtual_machine_data_disk_attachment" "test" { + count = 2 + managed_disk_id = azurerm_managed_disk.test[count.index].id + virtual_machine_id = azurerm_linux_virtual_machine.test[count.index].id + lun = "10" + caching = "ReadWrite" +} \ No newline at end of file diff --git a/quickstart/101-vm-cluster-linux/outputs.tf b/quickstart/101-vm-cluster-linux/outputs.tf new file mode 100644 index 00000000..ef67ce10 --- /dev/null +++ b/quickstart/101-vm-cluster-linux/outputs.tf @@ -0,0 +1,20 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "virtual_network_name" { + value = azurerm_virtual_network.test.name +} + +output "subnet_name" { + value = azurerm_subnet.test.name +} + +output "linux_virtual_machine_names" { + value = [for s in azurerm_linux_virtual_machine.test : s.name[*]] +} + +output "linux_virtual_machine_password" { + sensitive = true + value = local.password +} \ No newline at end of file diff --git a/quickstart/101-vm-cluster-linux/providers.tf b/quickstart/101-vm-cluster-linux/providers.tf new file mode 100644 index 00000000..4fd5f6ba --- /dev/null +++ b/quickstart/101-vm-cluster-linux/providers.tf @@ -0,0 +1,16 @@ +terraform { + required_version = ">=1.0" + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>3.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-vm-cluster-linux/readme.md b/quickstart/101-vm-cluster-linux/readme.md new file mode 100644 index 00000000..4ad966db --- /dev/null +++ b/quickstart/101-vm-cluster-linux/readme.md @@ -0,0 +1,31 @@ +# Azure Linux VM cluster + +This template deploys a Linux VM cluster on Azure. + +## Terraform resource types + +- [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) +- [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) +- [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) +- [azurerm_lb](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/lb) +- [azurerm_lb_backend_address_pool](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/lb_backend_address_pool) +- [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) +- [azurerm_managed_disk](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk) +- [azurerm_availability_set](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/availability_set) +- [random_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) +- [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) + +## Variables + +| Name | Description | Default | +|-|-|-| +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg | +| `resource_group_location` | Location of the resource group. | eastus | +| `user_name` | The username for the local account that will be created on the new VM. | | +| `password` | The password for the local account that will be created on the new VM. | | + +## Example + +To see how to run this example, see [Create an Azure Linux VM cluster with Terraform](https://learn.microsoft.com/azure/developer/terraform/create-vm-cluster-with-infrastructure). diff --git a/quickstart/101-vm-cluster-linux/variables.tf b/quickstart/101-vm-cluster-linux/variables.tf new file mode 100644 index 00000000..c776f82d --- /dev/null +++ b/quickstart/101-vm-cluster-linux/variables.tf @@ -0,0 +1,24 @@ +variable "resource_group_location" { + type = string + description = "Location for all resources." + default = "eastus" +} + +variable "resource_group_name_prefix" { + type = string + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." + default = "rg" +} + +variable "user_name" { + type = string + description = "The username for the local account that will be created on the new vm." + default = "azureadmin" +} + +variable "password" { + type = string + description = "The password for the local account that will be created on the new vm." + sensitive = true + default = null +}