I have a terraform file that creates an EC2 instance along with a couple of volumes:
resource "aws_instance" "generic" {
count = "${lookup(var.INSTANCE_COUNT, var.SERVICE)}"
ami = "${var.AMI}"
instance_type = "${lookup(var.INSTANCE_TYPE, var.BLD_ENV)}"
subnet_id = "${element(var.SUBNET,count.index)}"
vpc_security_group_ids = ["${var.SECURITY_GROUP}"]
key_name = "${var.AWS_KEY_NAME}"
availability_zone = "${element(var.AWS_AVAILABILITY_ZONE,count.index)}"
iam_instance_profile = "${var.IAM_ROLE}"
root_block_device {
volume_type = "gp2"
delete_on_termination = "${var.DELETE_ROOT_ON_TERMINATION}"
}
ebs_block_device {
device_name = "${lookup(var.DEVICE_NAME,"datalake")}"
volume_type = "${lookup(var.DATALAKE_VOLUME_TYPE, var.SERVICE)}"
volume_size = "${var.NONDATADIR_VOLUME_SIZE}"
delete_on_termination = "${var.DELETE_ROOT_ON_TERMINATION}"
encrypted = true
}
ebs_block_device {
device_name = "${lookup(var.DEVICE_NAME,"datalake_logdir")}"
delete_on_termination = "${var.DELETE_ROOT_ON_TERMINATION}"
volume_type = "${lookup(var.LOGDIR_VOLUME_TYPE, var.SERVICE)}"
volume_size = "${var.NONDATADIR_VOLUME_SIZE}"
encrypted = true
}
volume_tags {
Name = "${lookup(var.TAGS, "Name")}-${count.index}"
}
}
If the ec2 instance terminates how can I attach the existing volumes to the new ec2 instance created when I rerun terraform? I was hoping that terraform could somehow tell from the state file the the instance is gone but the volumes aren't and therefore they should be attached to the newly created EC2.
Thanks in advance!