2
votes

I have a terraform script where I launch a cluster of ec2 instances and combine them together (specifically for Influx Db). Here is the relevant part of the script:

resource "aws_instance" "influxdata" {
  count      = "${var.ec2-count-influx-data}"

  ami           = "${module.amis.rhel73_id}"
  instance_type = "${var.ec2-type-influx-data}"

  vpc_security_group_ids = ["${var.sg-ids}"]
  subnet_id              = "${element(module.infra.subnet,count.index)}"
  key_name               = "${var.KeyName}"

  tags {
    Name               = "influx-data-node"
    System                = "${module.infra.System}"
    Environment    = "${module.infra.Environment}"
    OwnerContact       = "${module.infra.OwnerContact}"
    Owner          = "${var.Owner}"
  }


  ebs_block_device {
    device_name = "/dev/sdg"
    volume_size = 750
    volume_type = "io1"
    iops = 3000
    encrypted = true
    delete_on_termination = false
  }

  user_data = "${file("terraform/attach_ebs.sh")}"

  connection {
    //private_key = "${file("/Users/key_CD.pem")}" #dev env
    //private_key = "${file("/Users/influx.pem")}" #qa env west
    private_key = "${file("/Users/influx_east.pem")}" #qa env east
    user        = "ec2-user"
  }

  provisioner "remote-exec" {
    inline = ["echo just checking for ssh. ttyl. bye."]
  }
}

What I'm now trying to do...is taint one instance and then have terraform rebuild it but...what I want it to do is to unmount ebs, detach ebs, rebuild instance, attach ebs, mount ebs.

When I do a terraform taint module=instance it does taint it and then when I go to apply the change, it creates a whole new instance and new ebs volume instead of reattaching the previous one back on the new instance.

I'm also ok with some data loss as this is part of a cluster so when the node gets rebuilt...it should just sync up with the other nodes....

How can one do this with Terraform?

2

2 Answers

0
votes

Create a snapshot of the instance you want to taint. Then change the ec2 resource's ebs volume to have the snapshot ID param of the previous snapshot. https://www.terraform.io/docs/providers/aws/r/instance.html#snapshot_id

0
votes

Since you are not concerned about data lose, I would want to think that having an ec2 instance that is in same state as before the rebuild but without the data is OK.

if this is the case, I will use user-data to automatically mount the newly attached volume after the rebuild. in that case after the rebuild, the ec2 instance will be in same state (with volume formatted and attached) and ready to sync with other nodes in the cluster for data without any additional effort. The script should look as below:

#!/bin/bash

DEVICE_FS=`blkid -o value -s TYPE /dev/xvdh`
if [ "`echo -n $DEVICE_FS`" == "" ] ; then
        mkfs.ext4 /dev/xvdh
fi
mkdir -p /data
echo '/dev/xvdh /data ext4 defaults 0 0' >> /etc/fstab
mount /data