1

I'm provisioning a VM on Azure using this terraform code:

resource "azurerm_linux_virtual_machine" "vm" {
  name                            = "vm"
  location                        = azurerm_resource_group.rg.location
  resource_group_name             = azurerm_resource_group.rg.name
  network_interface_ids           = [azurerm_network_interface.nic.id]
  size                            = "Standard_NV6ads_A10_v5"
  admin_username                  = "ubuntu"
  admin_password                  = "xxxx"
  disable_password_authentication = false
  custom_data = base64encode(templatefile("custom-data.tpl", {
    account_name        = azurerm_storage_account.storage_account.name
    container_name      = azurerm_storage_container.blob_container.name
    storage_account_key = azurerm_storage_account.storage_account.primary_access_key
    user_name           = "ubuntu"
  }))

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
    disk_size_gb         = 50
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "ubuntu-24_04-lts"
    sku       = "server"
    version   = "latest"
  }

  identity {
    type = "SystemAssigned"
  }
}

The custom-data.tpl script contains only the code to install xrdp:

#!/bin/bash
sudo wget https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update -y
sudo apt-get install -y ubuntu-desktop xrdp

systemctl enable xrdp
systemctl start xrdp

The thing is, after I log in via rdp I pull code from my git repository and install nvidia drivers. Sometimes after I reboot there's no repo in my home catalog and the drivers are also nowhere to be found. Aren't drives attached to VMs supposed to be persistent? Or maybe I'm doing something wrong?

1 Answer 1

1

You haven't named your OS disk... I don't know if that would play a factor, but I always create mine like this:

os_disk {
    name                 = "${local.jumpbox_vm_name}-os-disk"
    caching              = "ReadWrite"
    storage_account_type = "Premium_LRS"
  }

So maybe if you are re-applying terraform "over the top" of this TF isn't recognising that that VM already exists/persists and is destroying/recreating it. So it looks like the same VM to you, but its actually been rebuilt? Maybe you are making other changes that are causing it to be rebuilt as well. Keep an eye on the output of your TF plan and see if that is the case.

2
  • The thing is the purge happens even after vm reboot. But I think you may be onto something, I'll name my disk
    – Marek M.
    Commented Nov 2 at 10:42
  • 1
    @MarekM. if it works please remember to mark this as the correct answer Commented Nov 3 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.