# How to affect destroy ordering for aws\_instance and aws\_volume\_attachment

**URL:** <https://discuss.hashicorp.com/t/how-to-affect-destroy-ordering-for-aws-instance-and-aws-volume-attachment/29611>\
**Category:** Terraform\
**Created:** [September 17, 2021, 2:27am UTC](https://discuss.hashicorp.com/t/how-to-affect-destroy-ordering-for-aws-instance-and-aws-volume-attachment/29611 "2021-09-17T02:27:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan.steffen.de](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dan.steffen.de/32/16633_2.png) [@dan.steffen.de](https://discuss.hashicorp.com/u/dan.steffen.de)\
**Post date:** [September 17, 2021, 2:27am UTC](https://discuss.hashicorp.com/t/how-to-affect-destroy-ordering-for-aws-instance-and-aws-volume-attachment/29611/1 "2021-09-17T02:27:41Z")

</div>

Hello,

I am at the very beginning and so this could be a stupid questions. But I already try for the last two days around and didn’t found any solution why terraform try to detach an EBS-Volume before it try to destroy the instance.

At this moment my code looks like this:

```auto
resource "aws_instance" "ec2" {
  ami = var.image_id
  instance_type = var.type
  key_name = "sshkey"
  security_groups = [var.security_group_name]
  instance_initiated_shutdown_behavior = "terminate"
  iam_instance_profile = aws_iam_instance_profile.backup_profile.name
  availability_zone = var.az
  depends_on = [aws_security_group_rule.ingress_rules, aws_security_group_rule.egress_rules]

  root_block_device {
    delete_on_termination = true
  }

provisioner "remote-exec" {
    inline = ["sudo /usr/sbin/shutdown -h now"]
    when = destroy
    on_failure = continue
    connection {
      type = "ssh"
      host = self.public_ip
      user = "ec2-user"
      private_key = file("keys/sshkey_private.key")
      agent = false
    }
  }

  # Make sure instance has had some time to power down before attempting volume detachment
  provisioner "local-exec" {
    command = "sleep 120"
    when = destroy
  }
}

resource "aws_ebs_volume" "datavol" {
  availability_zone = var.az
  size = var.datadisc_size
  tags = {
    Name = "Jenkins-Volume"
  }
}

resource "aws_volume_attachment" "ebs_att" {
  device_name = var.datadisc_device
  volume_id = aws_ebs_volume.datavol.id
  instance_id = aws_instance.ec2.id
}

```

(without the security group defiitions)

and maybe I didn’t know something which I need to solve this riddle. I already try to definie aws\_volume\_attachment in the depends\_on clausel of the aws\_instance but then terraform gives me an circle error and I don’t get it why? Maybe because aws\_volume\_attachment is just a “meta” ressource?

I would very thanksful if someone here can give me an hint how I can reach the correct order to destroy all ressources.  
best regards  
Dan

Edit:  
I use terraform 1.0.6
