Answers: <p>The issue is with the <code>availability_zone</code> on the <code>aws_ebs_volume</code> resource. it depends on the <code>aws_instance</code>, so when Terraform destroys the EC2, it thinks that your EBS can't live without it. Even though it's an EC2 independent variable.</p>
  <p>For more info check a very detailed description:
  <a href="https://github.com/hashicorp/terraform/issues/30614#issuecomment-1058769588" rel="nofollow noreferrer">https://github.com/hashicorp/terraform/issues/30614#issuecomment-1058769588</a></p>
  <p>If you have a reference to your VPC, use that. For me it's <code>module.vpc-prod.azs[0]</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2022-06-25T22:24:29.343Z
Answers: 2022-06-26T04:36:47.023Z
Answers: 2022-06-26T04:36:47.023Z
Answers: marcellsimon
Body: <p>I am using this template to create AWS resources.</p>
  <p>My count is 2, and everything is getting created as per plan, I am attaching a 2nd EBS volume of 1 GB to both machines, which is also happening well, but the only issue is when I try to delete one EC2 instance by below cmd, both of the 2 EBS volume of 1GB are getting destroyed. I checked, and they are connected on separate instances.</p>
  <pre><code>$ terraform destroy -target=aws_instance.jumpserver[1]
  aws_vpc.main_vpc: Refreshing state... (ID: vpc-06b59734024ad6adc)
  aws_key_pair.ProdKeypair: Refreshing state... (ID: ProdKeypair)
  aws_security_group.sg_internet_facing: Refreshing state... (ID: sg-05a2739733f4f8a32)
  aws_subnet.public_subnet: Refreshing state... (ID: subnet-0a8c6ea2718a44224)
  aws_instance.jumpserver[1]: Refreshing state... (ID: i-05646d53baa34a988)
  
  An execution plan has been generated and is shown below.
  Resource actions are indicated with the following symbols:
    - destroy
  
  Terraform will perform the following actions:
  
    - aws_ebs_volume.vol_generic_data[0]
  
    - aws_ebs_volume.vol_generic_data[1]
  
    - aws_instance.jumpserver[1]
  
    - aws_volume_attachment.generic_data_vol_att[0]
  
    - aws_volume_attachment.generic_data_vol_att[1]
  </code></pre>
  <p>This is the main.tf</p>
  <pre><code># Define webserver inside the public subnets 
  resource &quot;aws_instance&quot; &quot;jumpserver&quot; {
    count                       = &quot;${var.num_of_instances}&quot;
    ami                         = &quot;${var.ami}&quot;
    instance_type               = &quot;t2.micro&quot;
    key_name                    = &quot;${aws_key_pair.ProdKeypair.id}&quot;
    subnet_id                   = &quot;${aws_subnet.public_subnet.id}&quot;
    vpc_security_group_ids      = [&quot;${aws_security_group.sg_internet_facing.id}&quot;]
    associate_public_ip_address = true
    source_dest_check           = false
    #   user_data = &quot;${file(&quot;install.sh&quot;)}&quot;
  
    root_block_device = {
      volume_type           = &quot;gp2&quot;
      volume_size           =  &quot;8&quot;  
      delete_on_termination = &quot;${var.delete_on_termincation}&quot;
    }
  
    tags {
      Name = &quot;${format(&quot;jump-%01d&quot;,count.index+1)}&quot;
    }
  
    provisioner &quot;remote-exec&quot; {
      inline = [&quot;sudo apt-get  -y install python&quot;]
  
      connection {
        type        = &quot;ssh&quot;
        user        = &quot;ubuntu&quot;
        private_key = &quot;${file(var.private_key_path)}&quot;
      }
    }
  }
  
  resource &quot;aws_ebs_volume&quot; &quot;vol_generic_data&quot; {
    size              = &quot;1&quot;
    count             = &quot;${var.num_of_instances}&quot;
    type              = &quot;gp2&quot;
    availability_zone = &quot;${element(aws_instance.jumpserver.*.availability_zone, count.index)}&quot;
  
   tags = {
      Name = &quot;${format(&quot;jump-%01d&quot;,count.index+1)}&quot;
    }
  }
  
  
  resource &quot;aws_volume_attachment&quot; &quot;generic_data_vol_att&quot; {
    device_name = &quot;/dev/xvdf&quot;
    volume_id   = &quot;${element(aws_ebs_volume.vol_generic_data.*.id, count.index)}&quot;
    instance_id = &quot;${element(aws_instance.jumpserver.*.id, count.index)}&quot;
    count       = &quot;${var.num_of_instances}&quot;
  }
  
  
  # Define webserver inside the private subnet
  resource &quot;aws_instance&quot; &quot;backendserver&quot; {
    ami                         = &quot;${var.ami}&quot;
    instance_type               = &quot;t2.micro&quot;
    key_name                    = &quot;${aws_key_pair.ProdKeypair.id}&quot;
    subnet_id                   = &quot;${aws_subnet.private_subnet.id}&quot;
    vpc_security_group_ids      = [&quot;${aws_security_group.sg_backend.id}&quot;]
    associate_public_ip_address = false
    source_dest_check           = false
    user_data                   = &quot;${file(&quot;install.sh&quot;)}&quot;
  
    tags {
      Name = &quot;backendserver&quot;
    }
  }
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2019-12-24T15:11:26.503Z
Comments: Could you add the log? Without it, it will be hard to answer the question. Please edit the Q&A and let us know when it can be reviewed. In the mean time the question will be closed.
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2022-06-24T15:16:38.943Z
Comments: The issue is with the `availability_zone` on the EBS resource. it depends on the aws_instance, so when terraform destroys the EC2, it thinks that your EBS can't live without it. Even though it's an EC2 independent variable.
  For more info check a very detailed description: https://github.com/hashicorp/terraform/issues/30614#issuecomment-1058769588
Comments: marcellsimon
Tags: <amazon-web-services><terraform>
Title: Why are all volumes deleted when 1 EC2 instance is deleted via Terraform?
UserDisplayName: Mohammed Ali
ThreadText: # Why are all volumes deleted when 1 EC2 instance is deleted via Terraform?
  
  (Asked by: username_0 on 2018-12-27)
  
  I am using this template to create AWS resources.
  
  My count is 2, and everything is getting created as per plan, I am attaching a 2nd EBS volume of 1 GB to both machines, which is also happening well, but the only issue is when I try to delete one EC2 instance by below cmd, both of the 2 EBS volume of 1GB are getting destroyed. I checked, and they are connected on separate instances.
  
  ```
  $ terraform destroy -target=aws_instance.jumpserver[1]
  aws_vpc.main_vpc: Refreshing state... (ID: vpc-06b59734024ad6adc)
  aws_key_pair.ProdKeypair: Refreshing state... (ID: ProdKeypair)
  aws_security_group.sg_internet_facing: Refreshing state... (ID: sg-05a2739733f4f8a32)
  aws_subnet.public_subnet: Refreshing state... (ID: subnet-0a8c6ea2718a44224)
  aws_instance.jumpserver[1]: Refreshing state... (ID: i-05646d53baa34a988)
  
  An execution plan has been generated and is shown below.
  Resource actions are indicated with the following symbols:
    - destroy
  
  Terraform will perform the following actions:
  
    - aws_ebs_volume.vol_generic_data[0]
  
    - aws_ebs_volume.vol_generic_data[1]
  
    - aws_instance.jumpserver[1]
  
    - aws_volume_attachment.generic_data_vol_att[0]
  
    - aws_volume_attachment.generic_data_vol_att[1]
  
  ```
  
  This is the main.tf
  
  ```
  # Define webserver inside the public subnets 
  resource "aws_instance" "jumpserver" {
    count                       = "${var.num_of_instances}"
    ami                         = "${var.ami}"
    instance_type               = "t2.micro"
    key_name                    = "${aws_key_pair.ProdKeypair.id}"
    subnet_id                   = "${aws_subnet.public_subnet.id}"
    vpc_security_group_ids      = ["${aws_security_group.sg_internet_facing.id}"]
    associate_public_ip_address = true
    source_dest_check           = false
    #   user_data = "${file("install.sh")}"
  
    root_block_device = {
      volume_type           = "gp2"
      volume_size           =  "8"  
      delete_on_termination = "${var.delete_on_termincation}"
    }
  
    tags {
      Name = "${format("jump-%01d",count.index+1)}"
    }
  
    provisioner "remote-exec" {
      inline = ["sudo apt-get  -y install python"]
  
      connection {
        type        = "ssh"
        user        = "ubuntu"
        private_key = "${file(var.private_key_path)}"
      }
    }
  }
  
  resource "aws_ebs_volume" "vol_generic_data" {
    size              = "1"
    count             = "${var.num_of_instances}"
    type              = "gp2"
    availability_zone = "${element(aws_instance.jumpserver.*.availability_zone, count.index)}"
  
   tags = {
      Name = "${format("jump-%01d",count.index+1)}"
    }
  }
  
  resource "aws_volume_attachment" "generic_data_vol_att" {
    device_name = "/dev/xvdf"
    volume_id   = "${element(aws_ebs_volume.vol_generic_data.*.id, count.index)}"
    instance_id = "${element(aws_instance.jumpserver.*.id, count.index)}"
    count       = "${var.num_of_instances}"
  }
  
  # Define webserver inside the private subnet
  resource "aws_instance" "backendserver" {
    ami                         = "${var.ami}"
    instance_type               = "t2.micro"
    key_name                    = "${aws_key_pair.ProdKeypair.id}"
    subnet_id                   = "${aws_subnet.private_subnet.id}"
    vpc_security_group_ids      = ["${aws_security_group.sg_backend.id}"]
    associate_public_ip_address = false
    source_dest_check           = false
    user_data                   = "${file("install.sh")}"
  
    tags {
      Name = "backendserver"
    }
  }
  
  ```
  
  **Tags:** amazon-web-services, terraform
  
  > **Comments:**
  > **username_1**: Could you add the log? Without it, it will be hard to answer the question. Please edit the Q&A and let us know when it can be reviewed. In the mean time the question will be closed.  
  > **username_2**: The issue is with the `availability_zone` on the EBS resource. it depends on the aws_instance, so when terraform destroys the EC2, it thinks that your EBS can't live without it. Even though it's an EC2 independent variable. For more info check a very detailed description: https://github.com/hashicorp/terraform/issues/30614#issuecomment-1058769588  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2022-06-25. Score: 2
  
  The issue is with the `availability_zone` on the `aws_ebs_volume` resource. it depends on the `aws_instance`, so when Terraform destroys the EC2, it thinks that your EBS can't live without it. Even though it's an EC2 independent variable.
  
  For more info check a very detailed description:
  <https://github.com/hashicorp/terraform/issues/30614#issuecomment-1058769588>
  
  If you have a reference to your VPC, use that. For me it's `module.vpc-prod.azs[0]`
  
  
  ---
  
  