Frederick Ollinger
Frederick Ollinger

Reputation: 777

azurerm_virtual_machine_extension Fails on Linux When Destroying

Given azurerm_linux_virtual_machine,

resource "azurerm_virtual_machine_extension" "postinstall" {
  name = azurerm_linux_virtual_machine.privateeye.computer_name
  virtual_machine_id = azurerm_linux_virtual_machine.privateeye.id
  publisher = "Microsoft.Azure.Extensions"
  type = "CustomScript"
  type_handler_version = "2.0"
  settings = << SETTINGS
  {
    "commandToExecute" : "/usr/sbin/postinstall.sh"
  }

When I destroy it, it gives an error:

azurerm_virtual_machine_extension.postinstall: Destroying: [id=/subscriptions/55bee-aaa-445b-vd-420608165/resourceGroups/rg_PrivateEye/providers/Microsoft.Compute/virtualMachines/ollinger-vm-gitlabtemplate/extensions/ollinger-vm-gitlabtemplate]

compute.VirtualMachineExtensionsClient#Delete: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="OperationNotAllowed" Message="Cannot modify extensions in the VM when the VM is not running."

Upvotes: 1

Views: 684

Answers (2)

Frederick Ollinger
Frederick Ollinger

Reputation: 777

The problem occurred because the VM was generalized which means that it could not be made into a specific instance anymore. Thus I could not restart it. One will generalize a VM in order to make a disk image which can be kept in an Azure Target disk gallery which allows deployment from custom images.

The only thing I could do was to delete the VM in the Azure console. Then did:

terraform destroy -auto-approve

This refreshed my local Terraform state to that of Azure.

Upvotes: 0

Ked Mardemootoo
Ked Mardemootoo

Reputation: 1595

This happens because Terraform is not able to access/view the extensions of that Linux VM when it is shut down.

The same behaviour is also seen from the Azure portal. If you shutdown your linux machine, you're not able to see the extensions, add new ones or uninstall existing ones.

To resolve this, start your VM and then run the destroy operation again.

Upvotes: 1

Related Questions