Repository: hashicorp/terraform-provider-azurerm

Issue #8195: r/(linux|windows)_virtual_machine - support for `managed_disk_id` from an existing OS disk
Created: 2020-08-21T12:52:43Z
Community Note

Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
If you are interested in working on this issue or have submitted a pull request, please leave a comment


Description
We want to build an instance from a pre-defined managed disk. You forgot to adopt this feature from azurerm_virtual_machine in the new  azurerm_*_virtual_machine.
In the legacy resource you could do the following:
  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "Attach"
    managed_disk_id = "..."
    managed_disk_type = "Standard_LRS"
  }

New or Affected Resource(s)


azurerm_linux_virtual_machine
azurerm_windows_virtual_machine

Potential Terraform Configuration

  os_disk {
    caching           = "ReadWrite"
    managed_disk_id = "..."
    storage_account_type = "Standard_LRS"
  }
References
Use case is to manage metadata of the OS disk. The details are described here:

#2568 (comment)

EDIT:
After comment from @tombuildsstuff I removed     create_option     = "Attach" from the "Potential Terraform Configuration" hcl, since "it should be managed by Terraform due to the quirks behind it"

Duplicate issue #17983: VM size change fails with a strange error (continued)
Created: 2022-08-12T14:35:19Z
Is there an existing issue for this?

 I have searched the existing issues

Community Note


Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
If you are interested in working on this issue or have submitted a pull request, please leave a comment


Terraform Version
1.2.4
AzureRM Provider Version
latest
Affected Resource(s)/Data Source(s)
azurerm_virtual_machine
Terraform Configuration Files
resource "azurerm_virtual_machine" "username-vm" {
  name                             = "username-vm"
  location                         = azurerm_resource_group.workstations-rg.location
  resource_group_name              = azurerm_resource_group.workstations-rg.name
  network_interface_ids            = [azurerm_network_interface.username-vm-nic1.id]
  primary_network_interface_id     = azurerm_network_interface.username-vm-nic1.id
  vm_size                          = "Standard_D4_v3"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true
  storage_os_disk {
    name              = "username-vm-osdisk-resized"
    caching           = "ReadWrite"
    create_option     = "Attach"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "username-vm"
    admin_username = "adm-username"
    admin_password = "secret"
  }
  os_profile_windows_config {
    provision_vm_agent = true
    timezone           = "Eastern Standard Time"
  }
  boot_diagnostics {
    enabled     = true
    storage_uri = ""
  }
}

resource "azurerm_network_interface" "username-vm-nic1" {
  name                = "username-vm-nic1"
  location            = azurerm_resource_group.workstations-rg.location
  resource_group_name = azurerm_resource_group.workstations-rg.name

  ip_configuration {
    name                          = "nic1-config1"
    subnet_id                     = azurerm_subnet.vnet-wkst-subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_public_ip" "username-vm-pip" {
  name                = "username-vm-pip"
  location            = azurerm_resource_group.workstations-rg.location
  resource_group_name = azurerm_resource_group.workstations-rg.name
  sku                 = "Basic"
  allocation_method   = "Dynamic"
  domain_name_label   = "username"
}

resource "azurerm_lb" "username-vm-lb" {
  name                = "username-vm-lb"
  location            = azurerm_resource_group.workstations-rg.location
  resource_group_name = azurerm_resource_group.workstations-rg.name
  sku                 = "Basic"

  frontend_ip_configuration {
    name                       = azurerm_public_ip.username-vm-pip.name
    public_ip_address_id       = azurerm_public_ip.username-vm-pip.id
  }
}
resource "azurerm_lb_backend_address_pool" "username-vm" {
  loadbalancer_id     = azurerm_lb.username-vm-lb.id
  name                = "username-vm"
}
resource "azurerm_network_interface_backend_address_pool_association" "username-vm" {
  network_interface_id    = azurerm_network_interface.username-vm-nic1.id
  ip_configuration_name   = "nic1-config1"
  backend_address_pool_id = azurerm_lb_backend_address_pool.username-vm.id
}
resource "azurerm_lb_probe" "username-vm-lb-RDP-Probe" {
  loadbalancer_id     = azurerm_lb.username-vm-lb.id
  name                = "RDP-Probe"
  protocol            = "Tcp"
  port                = 3389
  interval_in_seconds = 15
  number_of_probes    = 3
}
resource "azurerm_lb_rule" "FlexibleFrontEndPort" {
  loadbalancer_id     = azurerm_lb.username-vm-lb.id
  name                           = "FlexibleFrontEndPort"
  protocol                       = "Tcp"
  frontend_port                  = 443
  backend_port                   = 3389
  frontend_ip_configuration_name = azurerm_public_ip.username-vm-pip.name
  backend_address_pool_ids       = [azurerm_lb_backend_address_pool.username-vm.id]
  probe_id                       = azurerm_lb_probe.username-vm-lb-RDP-Probe.id
}
Debug Output/Panic Output
Terraform v1.2.4
on linux_amd64
Initializing plugins and modules...
azurerm_virtual_machine.username-vm: Modifying... [id=/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/workstations-rg/providers/Microsoft.Compute/virtualMachines/username-vm]
╷
│ Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="PropertyChangeNotAllowed" Message="Changing property 'osProfile' is not allowed." Target="osProfile"
│ 
│   with azurerm_virtual_machine.username-vm,
│   on azure.username-wks.tf line 1, in resource "azurerm_virtual_machine" "username-vm":
│    1: resource "azurerm_virtual_machine" "username-vm" {
│ 
╵
Operation failed: failed running terraform apply (exit 1)�

State versions created:

    username/Terraform-Azure#sv-dWA88iQoGLdEee2R (Jul 12, 2022 12:42:18 pm)

xxxxxxx_xxxxx_xxx
xxxxxxx_xxxxx_xxx a few seconds ago

Run confirmed
Expected Behaviour
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
azurerm_virtual_machine.username-vm will be updated in-place
~ resource "azurerm_virtual_machine" "username-vm" {
id                               = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/workstations-rg/providers/Microsoft.Compute/virtualMachines/username-vm"
name                             = "username-vm"
~ vm_size                          = "Standard_D4s_v3" -> "Standard_D4_v3"
# (7 unchanged attributes hidden)
    # (5 unchanged blocks hidden)
}

Plan: 0 to add, 1 to change, 0 to destroy.
Actual Behaviour
The run fails with an error that is unrelated to the VM size and instead it is about something that I am not even trying to modify:

Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status= Code="PropertyChangeNotAllowed" Message="Changing property 'osProfile' is not allowed." Target="osProfile"

Steps to Reproduce
No response
Important Factoids
No response
References
I previously opened #17610 and the advice provided was to use azurerm_windows_virtual_machine instead of azurerm_virtual_machine.
I tried that, however, it has one very important limitation: it does not support attaching existing disks. Given that almost all of my VMs actually use attached disks, using azurerm_windows_virtual_machine is not an option.
Is there anything else that can be done?

Closing comment (2023-06-02T18:12:34Z): @rumenavramov Thanks for taking the time to submit this issue. It looks like this is a duplicate of #8195. We like to try to keep discussions consolidated, so we’re going to close this newer issue in favor of #8195.