From 8d2fc90a25618f698168d2f9a5c99fe4215267f1 Mon Sep 17 00:00:00 2001 From: Jona Heitzer Date: Sat, 4 Oct 2025 16:57:12 +0200 Subject: [PATCH] Restructure into modules to enable persistent disk --- README.md | 4 +- main.tf | 132 ++---------------- modules/ephemeral/main.tf | 96 +++++++++++++ modules/ephemeral/outputs.tf | 4 + .../scripts}/provision-software.ps1.tpl | 19 +++ modules/ephemeral/variables.tf | 67 +++++++++ modules/persistent/main.tf | 17 +++ modules/persistent/outputs.tf | 3 + modules/persistent/variables.tf | 16 +++ outputs.tf | 6 +- providers.tf | 15 ++ variables.tf | 68 +-------- 12 files changed, 253 insertions(+), 194 deletions(-) create mode 100644 modules/ephemeral/main.tf create mode 100644 modules/ephemeral/outputs.tf rename {scripts => modules/ephemeral/scripts}/provision-software.ps1.tpl (86%) create mode 100644 modules/ephemeral/variables.tf create mode 100644 modules/persistent/main.tf create mode 100644 modules/persistent/outputs.tf create mode 100644 modules/persistent/variables.tf create mode 100644 providers.tf diff --git a/README.md b/README.md index 96c539e..d4cf04f 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ This terraform template sets up the infrastructur to enable remote gaming / stre # TODO -- [ ] Fix missing audio device on vm +- [x] Fix missing audio device on vm - [ ] Remove Tailscale plaintext auth-key from provisioning scripts and registry run entry - [ ] Replace Tailscale by native VPN - [ ] Check if public IP is even needed - [ ] Persist moonlight configuration between deployments -- [ ] Install Steam +- [x] Install Steam - [ ] Integrate budget watcher into terraform config - [ ] Is there a quicker way to download the installers? Invoke-WebRequest is insanely slow - [ ] Skip Windows OOTB tracking bullshit \ No newline at end of file diff --git a/main.tf b/main.tf index 6af76e9..8b10085 100644 --- a/main.tf +++ b/main.tf @@ -1,125 +1,17 @@ -terraform { - required_providers { - azurerm = { - source = "hashicorp/azurerm" - version = ">= 4.46" - } - } - required_version = ">= 1.0.0" +module "ephemeral" { + source = "./modules/ephemeral" + + subscription_id = var.subscription_id + prefix = var.prefix + location = var.location + tailscale_authkey = var.tailscale_authkey + datadisk_id = module.persistent.datadisk_id + vm_admin_username = var.vm_admin_username } -provider "azurerm" { - subscription_id = var.subscription_id +module "persistent" { + source = "./modules/persistent" - features {} -} - -resource "azurerm_resource_group" "rg" { - name = "${var.prefix}-rg" + prefix = var.prefix location = var.location } - -resource "azurerm_virtual_network" "vnet" { - name = "${var.prefix}-vnet" - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location - address_space = var.vnet_address_space -} - -resource "azurerm_public_ip" "pip" { - name = "${var.prefix}-pip" - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location - allocation_method = "Static" - sku = "Standard" -} - -resource "azurerm_subnet" "workload_subnet" { - name = "${var.prefix}-workload-subnet" - resource_group_name = azurerm_resource_group.rg.name - virtual_network_name = azurerm_virtual_network.vnet.name - address_prefixes = var.workload_subnet_address_prefixes -} - -resource "azurerm_network_interface" "vm_nic" { - name = "${var.prefix}-vm-nic" - location = azurerm_resource_group.rg.location - resource_group_name = azurerm_resource_group.rg.name - - ip_configuration { - name = "internal" - subnet_id = azurerm_subnet.workload_subnet.id - private_ip_address_allocation = "Dynamic" - public_ip_address_id = azurerm_public_ip.pip.id - } -} - -resource "azurerm_managed_disk" "data_disk" { - name = "${var.prefix}-winvm-datadisk" - location = azurerm_resource_group.rg.location - resource_group_name = azurerm_resource_group.rg.name - storage_account_type = "Premium_LRS" - create_option = "Empty" - disk_size_gb = var.datadisk_size_gb - - lifecycle { - prevent_destroy = true - } -} - -resource "random_password" "admin_password" { - length = 16 - special = false -} - -resource "azurerm_windows_virtual_machine" "vm" { - name = "${var.prefix}-winvm" - computer_name = var.prefix - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location - size = var.vm_size - priority = var.vm_priority - eviction_policy = var.vm_priority == "Spot" ? "Deallocate" : null - - admin_username = var.vm_admin_username - admin_password = random_password.admin_password.result - - network_interface_ids = [ - azurerm_network_interface.vm_nic.id - ] - - os_disk { - caching = "ReadWrite" - storage_account_type = "Premium_LRS" - } - - source_image_reference { - publisher = "MicrosoftWindowsDesktop" - offer = "Windows-10" - sku = "win10-22h2-pro" - version = "latest" - } -} - -resource "azurerm_virtual_machine_data_disk_attachment" "data_disk_attachment" { - managed_disk_id = azurerm_managed_disk.data_disk.id - virtual_machine_id = azurerm_windows_virtual_machine.vm.id - lun = var.datadisk_lun - caching = "ReadWrite" -} - -resource "azurerm_virtual_machine_extension" "provision_software" { - name = "provision-software" - virtual_machine_id = azurerm_windows_virtual_machine.vm.id - publisher = "Microsoft.Compute" - type = "CustomScriptExtension" - type_handler_version = "1.10" - - depends_on = [azurerm_virtual_machine_data_disk_attachment.data_disk_attachment] - - protected_settings = <