Answers: <p>You haven't named your OS disk... I don't know if that would play a factor, but I always create mine like this:</p>
  <pre><code>os_disk {
      name                 = &quot;${local.jumpbox_vm_name}-os-disk&quot;
      caching              = &quot;ReadWrite&quot;
      storage_account_type = &quot;Premium_LRS&quot;
    }
  </code></pre>
  <p>So maybe if you are re-applying terraform &quot;over the top&quot; 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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-11-02T10:42:44.770Z
Answers: The thing is the purge happens even after vm reboot. But I think you may be onto something, I'll name my disk
Answers: Marek M.
Answers: CC BY-SA 4.0
Answers: 2024-11-03T20:35:20.030Z
Answers: @MarekM. if it works please remember to mark this as the correct answer
Answers: Mark McGookin
Answers: CC BY-SA 4.0
Answers: 2024-10-31T08:24:18.143Z
Answers: 2024-10-31T08:24:18.143Z
Answers: Mark McGookin
Body: <p>I'm provisioning a VM on Azure using this terraform code:</p>
  <pre><code>resource &quot;azurerm_linux_virtual_machine&quot; &quot;vm&quot; {
    name                            = &quot;vm&quot;
    location                        = azurerm_resource_group.rg.location
    resource_group_name             = azurerm_resource_group.rg.name
    network_interface_ids           = [azurerm_network_interface.nic.id]
    size                            = &quot;Standard_NV6ads_A10_v5&quot;
    admin_username                  = &quot;ubuntu&quot;
    admin_password                  = &quot;xxxx&quot;
    disable_password_authentication = false
    custom_data = base64encode(templatefile(&quot;custom-data.tpl&quot;, {
      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           = &quot;ubuntu&quot;
    }))
  
    os_disk {
      caching              = &quot;ReadWrite&quot;
      storage_account_type = &quot;Standard_LRS&quot;
      disk_size_gb         = 50
    }
  
    source_image_reference {
      publisher = &quot;Canonical&quot;
      offer     = &quot;ubuntu-24_04-lts&quot;
      sku       = &quot;server&quot;
      version   = &quot;latest&quot;
    }
  
    identity {
      type = &quot;SystemAssigned&quot;
    }
  }
  </code></pre>
  <p>The <code>custom-data.tpl</code> script contains only the code to install xrdp:</p>
  <pre><code>#!/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
  </code></pre>
  <p>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?</p>
  
Tags: <terraform><azure><ubuntu>
Title: Azure virtual machine not persisting state after reboot
UserDisplayName: Marek M.
ThreadText: # Azure virtual machine not persisting state after reboot
  
  (Asked by: username_0 on 2024-10-30)
  
  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?
  
  **Tags:** terraform, azure, ubuntu
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-10-31. Score: 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.
  
  > **Comments:**
  > **username_0**: The thing is the purge happens even after vm reboot. But I think you may be onto something, I'll name my disk  
  > **username_1**: @MarekM. if it works please remember to mark this as the correct answer  
  
  ---
  
  