Skip to content

Commit 1ec8670

Browse files
authored
Merge pull request #20 from MicrosoftCloudEssentials-LearningHub/virtual-desktop
Azure virtual desktop Terraform templates
2 parents 44f484b + d1a95b4 commit 1ec8670

File tree

6 files changed

+247
-0
lines changed

6 files changed

+247
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Terraform Template - Azure Virtual Desktop
2+
3+
Costa Rica
4+
5+
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
6+
[brown9804](https://github.com/brown9804)
7+
8+
Last updated: 2025-03-24
9+
10+
----------
11+
12+
> This template contains Terraform configurations to create and manage an Azure Virtual Desktop (AVD) environment with dependencies on a Resource Group, Virtual Network, and Subnet. Below is a description of the files and the variables used in this template.
13+
14+
> [!NOTE]
15+
> The AVD environment depends on the Resource Group, Virtual Network, and Subnet. Terraform ensures that these resources are created before the AVD environment by using the `depends_on` argument in the configuration.
16+
17+
<p align="center">
18+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/f3112a03-a3a1-4f10-80a1-42ef8d1f29f6">
19+
20+
</p>
21+
22+
## File Descriptions
23+
24+
- **main.tf**: Contains the main configuration for creating the Azure Virtual Desktop environment and the resources it depends on.
25+
- **variables.tf**: Defines the input variables used in the Terraform configuration.
26+
- **provider.tf**: Configures the Azure provider to interact with Azure resources.
27+
- **terraform.tfvars**: Provides default values for the variables defined in `variables.tf`.
28+
- **outputs.tf**: Defines the outputs of the Terraform configuration, such as the AVD workspace name, host pool name, and associated Resource Group.
29+
30+
## Variables
31+
32+
Below is a list of variables used in this template, their expected values, types, and examples:
33+
34+
| Variable Name | Description | Type | Example Value |
35+
|---------------------------|--------------------------------------------------|--------|-----------------------|
36+
| `resource_group_name` | The name of the Azure Resource Group to associate the AVD environment with. | string | `"my-resource-group"` |
37+
| `location` | The Azure region where the Resource Group will be created. | string | `"East US"` |
38+
| `virtual_network_name` | The name of the Azure Virtual Network to create. | string | `"my-vnet"` |
39+
| `subnet_name` | The name of the Subnet to create within the Virtual Network. | string | `"my-subnet"` |
40+
| `workspace_name` | The name of the Azure Virtual Desktop Workspace to create. | string | `"my-avd-workspace"` |
41+
| `host_pool_name` | The name of the Azure Virtual Desktop Host Pool to create. | string | `"my-avd-hostpool"` |
42+
| `vm_size` | The size of the Virtual Machines for the session hosts. | string | `"Standard_DS2_v2"` |
43+
| `admin_username` | The administrator username for the session hosts. | string | `"adminuser"` |
44+
| `admin_password` | The administrator password for the session hosts. | string | `"P@ssw0rd1234"` |
45+
| `tags` | A map of tags to assign to the resources. | map | `{ "env": "dev" }` |
46+
47+
<div align="center">
48+
<h3 style="color: #4CAF50;">Total Visitors</h3>
49+
<img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
50+
</div>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
resource "azurerm_resource_group" "example" {
2+
name = var.resource_group_name
3+
location = var.location
4+
}
5+
6+
resource "azurerm_virtual_network" "example" {
7+
name = var.virtual_network_name
8+
resource_group_name = azurerm_resource_group.example.name
9+
location = azurerm_resource_group.example.location
10+
address_space = ["10.0.0.0/16"]
11+
}
12+
13+
resource "azurerm_subnet" "example" {
14+
name = var.subnet_name
15+
resource_group_name = azurerm_resource_group.example.name
16+
virtual_network_name = azurerm_virtual_network.example.name
17+
address_prefixes = ["10.0.1.0/24"]
18+
}
19+
20+
resource "azurerm_virtual_desktop_workspace" "example" {
21+
name = var.workspace_name
22+
resource_group_name = azurerm_resource_group.example.name
23+
location = azurerm_resource_group.example.location
24+
friendly_name = "${var.workspace_name} Friendly"
25+
description = "${var.workspace_name} Description"
26+
}
27+
28+
resource "azurerm_virtual_desktop_host_pool" "example" {
29+
name = var.host_pool_name
30+
resource_group_name = azurerm_resource_group.example.name
31+
location = azurerm_resource_group.example.location
32+
type = "Pooled"
33+
load_balancer_type = "BreadthFirst"
34+
preferred_app_group_type = "Desktop"
35+
friendly_name = "${var.host_pool_name} Friendly"
36+
description = "${var.host_pool_name} Description"
37+
}
38+
39+
resource "azurerm_virtual_desktop_application_group" "example" {
40+
name = "${var.host_pool_name}-appgroup"
41+
resource_group_name = azurerm_resource_group.example.name
42+
location = azurerm_resource_group.example.location
43+
host_pool_id = azurerm_virtual_desktop_host_pool.example.id
44+
type = "Desktop"
45+
friendly_name = "${var.host_pool_name} App Group"
46+
description = "${var.host_pool_name} App Group Description"
47+
}
48+
49+
resource "azurerm_virtual_desktop_workspace_application_group_association" "example" {
50+
workspace_id = azurerm_virtual_desktop_workspace.example.id
51+
application_group_id = azurerm_virtual_desktop_application_group.example.id
52+
}
53+
54+
resource "azurerm_network_interface" "example" {
55+
count = 2
56+
name = "${var.host_pool_name}-nic-${count.index}"
57+
resource_group_name = azurerm_resource_group.example.name
58+
location = azurerm_resource_group.example.location
59+
60+
ip_configuration {
61+
name = "internal"
62+
subnet_id = azurerm_subnet.example.id
63+
private_ip_address_allocation = "Dynamic"
64+
}
65+
}
66+
67+
resource "azurerm_windows_virtual_machine" "example" {
68+
count = 2
69+
name = "${var.host_pool_name}-vm-${count.index}"
70+
resource_group_name = azurerm_resource_group.example.name
71+
location = azurerm_resource_group.example.location
72+
size = var.vm_size
73+
admin_username = var.admin_username
74+
admin_password = var.admin_password
75+
network_interface_ids = [element(azurerm_network_interface.example.*.id, count.index)]
76+
77+
os_disk {
78+
name = "${var.host_pool_name}-osdisk-${count.index}"
79+
caching = "ReadWrite"
80+
storage_account_type = "Standard_LRS"
81+
}
82+
83+
source_image_reference {
84+
publisher = "MicrosoftWindowsServer"
85+
offer = "WindowsServer"
86+
sku = "2019-Datacenter"
87+
version = "latest"
88+
}
89+
90+
tags = var.tags
91+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "workspace_name" {
2+
value = azurerm_virtual_desktop_workspace.example.name
3+
}
4+
5+
output "host_pool_name" {
6+
value = azurerm_virtual_desktop_host_pool.example.name
7+
}
8+
9+
output "application_group_name" {
10+
value = azurerm_virtual_desktop_application_group.example.name
11+
}
12+
13+
output "vm_names" {
14+
value = [for vm in azurerm_windows_virtual_machine.example : vm.name]
15+
}
16+
17+
output "resource_group_name" {
18+
value = azurerm_resource_group.example.name
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# provider.tf
2+
# This file configures the Azure provider to interact with Azure resources.
3+
# It specifies the required provider and its version, along with provider-specific configurations.
4+
5+
terraform {
6+
required_version = ">= 1.8, < 2.0"
7+
# Specify the required provider and its version
8+
required_providers {
9+
azurerm = {
10+
source = "hashicorp/azurerm" # Source of the AzureRM provider
11+
version = "~> 4.16.0" # Version of the AzureRM provider
12+
}
13+
}
14+
}
15+
16+
provider "azurerm" {
17+
features {} # Enable all features for the AzureRM provider
18+
subscription_id = var.subscription_id # Use the subscription ID variable
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource_group_name = "my-resource-group"
2+
location = "East US 2"
3+
virtual_network_name = "my-vnetbr"
4+
subnet_name = "my-subnetbr"
5+
workspace_name = "my-avd-workspacebr"
6+
host_pool_name = "vmhpoolbr"
7+
vm_size = "Standard_DS2_v2"
8+
admin_username = "adminuser"
9+
admin_password = "P@ssw0rd1234"
10+
subscription_id = "your-subscription-id"
11+
tags = {
12+
env = "dev"
13+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
variable "resource_group_name" {
2+
description = "The name of the Azure Resource Group to associate the AVD environment with."
3+
type = string
4+
}
5+
6+
variable "location" {
7+
description = "The Azure region where the Resource Group will be created."
8+
type = string
9+
}
10+
11+
variable "virtual_network_name" {
12+
description = "The name of the Azure Virtual Network to create."
13+
type = string
14+
}
15+
16+
variable "subnet_name" {
17+
description = "The name of the Subnet to create within the Virtual Network."
18+
type = string
19+
}
20+
21+
variable "workspace_name" {
22+
description = "The name of the Azure Virtual Desktop Workspace to create."
23+
type = string
24+
}
25+
26+
variable "host_pool_name" {
27+
description = "The name of the Azure Virtual Desktop Host Pool to create."
28+
type = string
29+
}
30+
31+
variable "vm_size" {
32+
description = "The size of the Virtual Machines for the session hosts."
33+
type = string
34+
}
35+
36+
variable "admin_username" {
37+
description = "The administrator username for the session hosts."
38+
type = string
39+
}
40+
41+
variable "admin_password" {
42+
description = "The administrator password for the session hosts."
43+
type = string
44+
}
45+
46+
variable "tags" {
47+
description = "A map of tags to assign to the resources."
48+
type = map(string)
49+
}
50+
51+
52+
variable "subscription_id" {
53+
description = "The subscription ID for Azure"
54+
type = string
55+
}

0 commit comments

Comments
 (0)