hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.6k stars 4.64k forks source link

osProfile is attempted to change on apply and fails; doesn't show change in plan #24533

Open unkinected opened 9 months ago

unkinected commented 9 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.6.6

AzureRM Provider Version

3.86.0

Affected Resource(s)/Data Source(s)

azurerm_virtual_machine

Terraform Configuration Files

resource "azurerm_virtual_machine" "windows_vm" {
    name                  = "server"
    license_type          = "Windows_Server"
    location              = "eastus2"
    resource_group_name   = "mygroup"
    vm_size               = "Standard_DS3_v2"
    availability_set_id   = "myavset"

    os_profile {
        computer_name  = "servername"
        admin_password = "blahblah"
        admin_username = "blahblah"
    }
    os_profile_windows_config {
        provision_vm_agent = true

    }
    network_interface_ids = [
        "/subscriptions/subid/resourceGroups/myrg/providers/Microsoft.Network/networkInterfaces/nic_0"
    ]
    boot_diagnostics {
        enabled             = true
        storage_uri = "https://vxcvmdiagnostics.blob.core.windows.net"
    }
    storage_os_disk {
        caching           = "ReadWrite"
        create_option     = "Attach"
        name              = "disk"
        managed_disk_id   = "diskid"
        managed_disk_type = "Premium_LRS"
        os_type           = "Windows"
    }

    depends_on = [
        azurerm_network_interface.vm_nics
    ]
    lifecycle {
        ignore_changes = [ 
            availability_set_id, 
            os_profile, 
            os_profile_windows_config ]
    }
}

Debug Output/Panic Output

Terraform v1.6.6
on linux_amd64
Initializing plugins and modules...
{"@level":"info","@message":"Terraform 1.6.6","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:04.346448Z","terraform":"1.6.6","type":"version","ui":"1.2"}
{"@level":"info","@message":"local_file.ApplicationHost-MIMEandGzip: Plan to create","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.658685Z","change":{"resource":{"addr":"local_file.ApplicationHost-MIMEandGzip","module":"","resource":"local_file.ApplicationHost-MIMEandGzip","implied_provider":"local","resource_type":"local_file","resource_name":"ApplicationHost-MIMEandGzip","resource_key":null},"action":"create"},"type":"planned_change"}
{"@level":"info","@message":"local_file.ApplicationHost-MIME: Plan to create","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.658815Z","change":{"resource":{"addr":"local_file.ApplicationHost-MIME","module":"","resource":"local_file.ApplicationHost-MIME","implied_provider":"local","resource_type":"local_file","resource_name":"ApplicationHost-MIME","resource_key":null},"action":"create"},"type":"planned_change"}
{"@level":"info","@message":"azurerm_storage_account.redactedcloudwitness: Plan to update","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.658868Z","change":{"resource":{"addr":"azurerm_storage_account.redactedcloudwitness","module":"","resource":"azurerm_storage_account.redactedcloudwitness","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedcloudwitness","resource_key":null},"action":"update"},"type":"planned_change"}
{"@level":"info","@message":"azurerm_storage_account.redactedazofficebackup: Plan to update","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.658906Z","change":{"resource":{"addr":"azurerm_storage_account.redactedazofficebackup","module":"","resource":"azurerm_storage_account.redactedazofficebackup","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedazofficebackup","resource_key":null},"action":"update"},"type":"planned_change"}
{"@level":"info","@message":"azurerm_storage_account.redactedcorpfs: Plan to update","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.658940Z","change":{"resource":{"addr":"azurerm_storage_account.redactedcorpfs","module":"","resource":"azurerm_storage_account.redactedcorpfs","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedcorpfs","resource_key":null},"action":"update"},"type":"planned_change"}
{"@level":"info","@message":"azurerm_storage_account.redactedappservices: Plan to update","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.658983Z","change":{"resource":{"addr":"azurerm_storage_account.redactedappservices","module":"","resource":"azurerm_storage_account.redactedappservices","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedappservices","resource_key":null},"action":"update"},"type":"planned_change"}
{"@level":"info","@message":"azurerm_storage_account.redactedazfs: Plan to update","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.659043Z","change":{"resource":{"addr":"azurerm_storage_account.redactedazfs","module":"","resource":"azurerm_storage_account.redactedazfs","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedazfs","resource_key":null},"action":"update"},"type":"planned_change"}
{"@level":"info","@message":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]: Plan to update","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.659100Z","change":{"resource":{"addr":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]","module":"","resource":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]","implied_provider":"azurerm","resource_type":"azurerm_windows_virtual_machine","resource_name":"vms","resource_key":"cus-azsqltest02"},"action":"update"},"type":"planned_change"}
{"@level":"info","@message":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]: Plan to update","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.659154Z","change":{"resource":{"addr":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]","module":"","resource":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]","implied_provider":"azurerm","resource_type":"azurerm_virtual_machine","resource_name":"windows_vms","resource_key":"redactedazweb05"},"action":"update"},"type":"planned_change"}
{"@level":"info","@message":"local_file.ApplicationHost-MIMEandGzip: Creating...","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.977290Z","hook":{"resource":{"addr":"local_file.ApplicationHost-MIMEandGzip","module":"","resource":"local_file.ApplicationHost-MIMEandGzip","implied_provider":"local","resource_type":"local_file","resource_name":"ApplicationHost-MIMEandGzip","resource_key":null},"action":"create"},"type":"apply_start"}
{"@level":"info","@message":"local_file.ApplicationHost-MIME: Creating...","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.977574Z","hook":{"resource":{"addr":"local_file.ApplicationHost-MIME","module":"","resource":"local_file.ApplicationHost-MIME","implied_provider":"local","resource_type":"local_file","resource_name":"ApplicationHost-MIME","resource_key":null},"action":"create"},"type":"apply_start"}
{"@level":"info","@message":"local_file.ApplicationHost-MIME: Creation complete after 0s [id=e675b7463fd3e5c884b454d79c5b6311356c7863]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.982088Z","hook":{"resource":{"addr":"local_file.ApplicationHost-MIME","module":"","resource":"local_file.ApplicationHost-MIME","implied_provider":"local","resource_type":"local_file","resource_name":"ApplicationHost-MIME","resource_key":null},"action":"create","id_key":"id","id_value":"e675b7463fd3e5c884b454d79c5b6311356c7863","elapsed_seconds":0},"type":"apply_complete"}
{"@level":"info","@message":"local_file.ApplicationHost-MIMEandGzip: Creation complete after 0s [id=598a710216ff7f5ed68ac6e8b8e2f612ae12b01b]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:08.989494Z","hook":{"resource":{"addr":"local_file.ApplicationHost-MIMEandGzip","module":"","resource":"local_file.ApplicationHost-MIMEandGzip","implied_provider":"local","resource_type":"local_file","resource_name":"ApplicationHost-MIMEandGzip","resource_key":null},"action":"create","id_key":"id","id_value":"598a710216ff7f5ed68ac6e8b8e2f612ae12b01b","elapsed_seconds":0},"type":"apply_complete"}
{"@level":"info","@message":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]: Modifying... [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Compute/virtualMachines/redactedazweb05]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:11.498031Z","hook":{"resource":{"addr":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]","module":"","resource":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]","implied_provider":"azurerm","resource_type":"azurerm_virtual_machine","resource_name":"windows_vms","resource_key":"redactedazweb05"},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Compute/virtualMachines/redactedazweb05"},"type":"apply_start"}
{"@level":"info","@message":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]: Modifying... [id=/subscriptions/redacted/resourceGroups/cus_sqltest_rg/providers/Microsoft.Compute/virtualMachines/cus-azsqltest02]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:11.508086Z","hook":{"resource":{"addr":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]","module":"","resource":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]","implied_provider":"azurerm","resource_type":"azurerm_windows_virtual_machine","resource_name":"vms","resource_key":"cus-azsqltest02"},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/cus_sqltest_rg/providers/Microsoft.Compute/virtualMachines/cus-azsqltest02"},"type":"apply_start"}
{"@level":"info","@message":"azurerm_storage_account.redactedcloudwitness: Modifying... [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcloudwitness]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:11.554187Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedcloudwitness","module":"","resource":"azurerm_storage_account.redactedcloudwitness","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedcloudwitness","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcloudwitness"},"type":"apply_start"}
{"@level":"info","@message":"azurerm_storage_account.redactedazfs: Modifying... [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazfs]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:11.559046Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedazfs","module":"","resource":"azurerm_storage_account.redactedazfs","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedazfs","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazfs"},"type":"apply_start"}
{"@level":"info","@message":"azurerm_storage_account.redactedazofficebackup: Modifying... [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazofficebackup]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:11.570022Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedazofficebackup","module":"","resource":"azurerm_storage_account.redactedazofficebackup","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedazofficebackup","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazofficebackup"},"type":"apply_start"}
{"@level":"info","@message":"azurerm_storage_account.redactedcorpfs: Modifying... [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcorpfs]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:11.570673Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedcorpfs","module":"","resource":"azurerm_storage_account.redactedcorpfs","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedcorpfs","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcorpfs"},"type":"apply_start"}
{"@level":"info","@message":"azurerm_storage_account.redactedappservices: Modifying... [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedappservices]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:11.576574Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedappservices","module":"","resource":"azurerm_storage_account.redactedappservices","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedappservices","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedappservices"},"type":"apply_start"}
{"@level":"info","@message":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]: Modifications errored after 2s","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:12.509288Z","hook":{"resource":{"addr":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]","module":"","resource":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]","implied_provider":"azurerm","resource_type":"azurerm_virtual_machine","resource_name":"windows_vms","resource_key":"redactedazweb05"},"action":"update","elapsed_seconds":2},"type":"apply_errored"}
{"@level":"info","@message":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]: Modifications complete after 2s [id=/subscriptions/redacted/resourceGroups/cus_sqltest_rg/providers/Microsoft.Compute/virtualMachines/cus-azsqltest02]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:14.191417Z","hook":{"resource":{"addr":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]","module":"","resource":"azurerm_windows_virtual_machine.vms[\"cus-azsqltest02\"]","implied_provider":"azurerm","resource_type":"azurerm_windows_virtual_machine","resource_name":"vms","resource_key":"cus-azsqltest02"},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/cus_sqltest_rg/providers/Microsoft.Compute/virtualMachines/cus-azsqltest02","elapsed_seconds":2},"type":"apply_complete"}
{"@level":"info","@message":"azurerm_storage_account.redactedazofficebackup: Modifications complete after 5s [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazofficebackup]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:16.616398Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedazofficebackup","module":"","resource":"azurerm_storage_account.redactedazofficebackup","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedazofficebackup","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazofficebackup","elapsed_seconds":5},"type":"apply_complete"}
{"@level":"info","@message":"azurerm_storage_account.redactedcloudwitness: Modifications complete after 6s [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcloudwitness]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:17.961679Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedcloudwitness","module":"","resource":"azurerm_storage_account.redactedcloudwitness","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedcloudwitness","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcloudwitness","elapsed_seconds":6},"type":"apply_complete"}
{"@level":"info","@message":"azurerm_storage_account.redactedcorpfs: Modifications complete after 7s [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcorpfs]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:19.310755Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedcorpfs","module":"","resource":"azurerm_storage_account.redactedcorpfs","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedcorpfs","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedcorpfs","elapsed_seconds":7},"type":"apply_complete"}
{"@level":"info","@message":"azurerm_storage_account.redactedappservices: Modifications complete after 9s [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedappservices]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:21.283464Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedappservices","module":"","resource":"azurerm_storage_account.redactedappservices","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedappservices","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedappservices","elapsed_seconds":9},"type":"apply_complete"}
{"@level":"info","@message":"azurerm_storage_account.redactedazfs: Still modifying... [10s elapsed]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:21.560891Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedazfs","module":"","resource":"azurerm_storage_account.redactedazfs","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedazfs","resource_key":null},"action":"update","elapsed_seconds":10},"type":"apply_progress"}
{"@level":"info","@message":"azurerm_storage_account.redactedazfs: Modifications complete after 11s [id=/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazfs]","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:23.300256Z","hook":{"resource":{"addr":"azurerm_storage_account.redactedazfs","module":"","resource":"azurerm_storage_account.redactedazfs","implied_provider":"azurerm","resource_type":"azurerm_storage_account","resource_name":"redactedazfs","resource_key":null},"action":"update","id_key":"id","id_value":"/subscriptions/redacted/resourceGroups/rg_prod/providers/Microsoft.Storage/storageAccounts/redactedazfs","elapsed_seconds":11},"type":"apply_complete"}
{"@level":"error","@message":"Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=409 -- Original Error: Code=\"PropertyChangeNotAllowed\" Message=\"Changing property 'osProfile' is not allowed.\" Target=\"osProfile\"","@module":"terraform.ui","@timestamp":"2024-01-17T14:49:27.126131Z","diagnostic":{"severity":"error","summary":"compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=409 -- Original Error: Code=\"PropertyChangeNotAllowed\" Message=\"Changing property 'osProfile' is not allowed.\" Target=\"osProfile\"","detail":"","address":"azurerm_virtual_machine.windows_vms[\"redactedazweb05\"]","range":{"filename":"virtualmachines.tf","start":{"line":1728,"column":50,"byte":75976},"end":{"line":1728,"column":51,"byte":75977}},"snippet":{"context":"resource \"azurerm_virtual_machine\" \"windows_vms\"","code":"resource \"azurerm_virtual_machine\" \"windows_vms\" {","start_line":1728,"highlight_start_offset":49,"highlight_end_offset":50,"values":[]}},"type":"diagnostic"}
Operation failed: failed running terraform apply (exit 1)

Expected Behaviour

Actual Behaviour

This VM was created from a backup of an older VM, and as such I have to use the azurerm_virtual_machine resource instead of azurerm_windows_virtual_machine (due to https://github.com/hashicorp/terraform-provider-azurerm/issues/8794). This machine had already been in my Terraform state for awhile and had been operating fine with previous versions of the azurerm provider.

I upgraded the azurerm provider from 3.83 to 3.86 and that's when this problem started happening.

Whenever I run a plan after upgrading, terraform cloud shows that this VM will be modified, but expanding the plan shows no attributes actually changing. The os_profile section is of course redacted in the plan due to secrets, so I'm assuming that it is trying to change this in the plan but I just can't see it.

Then when I do apply, I get the error: Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=409 -- Original Error: Code="PropertyChangeNotAllowed" Message="Changing property 'osProfile' is not allowed." Target="osProfile"

Note that I haven't touched this VM. I would not expect it to show up in the plan at all. Removing the os_profile block doesn't fix - the same error appears. Looking at the json export of the VM from within the Azure portal does include an osProfile block, so there is values there (i.e. it's not null).

You can see that I've tried to ignore the os_profile block but that does nothing. Per another issue, it seems ignoring that block doesn't work in the azurerm_virtual_machine resource.

I tried downgrading back to 3.83 but unfortunately the problem persists. Also tried on terraform v1.5.7 and that was the same.

Steps to Reproduce

  1. terraform plan
  2. terraform apply

Important Factoids

No response

References

https://github.com/hashicorp/terraform-provider-azurerm/issues/8794 https://github.com/hashicorp/terraform-provider-azurerm/issues/3592

unkinected commented 5 months ago

FYI this one is still happening on azurerm v3.103.1

elesandroo commented 3 months ago

I would say this is an error happening in the API. I am using azapi_resource for the same scenario you have and I am getting the same error even though the plan shows no changes.