Skip to main content
clean up grammar
Source Link
Bruce Becker
  • 3.7k
  • 4
  • 19
  • 41

all All volumes are geting deleted when one EC2 intance is being deleted via terraform

iI am using thisa terraform template to create awsAWS resources. myMy count is 2, every thing is gettingeverything gets created as per plan, and, iI am attaching a 2nd ebsEBS volume of 1 gbGB to both machines, which is also happening well.

However, but the only issue is when iI try to delete one ec2EC2 instance byusing the command below cmd,, both of the 2nd ebs volumeEBS volumes of 1gb1 GB are getting destory,,idestroyed. I checked they are connected on seperateseparate instances.

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

all volumes are geting deleted when one EC2 is being deleted via terraform

i am using this template to create aws resources. my count is 2, every thing 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 2nd ebs volume of 1gb are getting destory,,i checked they are connected on seperate instances.

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

All volumes are deleted when EC2 intance is deleted via terraform

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.

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"
      }
    }
Post Migrated Here from unix.stackexchange.com (revisions)
Source Link

all volumes are geting deleted when one EC2 is being deleted via terraform

i am using this template to create aws resources. my count is 2, every thing 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 2nd ebs volume of 1gb are getting destory,,i checked they are connected on seperate 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"
  }
}