Problems in creating vm from shared image

Here is the code I am using for creating a Windows VM from Shared Image gallery:

resource "azurerm_virtual_machine" "vm" {
  name                  = "myvm" 
  location              = "East US"
  resource_group_name   = "MyResGroup"
  vm_size               = "Standard_D4as_v5"

  network_interface_ids =  [azurerm_network_interface.nic.id]

  storage_image_reference {
    id = "/subscriptions/4a4.....673/resourceGroups/rg-vm-images/providers/Microsoft.Compute/galleries/vmsgallary/images/testvmimage"
  }

  storage_os_disk {
    name               = "my_os_storage_disk"
    caching            = "ReadWrite"
    create_option      = "FromImage"
    managed_disk_type  = "Standard_LRS"
  }

  os_profile {
    computer_name  = "myvm"
    admin_username = "azureuser"
    admin_password = "MyComplexPassword"
  }

  os_profile_windows_config {
  }
}

It gives the following error (after 40/50 mins or so):

OS Provisioning for VM ‘xxx’ did not finish in the allotted time. However, the VM guest agent was detected running. This suggests the guest OS has not been properly prepared to be used as a VM image (with CreateOption=FromImage). To resolve this issue, either use the VHD as is with CreateOption=Attach or prepare it properly for use as an image:

Deleting and recreating the virtual machine may resolve the issue.

Here is the information of the shared image:

OS type: Windows
OS state: Generalized

Note that I have been successful in creating a Linux VM using the same code (except those that are only applicable to Linux).