Answers: <p>I could see some spelling mistakes in the below mentioned line, rest all are fine i think.</p>
  
  <pre><code>delete_on_termination = "${var.delete_on_termincation}"
  </code></pre>
  
  <p>Better you could avoid this line, This might be deleting the EBS when u terminating the ec2 instance. Skip this option and check.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-03T12:45:10.347Z
Answers: Yah nice catch there,,but my problem is i ma deleting 2nd node and its deleting that completely,, along with that deleting the volumes created for the 1node
Answers: Mohammed Ali
Answers: CC BY-SA 4.0
Answers: 2019-01-03T12:49:59.980Z
Answers: Why because, In this line "delete_on_termination = "${var.delete_on_termincation" you are defining the value of EBS termination globally not specific.
Answers: Surya SG
Answers: CC BY-SA 4.0
Answers: 2019-01-03T12:40:49.613Z
Answers: 2019-01-03T12:40:49.613Z
Answers: Surya SG
Answers: <p>There is an bug reported on TF AWS provider that is certainly related (<a href="https://github.com/terraform-providers/terraform-provider-aws/issues/83" rel="nofollow noreferrer">terraform-provider-aws/issues/83</a>). Unfortunately, there is no solution for it except waiting to see if the bug still exists with the config language improvements expected in TF 0.12.</p>
  
  <p>What is your use case to delete this instance and ebs with <code>terraform destroy -target=aws_instance.xxx[1]</code> command? Do you want to recreate this instance in a second step or stay with only one instance?</p>
  
  <p>What I would try as a workaround is tainting the instance and ebs volume you want to delete with <code>terraform taint aws_instance.xxx.1 &amp;&amp; terraform taint aws_ebs_volume.xxx.1</code>.
  Then if you want to recreate them, just run <code>terraform apply</code> or if you want to stay with one instance run <code>terraform apply -var 'count=1'</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-04T05:24:35.407Z
Answers: The use case would be to re create a instance in case if it mis behaves ,,,. This would be my kafka cluster node,, so i can delete without worrying about data loss,2nd would be to lower the capacity of cluster if not needed!!!
Answers: Mohammed Ali
Answers: CC BY-SA 4.0
Answers: 2019-01-04T10:35:36.507Z
Answers: OK so try just tainting the resources and `terraform apply`. If it's for Kafka cluster node I think you should keep the data on EBS and reattach it to the new instance. Instead of dropping it, so instead of tainting `aws_ebs_volume` maybe you can taint `aws_ebs_volume_attachement`.
Answers: jmlrt
Answers: CC BY-SA 4.0
Answers: 2019-01-04T10:37:44.040Z
Answers: Ok so i removed the delete on terminate and tried to delete one ebs volume,,it worked,, but it also tried to delete all attachments
Answers: Mohammed Ali
Answers: CC BY-SA 4.0
Answers: 2019-01-06T17:57:55.487Z
Answers: What command did you use to delete the EBS volume? `terraform destroy` or `terraform taint && terraform apply`?
Answers: jmlrt
Answers: CC BY-SA 4.0
Answers: 2019-01-07T03:44:06.547Z
Answers: I used destroy one with a target pointing to the proper index of the individual volume
Answers: Mohammed Ali
Answers: CC BY-SA 4.0
Answers: 2019-01-07T10:00:54.777Z
Answers: And did you try with `terraform taint aws_instance.xxx.1 && terraform taint aws_ebs_volume_attachement.xxx.1 && terraform apply` like I advised you?
Answers: jmlrt
Answers: CC BY-SA 4.0
Answers: 2019-01-04T05:11:41.510Z
Answers: 2019-01-04T05:11:41.510Z
Answers: jmlrt
Body: <p>I am using a terraform template to create AWS resources.
  My count is 2, everything gets created as per plan, and, I am attaching a 2nd EBS volume of 1 GB to both machines, which is also happening well.</p>
  <p>However, the only issue is when I try to delete one EC2 instance using the command below, both of the 2nd EBS volumes of 1 GB are getting destroyed. I checked 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>
  
Tags: <amazon-web-services><terraform>
Title: All volumes are deleted when EC2 intance is deleted via terraform
UserDisplayName: Mohammed Ali
ThreadText: # All volumes are deleted when EC2 intance is deleted via terraform
  
  (Asked by: username_0 on 2018-12-27)
  
  I am using a terraform template to create AWS resources.
  My count is 2, everything gets created as per plan, and, I am attaching a 2nd EBS volume of 1 GB to both machines, which is also happening well.
  
  However, the only issue is when I try to delete one EC2 instance using the command below, both of the 2nd EBS volumes of 1 GB are getting destroyed. I checked 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
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-01-03. Score: 3
  
  I could see some spelling mistakes in the below mentioned line, rest all are fine i think.
  
  ```
  delete_on_termination = "${var.delete_on_termincation}"
  
  ```
  
  Better you could avoid this line, This might be deleting the EBS when u terminating the ec2 instance. Skip this option and check.
  
  > **Comments:**
  > **username_0**: Yah nice catch there,,but my problem is i ma deleting 2nd node and its deleting that completely,, along with that deleting the volumes created for the 1node  
  > **username_1**: Why because, In this line "delete_on_termination = "${var.delete_on_termincation" you are defining the value of EBS termination globally not specific.  
  
  ---
  
  ### Answer by username_2 on 2019-01-04. Score: 1
  
  There is an bug reported on TF AWS provider that is certainly related ([terraform-provider-aws/issues/83](https://github.com/terraform-providers/terraform-provider-aws/issues/83)). Unfortunately, there is no solution for it except waiting to see if the bug still exists with the config language improvements expected in TF 0.12.
  
  What is your use case to delete this instance and ebs with `terraform destroy -target=aws_instance.xxx[1]` command? Do you want to recreate this instance in a second step or stay with only one instance?
  
  What I would try as a workaround is tainting the instance and ebs volume you want to delete with `terraform taint aws_instance.xxx.1 && terraform taint aws_ebs_volume.xxx.1`.
  Then if you want to recreate them, just run `terraform apply` or if you want to stay with one instance run `terraform apply -var 'count=1'`
  
  > **Comments:**
  > **username_0**: The use case would be to re create a instance in case if it mis behaves ,,,. This would be my kafka cluster node,, so i can delete without worrying about data loss,2nd would be to lower the capacity of cluster if not needed!!!  
  > **username_2**: OK so try just tainting the resources and `terraform apply`. If it's for Kafka cluster node I think you should keep the data on EBS and reattach it to the new instance. Instead of dropping it, so instead of tainting `aws_ebs_volume` maybe you can taint `aws_ebs_volume_attachement`.  
  > **username_0**: Ok so i removed the delete on terminate and tried to delete one ebs volume,,it worked,, but it also tried to delete all attachments  
  > **username_2**: What command did you use to delete the EBS volume? `terraform destroy` or `terraform taint && terraform apply`?  
  > **username_0**: I used destroy one with a target pointing to the proper index of the individual volume  
  > **username_2**: And did you try with `terraform taint aws_instance.xxx.1 && terraform taint aws_ebs_volume_attachement.xxx.1 && terraform apply` like I advised you?  
  
  ---
  
  