From bd72b68a7ce72754dbec06aad9ad0bef299dc160 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 19 Jun 2023 10:03:53 -0700 Subject: [PATCH 1/7] Fixing Windows VM cluster article code --- quickstart/101-vm-cluster-windows/main.tf | 51 +++++++++++++++++++ quickstart/101-vm-cluster-windows/outputs.tf | 19 +++++++ .../101-vm-cluster-windows/providers.tf | 20 ++++++++ quickstart/101-vm-cluster-windows/readme.md | 23 +++++++++ .../101-vm-cluster-windows/variables.tf | 11 ++++ 5 files changed, 124 insertions(+) create mode 100644 quickstart/101-vm-cluster-windows/main.tf create mode 100644 quickstart/101-vm-cluster-windows/outputs.tf create mode 100644 quickstart/101-vm-cluster-windows/providers.tf create mode 100644 quickstart/101-vm-cluster-windows/readme.md create mode 100644 quickstart/101-vm-cluster-windows/variables.tf diff --git a/quickstart/101-vm-cluster-windows/main.tf b/quickstart/101-vm-cluster-windows/main.tf new file mode 100644 index 000000000..a78dc33bd --- /dev/null +++ b/quickstart/101-vm-cluster-windows/main.tf @@ -0,0 +1,51 @@ +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_string" "windows_server_vm_hostname" { + length = 10 + lower = true + upper = false + special = false +} + +resource "random_pet" "windows_server_public_ip_dns" { + prefix = "dns" +} + +resource "random_password" "password" { + length = 16 + special = true + lower = true + upper = true + numeric = true +} + +# Define 3 Windows Server VMs +module "windows_server" { + count = 3 + source = "Azure/compute/azurerm" + resource_group_name = azurerm_resource_group.rg.name + vnet_subnet_id = module.network.vnet_subnets[0] + is_windows_image = true + vm_hostname = "vm-${random_string.windows_server_vm_hostname.result}-${count.index}" + admin_password = random_password.password.result + vm_os_simple = "WindowsServer" + public_ip_dns = ["${random_pet.windows_server_public_ip_dns.id}-${count.index}"] + depends_on = [azurerm_resource_group.rg] +} + +module "network" { + source = "Azure/network/azurerm" + resource_group_name = azurerm_resource_group.rg.name + version = "5.2.0" + subnet_prefixes = ["10.0.1.0/24"] + subnet_names = ["subnet1"] + use_for_each = true + depends_on = [azurerm_resource_group.rg] +} \ No newline at end of file diff --git a/quickstart/101-vm-cluster-windows/outputs.tf b/quickstart/101-vm-cluster-windows/outputs.tf new file mode 100644 index 000000000..4fafa7bd0 --- /dev/null +++ b/quickstart/101-vm-cluster-windows/outputs.tf @@ -0,0 +1,19 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "windows_vm_public_names" { + value = module.windows_server[*].public_ip_dns_name +} + +output "vm_public_ip_addresses" { + value = module.windows_server[*].public_ip_address +} + +output "vm_private_ip_addresses" { + value = module.windows_server[*].network_interface_private_ip +} + +output "vm_hostnames" { + value = module.windows_server[*].vm_names +} \ No newline at end of file diff --git a/quickstart/101-vm-cluster-windows/providers.tf b/quickstart/101-vm-cluster-windows/providers.tf new file mode 100644 index 000000000..0174615d3 --- /dev/null +++ b/quickstart/101-vm-cluster-windows/providers.tf @@ -0,0 +1,20 @@ +terraform { + required_version = ">=1.0" + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>3.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} +provider "azurerm" { + features { + resource_group { + prevent_deletion_if_contains_resources = false + } + } +} \ No newline at end of file diff --git a/quickstart/101-vm-cluster-windows/readme.md b/quickstart/101-vm-cluster-windows/readme.md new file mode 100644 index 000000000..6f393709c --- /dev/null +++ b/quickstart/101-vm-cluster-windows/readme.md @@ -0,0 +1,23 @@ +# Azure Windows VM cluster + +This template deploys a Windows 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) +- [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) +- [random_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) +- [windows_server](https://registry.terraform.io/modules/Azure/compute/azurerm) +- [network](https://registry.terraform.io/modules/Azure/network/azurerm) + +## 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 | + +## Example + +To see how to run this example, see [Configure an Azure VM cluster using Terraform](https://learn.microsoft.com/azure/developer/terraform/create-vm-cluster-module). diff --git a/quickstart/101-vm-cluster-windows/variables.tf b/quickstart/101-vm-cluster-windows/variables.tf new file mode 100644 index 000000000..f53693ce1 --- /dev/null +++ b/quickstart/101-vm-cluster-windows/variables.tf @@ -0,0 +1,11 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location for all resources." +} + +variable "resource_group_name_prefix" { + type = string + default = "rg" + description = "Prefix of the resource group name that's combined with a random value so name is unique in your Azure subscription." +} \ No newline at end of file From acb0e06e9e7f25499c8dbbb040df46d71f9e95a8 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 19 Jun 2023 11:55:19 -0700 Subject: [PATCH 2/7] Updates --- quickstart/101-vm-cluster-windows/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickstart/101-vm-cluster-windows/main.tf b/quickstart/101-vm-cluster-windows/main.tf index a78dc33bd..90b8ffe0d 100644 --- a/quickstart/101-vm-cluster-windows/main.tf +++ b/quickstart/101-vm-cluster-windows/main.tf @@ -8,7 +8,7 @@ resource "azurerm_resource_group" "rg" { } resource "random_string" "windows_server_vm_hostname" { - length = 10 + length = 8 lower = true upper = false special = false From 4dceb66ffc4e20f1268faf86f75341e0251b9cb8 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Thu, 22 Jun 2023 05:58:15 -0700 Subject: [PATCH 3/7] Edits per Code Review --- quickstart/101-vm-cluster-windows/main.tf | 7 +++---- quickstart/101-vm-cluster-windows/providers.tf | 6 +----- quickstart/101-vm-cluster-windows/readme.md | 3 +++ 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/quickstart/101-vm-cluster-windows/main.tf b/quickstart/101-vm-cluster-windows/main.tf index 90b8ffe0d..5f71e491d 100644 --- a/quickstart/101-vm-cluster-windows/main.tf +++ b/quickstart/101-vm-cluster-windows/main.tf @@ -26,9 +26,9 @@ resource "random_password" "password" { numeric = true } -# Define 3 Windows Server VMs +# https://registry.terraform.io/modules/Azure/compute/azurerm module "windows_server" { - count = 3 + count = 3 # Define 3 Windows Server VMs source = "Azure/compute/azurerm" resource_group_name = azurerm_resource_group.rg.name vnet_subnet_id = module.network.vnet_subnets[0] @@ -37,9 +37,9 @@ module "windows_server" { admin_password = random_password.password.result vm_os_simple = "WindowsServer" public_ip_dns = ["${random_pet.windows_server_public_ip_dns.id}-${count.index}"] - depends_on = [azurerm_resource_group.rg] } +# https://registry.terraform.io/modules/Azure/network/azurerm module "network" { source = "Azure/network/azurerm" resource_group_name = azurerm_resource_group.rg.name @@ -47,5 +47,4 @@ module "network" { subnet_prefixes = ["10.0.1.0/24"] subnet_names = ["subnet1"] use_for_each = true - depends_on = [azurerm_resource_group.rg] } \ No newline at end of file diff --git a/quickstart/101-vm-cluster-windows/providers.tf b/quickstart/101-vm-cluster-windows/providers.tf index 0174615d3..4fd5f6ba7 100644 --- a/quickstart/101-vm-cluster-windows/providers.tf +++ b/quickstart/101-vm-cluster-windows/providers.tf @@ -12,9 +12,5 @@ terraform { } } provider "azurerm" { - features { - resource_group { - prevent_deletion_if_contains_resources = false - } - } + features {} } \ No newline at end of file diff --git a/quickstart/101-vm-cluster-windows/readme.md b/quickstart/101-vm-cluster-windows/readme.md index 6f393709c..49f3e0deb 100644 --- a/quickstart/101-vm-cluster-windows/readme.md +++ b/quickstart/101-vm-cluster-windows/readme.md @@ -8,6 +8,9 @@ This template deploys a Windows VM cluster on Azure. - [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) - [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) - [random_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) + +## Terraform modules + - [windows_server](https://registry.terraform.io/modules/Azure/compute/azurerm) - [network](https://registry.terraform.io/modules/Azure/network/azurerm) From 57033e014b4ec78451c4ea052f70f9977a19d65e Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 26 Jun 2023 13:39:57 -0700 Subject: [PATCH 4/7] Corrected links for additional info about verified modules --- quickstart/101-vm-cluster-windows/main.tf | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/quickstart/101-vm-cluster-windows/main.tf b/quickstart/101-vm-cluster-windows/main.tf index 5f71e491d..e721c1ab9 100644 --- a/quickstart/101-vm-cluster-windows/main.tf +++ b/quickstart/101-vm-cluster-windows/main.tf @@ -26,7 +26,9 @@ resource "random_password" "password" { numeric = true } -# https://registry.terraform.io/modules/Azure/compute/azurerm +# The following module is a Terraform Verified Module. +# For more information about Verified Modules, see +# https://github.com/azure/terraform-azure-modules/ module "windows_server" { count = 3 # Define 3 Windows Server VMs source = "Azure/compute/azurerm" @@ -39,7 +41,9 @@ module "windows_server" { public_ip_dns = ["${random_pet.windows_server_public_ip_dns.id}-${count.index}"] } -# https://registry.terraform.io/modules/Azure/network/azurerm +# The following module is a Terraform Verified Module. +# For more information about Verified Modules, see +# https://github.com/azure/terraform-azure-modules/ module "network" { source = "Azure/network/azurerm" resource_group_name = azurerm_resource_group.rg.name From 4cd61cf6e97f41f18ddfbb17505edc113ce4c786 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Tue, 27 Jun 2023 16:50:27 -0700 Subject: [PATCH 5/7] Added flag to allow for RG deletion when other resources are present --- quickstart/101-vm-cluster-windows/providers.tf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/quickstart/101-vm-cluster-windows/providers.tf b/quickstart/101-vm-cluster-windows/providers.tf index 4fd5f6ba7..947ac1568 100644 --- a/quickstart/101-vm-cluster-windows/providers.tf +++ b/quickstart/101-vm-cluster-windows/providers.tf @@ -12,5 +12,7 @@ terraform { } } provider "azurerm" { - features {} + features { + prevent_deletion_if_contains_resources = false + } } \ No newline at end of file From 61bd196ac406ed1f595ec5707700c0b343207d4d Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Tue, 27 Jun 2023 16:57:16 -0700 Subject: [PATCH 6/7] Added flag to allow for RG deletion when other resources are present --- quickstart/101-vm-cluster-windows/providers.tf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/quickstart/101-vm-cluster-windows/providers.tf b/quickstart/101-vm-cluster-windows/providers.tf index 947ac1568..0174615d3 100644 --- a/quickstart/101-vm-cluster-windows/providers.tf +++ b/quickstart/101-vm-cluster-windows/providers.tf @@ -13,6 +13,8 @@ terraform { } provider "azurerm" { features { - prevent_deletion_if_contains_resources = false + resource_group { + prevent_deletion_if_contains_resources = false + } } } \ No newline at end of file From e93d4e4aa6c83c3ddf88c28f6fc074321514fe93 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Thu, 6 Jul 2023 08:00:40 -0700 Subject: [PATCH 7/7] Changes per Zijie --- quickstart/101-vm-cluster-windows/main.tf | 19 ++++++++++--------- .../101-vm-cluster-windows/providers.tf | 6 +----- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/quickstart/101-vm-cluster-windows/main.tf b/quickstart/101-vm-cluster-windows/main.tf index e721c1ab9..c164d7b48 100644 --- a/quickstart/101-vm-cluster-windows/main.tf +++ b/quickstart/101-vm-cluster-windows/main.tf @@ -30,15 +30,16 @@ resource "random_password" "password" { # For more information about Verified Modules, see # https://github.com/azure/terraform-azure-modules/ module "windows_server" { - count = 3 # Define 3 Windows Server VMs - source = "Azure/compute/azurerm" - resource_group_name = azurerm_resource_group.rg.name - vnet_subnet_id = module.network.vnet_subnets[0] - is_windows_image = true - vm_hostname = "vm-${random_string.windows_server_vm_hostname.result}-${count.index}" - admin_password = random_password.password.result - vm_os_simple = "WindowsServer" - public_ip_dns = ["${random_pet.windows_server_public_ip_dns.id}-${count.index}"] + count = 3 # Define 3 Windows Server VMs + source = "Azure/compute/azurerm" + resource_group_name = azurerm_resource_group.rg.name + vnet_subnet_id = module.network.vnet_subnets[0] + is_windows_image = true + vm_hostname = "vm-${random_string.windows_server_vm_hostname.result}-${count.index}" + delete_os_disk_on_termination = true + admin_password = random_password.password.result + vm_os_simple = "WindowsServer" + public_ip_dns = ["${random_pet.windows_server_public_ip_dns.id}-${count.index}"] } # The following module is a Terraform Verified Module. diff --git a/quickstart/101-vm-cluster-windows/providers.tf b/quickstart/101-vm-cluster-windows/providers.tf index 0174615d3..4fd5f6ba7 100644 --- a/quickstart/101-vm-cluster-windows/providers.tf +++ b/quickstart/101-vm-cluster-windows/providers.tf @@ -12,9 +12,5 @@ terraform { } } provider "azurerm" { - features { - resource_group { - prevent_deletion_if_contains_resources = false - } - } + features {} } \ No newline at end of file