How to affect destroy ordering for aws_instance and aws_volume_attachment


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:

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                 =
  availability_zone                    =
  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 =
  size              = var.datadisc_size
  tags = {
    Name = "Jenkins-Volume"

resource "aws_volume_attachment" "ebs_att" {
  device_name = var.datadisc_device
  volume_id   =
  instance_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

I use terraform 1.0.6