From d0af975d18142326eff6f807db57d9e88a3cc58d Mon Sep 17 00:00:00 2001 From: Jen Sheerin Date: Fri, 25 Mar 2022 14:54:22 -0400 Subject: [PATCH] refactor code --- .../101-azure-virtual-desktop-anf/main.tf | 25 ++++++---- .../101-azure-virtual-desktop-anf/outputs.tf | 8 ++++ .../variables.tf | 11 ++++- .../101-azure-virtual-desktop/afstorage.tf | 4 +- quickstart/101-azure-virtual-desktop/host.tf | 15 ++++-- .../101-azure-virtual-desktop/loganalytics.tf | 2 +- quickstart/101-azure-virtual-desktop/main.tf | 29 ++++++------ .../101-azure-virtual-desktop/networking.tf | 4 +- .../101-azure-virtual-desktop/outputs.tf | 33 +++++++++---- quickstart/101-azure-virtual-desktop/rbac.tf | 2 +- quickstart/101-azure-virtual-desktop/sig.tf | 21 ++++++--- .../101-azure-virtual-desktop/variables.tf | 46 +++++++++++++++---- 12 files changed, 139 insertions(+), 61 deletions(-) diff --git a/quickstart/101-azure-virtual-desktop-anf/main.tf b/quickstart/101-azure-virtual-desktop-anf/main.tf index cbcb6d6a..41fc31fc 100644 --- a/quickstart/101-azure-virtual-desktop-anf/main.tf +++ b/quickstart/101-azure-virtual-desktop-anf/main.tf @@ -1,7 +1,11 @@ -# Create AVD Resource Group +# Randomization of resource group name. +# Resource group name is output when execution plan is applied. +resource "random_pet" "rg-name" { + prefix = var.resource_group_name_prefix +} resource "azurerm_resource_group" "rg" { - name = var.rg_name - location = var.deploy_location + name = random_pet.rg-name.id + location = var.resource_group_location } # Create AVD workspace @@ -13,10 +17,6 @@ resource "azurerm_virtual_desktop_workspace" "workspace" { description = "${var.prefix} Workspace" } -resource "time_rotating" "avd_token" { - rotation_days = 30 -} - # Create AVD host pool resource "azurerm_virtual_desktop_host_pool" "hostpool" { resource_group_name = azurerm_resource_group.rg.name @@ -30,9 +30,14 @@ resource "azurerm_virtual_desktop_host_pool" "hostpool" { maximum_sessions_allowed = 16 load_balancer_type = "DepthFirst" #[BreadthFirst DepthFirst] - registration_info { - expiration_date = time_rotating.avd_token.rotation_rfc3339 - } + +# Create registration info +resource "time_rotating" "avd_token" { + rotation_days = 30 +} +resource "azurerm_virtual_desktop_host_pool_registration_info" "registrationinfo" { + hostpool_id = azurerm_virtual_desktop_host_pool.hostpool.id + expiration_date = time_rotating.avd_token.rfc3339 } # Create AVD DAG diff --git a/quickstart/101-azure-virtual-desktop-anf/outputs.tf b/quickstart/101-azure-virtual-desktop-anf/outputs.tf index ec1f9624..96f13884 100644 --- a/quickstart/101-azure-virtual-desktop-anf/outputs.tf +++ b/quickstart/101-azure-virtual-desktop-anf/outputs.tf @@ -3,6 +3,14 @@ output "resource_group_name" { value = azurerm_resource_group.rg.name } +output "azurerm_virtual_desktop_application_group" { + value = azurerm_virtual_desktop_application_group.dag.name +} + +output "azurerm_virtual_desktop_workspace" { + value = azurerm_virtual_desktop_workspace.workspace.name +} + output "location" { description = "The Azure region" value = azurerm_resource_group.rg.location diff --git a/quickstart/101-azure-virtual-desktop-anf/variables.tf b/quickstart/101-azure-virtual-desktop-anf/variables.tf index 9428c28f..9562b9b1 100644 --- a/quickstart/101-azure-virtual-desktop-anf/variables.tf +++ b/quickstart/101-azure-virtual-desktop-anf/variables.tf @@ -1,3 +1,12 @@ +variable "resource_group_name_prefix" { + default = "rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "resource_group_location" { + default = "eastus" + description = "Location of the resource group." +} variable "rg_name" { type = string default = "avd-resources-rg" @@ -6,7 +15,7 @@ variable "rg_name" { variable "deploy_location" { type = string - default = "east us" + default = "eastus" description = "The Azure Region in which all resources in this example should be created." } diff --git a/quickstart/101-azure-virtual-desktop/afstorage.tf b/quickstart/101-azure-virtual-desktop/afstorage.tf index d674a34e..cadeb47a 100644 --- a/quickstart/101-azure-virtual-desktop/afstorage.tf +++ b/quickstart/101-azure-virtual-desktop/afstorage.tf @@ -1,7 +1,7 @@ ## Create a Resource Group for Storage resource "azurerm_resource_group" "rg_storage" { - location = "east us" - name = "af-storage-rg" + location = var.deploy_location + name = var.rg_stor } # generate a random string (consisting of four characters) diff --git a/quickstart/101-azure-virtual-desktop/host.tf b/quickstart/101-azure-virtual-desktop/host.tf index aa3285ff..b60b558b 100644 --- a/quickstart/101-azure-virtual-desktop/host.tf +++ b/quickstart/101-azure-virtual-desktop/host.tf @@ -1,5 +1,5 @@ locals { - registration_token = azurerm_virtual_desktop_host_pool.hostpool.registration_info[0].token + registration_token = azurerm_virtual_desktop_host_pool_registration_info.registrationinfo.token } resource "random_string" "AVD_local_password" { @@ -10,11 +10,16 @@ resource "random_string" "AVD_local_password" { override_special = "*!@#?" } +resource "azurerm_resource_group" "rg" { + name = var.rg + location = var.resource_group_location +} + resource "azurerm_network_interface" "avd_vm_nic" { count = var.rdsh_count name = "${var.prefix}-${count.index + 1}-nic" - resource_group_name = var.rg_name - location = var.deploy_location + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location ip_configuration { name = "nic${count.index + 1}_config" @@ -30,8 +35,8 @@ resource "azurerm_network_interface" "avd_vm_nic" { resource "azurerm_windows_virtual_machine" "avd_vm" { count = var.rdsh_count name = "${var.prefix}-${count.index + 1}" - resource_group_name = var.rg_name - location = var.deploy_location + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location size = var.vm_size network_interface_ids = ["${azurerm_network_interface.avd_vm_nic.*.id[count.index]}"] provision_vm_agent = true diff --git a/quickstart/101-azure-virtual-desktop/loganalytics.tf b/quickstart/101-azure-virtual-desktop/loganalytics.tf index 4bf5e8ff..df69d917 100644 --- a/quickstart/101-azure-virtual-desktop/loganalytics.tf +++ b/quickstart/101-azure-virtual-desktop/loganalytics.tf @@ -1,5 +1,5 @@ resource "azurerm_resource_group" "log" { - name = "${var.shared}-resources" + name = var.rg_shared_name location = var.deploy_location } diff --git a/quickstart/101-azure-virtual-desktop/main.tf b/quickstart/101-azure-virtual-desktop/main.tf index cbcb6d6a..fa318567 100644 --- a/quickstart/101-azure-virtual-desktop/main.tf +++ b/quickstart/101-azure-virtual-desktop/main.tf @@ -1,26 +1,22 @@ -# Create AVD Resource Group -resource "azurerm_resource_group" "rg" { +# Resource group name is output when execution plan is applied. +resource "azurerm_resource_group" "sh" { name = var.rg_name - location = var.deploy_location + location = var.resource_group_location } # Create AVD workspace resource "azurerm_virtual_desktop_workspace" "workspace" { name = var.workspace - resource_group_name = azurerm_resource_group.rg.name - location = var.deploy_location + resource_group_name = azurerm_resource_group.sh.name + location = azurerm_resource_group.sh.location friendly_name = "${var.prefix} Workspace" description = "${var.prefix} Workspace" } -resource "time_rotating" "avd_token" { - rotation_days = 30 -} - # Create AVD host pool resource "azurerm_virtual_desktop_host_pool" "hostpool" { - resource_group_name = azurerm_resource_group.rg.name - location = var.deploy_location + resource_group_name = azurerm_resource_group.sh.name + location = azurerm_resource_group.sh.location name = var.hostpool friendly_name = var.hostpool validate_environment = true @@ -29,17 +25,18 @@ resource "azurerm_virtual_desktop_host_pool" "hostpool" { type = "Pooled" maximum_sessions_allowed = 16 load_balancer_type = "DepthFirst" #[BreadthFirst DepthFirst] +} - registration_info { - expiration_date = time_rotating.avd_token.rotation_rfc3339 - } +resource "azurerm_virtual_desktop_host_pool_registration_info" "registrationinfo" { + hostpool_id = azurerm_virtual_desktop_host_pool.hostpool.id + expiration_date = var.rfc3339 } # Create AVD DAG resource "azurerm_virtual_desktop_application_group" "dag" { - resource_group_name = azurerm_resource_group.rg.name + resource_group_name = azurerm_resource_group.sh.name host_pool_id = azurerm_virtual_desktop_host_pool.hostpool.id - location = var.deploy_location + location = azurerm_resource_group.sh.location type = "Desktop" name = "${var.prefix}-dag" friendly_name = "Desktop AppGroup" diff --git a/quickstart/101-azure-virtual-desktop/networking.tf b/quickstart/101-azure-virtual-desktop/networking.tf index dddcb41f..500fcafa 100644 --- a/quickstart/101-azure-virtual-desktop/networking.tf +++ b/quickstart/101-azure-virtual-desktop/networking.tf @@ -44,13 +44,13 @@ data "azurerm_virtual_network" "ad_vnet_data" { } resource "azurerm_virtual_network_peering" "peer1" { - name = "peer_avd_ad" + name = "peer_avdspoke_ad" resource_group_name = var.rg_name virtual_network_name = azurerm_virtual_network.vnet.name remote_virtual_network_id = data.azurerm_virtual_network.ad_vnet_data.id } resource "azurerm_virtual_network_peering" "peer2" { - name = "peer_ad_avd" + name = "peer_ad_avdspoke" resource_group_name = var.ad_rg virtual_network_name = var.ad_vnet remote_virtual_network_id = azurerm_virtual_network.vnet.id diff --git a/quickstart/101-azure-virtual-desktop/outputs.tf b/quickstart/101-azure-virtual-desktop/outputs.tf index ec1f9624..735af5ad 100644 --- a/quickstart/101-azure-virtual-desktop/outputs.tf +++ b/quickstart/101-azure-virtual-desktop/outputs.tf @@ -1,19 +1,39 @@ -output "resource_group_name" { - description = "Name of the Resource group created" +output "azure_virtual_desktop_compute_resource_group" { + description = "Name of the Resource group in which to deploy session host" value = azurerm_resource_group.rg.name } +output "azure_virtual_desktop_host_pool" { + description = "Name of the Azure Virtual Desktop host pool" + value = azurerm_virtual_desktop_host_pool.hostpool.name +} + +output "azurerm_virtual_desktop_application_group" { + description = "Name of the Azure Virtual Desktop DAG" + value = azurerm_virtual_desktop_application_group.dag.name +} + +output "azurerm_virtual_desktop_workspace" { + description = "Name of the Azure Virtual Desktop workspace" + value = azurerm_virtual_desktop_workspace.workspace.name +} + output "location" { description = "The Azure region" value = azurerm_resource_group.rg.location } +output "storage_account" { + description = "Storage account for Profiles" + value = azurerm_storage_account.storage.name +} + output "storage_account_share" { description = "Name of the Azure File Share created for FSLogix" value = azurerm_storage_share.FSShare.name } -output "rdshcount" { +output "session_host_count" { description = "The number of VMs created" value = var.rdsh_count } @@ -26,14 +46,9 @@ output "dnsservers" { output "vnetrange" { description = "Address range for deployment vnet" value = azurerm_virtual_network.vnet.address_space - -} -output "avdusers" { - description = "AVD users" - value = azuread_group.aad_group.members } -output "aadgroupname" { +output "AVD_user_groupname" { description = "Azure Active Directory Group for AVD users" value = azuread_group.aad_group.display_name } diff --git a/quickstart/101-azure-virtual-desktop/rbac.tf b/quickstart/101-azure-virtual-desktop/rbac.tf index 7da8669d..49ae7588 100644 --- a/quickstart/101-azure-virtual-desktop/rbac.tf +++ b/quickstart/101-azure-virtual-desktop/rbac.tf @@ -8,7 +8,7 @@ data "azurerm_role_definition" "role" { # access an existing built-in role } resource "azuread_group" "aad_group" { - display_name = var.aad_group_name + display_name = var.aad_group_name security_enabled = true } diff --git a/quickstart/101-azure-virtual-desktop/sig.tf b/quickstart/101-azure-virtual-desktop/sig.tf index 9ec2d2f3..acab5246 100644 --- a/quickstart/101-azure-virtual-desktop/sig.tf +++ b/quickstart/101-azure-virtual-desktop/sig.tf @@ -1,14 +1,23 @@ resource "azurerm_resource_group" "sigrg" { location = var.deploy_location - name = "${var.prefix}-rg" + name = var.rg_shared_name } +# generate a random string (consisting of four characters) +# https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string +resource "random_string" "rando" { + length = 4 + upper = false + special = false +} + + # Creates Shared Image Gallery # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/shared_image_gallery resource "azurerm_shared_image_gallery" "sig" { - name = "AVDTFsig" - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location + name = "sig${random_string.random.id}" + resource_group_name = azurerm_resource_group.sigrg.name + location = azurerm_resource_group.sigrg.location description = "Shared images" tags = { @@ -22,8 +31,8 @@ resource "azurerm_shared_image_gallery" "sig" { resource "azurerm_shared_image" "example" { name = "avd-image" gallery_name = azurerm_shared_image_gallery.sig.name - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.sigrg.name + location = azurerm_resource_group.sigrg.location os_type = "Windows" identifier { diff --git a/quickstart/101-azure-virtual-desktop/variables.tf b/quickstart/101-azure-virtual-desktop/variables.tf index 4d4064ba..e1b6dded 100644 --- a/quickstart/101-azure-virtual-desktop/variables.tf +++ b/quickstart/101-azure-virtual-desktop/variables.tf @@ -1,12 +1,35 @@ +variable "resource_group_location" { + default = "eastus" + description = "Location of the resource group." +} + +variable "rg" { + type = string + default = "rg-avd-compute" + description = "Name of the Resource group in which to deploy session host" +} + variable "rg_name" { type = string - default = "avd-resources-rg" - description = "Name of the Resource group in which to deploy these resources" + default = "rg-avd-resources" + description = "Name of the Resource group in which to deploy service objects" +} + +variable "rg_stor" { + type = string + default = "rg-avd-storage" + description = "Name of the Resource group in which to deploy storage" +} + +variable "rg_shared_name" { + type = string + default = "rg-shared-resources" + description = "Name of the Resource group in which to deploy shared resources" } variable "deploy_location" { type = string - default = "east us" + default = "eastus" description = "The Azure Region in which all resources in this example should be created." } @@ -28,6 +51,13 @@ variable "ad_vnet" { description = "Name of domain controller vnet" } +variable "rfc3339" { + type = string + default = "2022-03-30T12:43:13Z" + description = "Registration token expiration" +} + + variable "dns_servers" { type = list(string) default = ["10.0.1.4", "168.63.129.16"] @@ -36,12 +66,12 @@ variable "dns_servers" { variable "vnet_range" { type = list(string) - default = ["10.1.0.0/16"] + default = ["10.2.0.0/16"] description = "Address range for deployment VNet" } variable "subnet_range" { type = list(string) - default = ["10.1.0.0/24"] + default = ["10.2.0.0/24"] description = "Address range for session host subnet" } @@ -54,8 +84,8 @@ variable "ad_rg" { variable "avd_users" { description = "AVD users" default = [ - "avduser01@infra.local", - "avduser01@infra.local" + "avduser01@contoso.net", + "avduser02@contoso.net" ] } @@ -84,7 +114,7 @@ variable "domain_name" { variable "domain_user_upn" { type = string - default = "admin" # do not include domain name as this is appended + default = "domainjoineruser" # do not include domain name as this is appended description = "Username for domain join (do not include domain name as this is appended)" }