to detach from Instance (): unexpected state 'busy', wanted target 'detached'. last error: %!s(<nil>)

I get the above error when I try to run terraform destroy. I think the ebs volume must be detatched first. How can I do this?

I tried doing this:

# Used for deleting and unmounting the /perforce volume, we just shut down the instance so it can be terminated after creating a snapshot
# Also creates a snapshot on deletion
resource "null_resource" "perforce_ondestroy" {
  count = var.start_perforce_master ? 1 : 0
  depends_on = [ aws_volume_attachment.perforce_ebs_volume_attachment, aws_instance.perforce_master, aws_ebs_volume.perforce_master_volume1]

  triggers = {
    "p4_private_key_path" = "${var.perforce_master_private_key_file_path}"
    "p4_host" = "${aws_instance.perforce_master[count.index].public_ip}"
    "volume_id" = "${aws_ebs_volume.perforce_master_volume1[count.index].id}"

  provisioner "remote-exec" {
    when = destroy
    connection {
      host        = "${self.triggers.p4_host}"
      type        = "ssh"
      user        = "ubuntu"
      private_key = "${file(self.triggers.p4_private_key_path)}"
    inline = [
      "sudo aws ec2 create-snapshot --volume-id ${self.triggers.volume_id} --description \" Perforce backup \"",
      "sudo shutdown"

but it did not work. I want to create a snapshot before I destroy it as well.

  • I see that there are still ongoing issues with this Link. Do you have a peace of Terraform code that is used for attaching the volume? That would be useful to see. If you go through the official documentation there are options for destroying the volume Link. But I advise you to try doing detaching manually and after that destroying. Let me know if you get any progress on this. Oct 23, 2021 at 8:32


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.