3

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"
      }
    }

2 Answers 2

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.

2
  • 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 Jan 3, 2019 at 12:45
  • Why because, In this line "delete_on_termination = "${var.delete_on_termincation" you are defining the value of EBS termination globally not specific.
    – Surya SG
    Jan 3, 2019 at 12:49
1

There is an bug reported on TF AWS provider that is certainly related (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'

6
  • 1
    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!!! Jan 4, 2019 at 5:24
  • 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.
    – jmlrt
    Jan 4, 2019 at 10:35
  • 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 Jan 4, 2019 at 10:37
  • What command did you use to delete the EBS volume? terraform destroy or terraform taint && terraform apply?
    – jmlrt
    Jan 6, 2019 at 17:57
  • I used destroy one with a target pointing to the proper index of the individual volume Jan 7, 2019 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.