简体   繁体   中英

Terraform AWS : Couldn't reuse previously created root_block_device with AWS EC2 instance launched with aws_launch_configuration

I've deployed an ELK stack to AWS ECS with terraform. All was running nicely for a few weeks, but 2 days ago I had to restart the instance.

Sadly, the new instance did not rely on the existing volume to mount the root block device. So all my elasticsearch data are no longer available to my Kibana instance.

Datas are still here, on previous volume, currently not used.

So I tried many things to get this volume attached at "dev/xvda" but without for exemple:

  • Use ebs_block_device instead of root_block_device using
  • Swap "dev/xvda" when instance is already running

I am using an aws_autoscaling_group with an aws_launch_configuration.

resource "aws_launch_configuration" "XXX" {
  name = "XXX"
  image_id = data.aws_ami.latest_ecs.id
  instance_type = var.INSTANCE_TYPE
  security_groups = [var.SECURITY_GROUP_ID]
  associate_public_ip_address = true
  iam_instance_profile = "XXXXXX"

  spot_price = "0.04" 
  lifecycle {
    create_before_destroy = true

  }

  user_data = templatefile("${path.module}/ecs_agent_conf_options.tmpl",
    {
      cluster_name = aws_ecs_cluster.XXX.name
    }
  )

//The volume i want to reuse was created with this configuration. I though it would
//be enough to reuse the same volume. It doesn't.
  root_block_device {
    delete_on_termination = false
    volume_size = 50
    volume_type = "gp2"
  }
} 

resource "aws_autoscaling_group" "YYY" {
  name = "YYY"
  min_size = var.MIN_INSTANCES
  max_size = var.MAX_INSTANCES
  desired_capacity = var.DESIRED_CAPACITY
  health_check_type = "EC2"
  availability_zones = ["eu-west-3b"]
  launch_configuration = aws_launch_configuration.XXX.name

  vpc_zone_identifier = [
    var.SUBNET_1_ID,
    var.SUBNET_2_ID]

}

Do I miss something obvious about this?

Sadly, you cannot attach a volume as a root volume to an instance.

What you have to do is create a custom AMI based on your volume. This involves creating a snapshot of the volume followed by construction of the AMI:

In terraform, there is aws_ami specially for that purpose.

The following terraform script exemplifies the process in three steps :

  1. Creation of a snapshot of a given volume
  2. Creation of an AMI from the snapshot
  3. Creation of an instance from the AMI
provider "aws" {
   # your data
}


resource "aws_ebs_snapshot" "snapshot" {
  volume_id = "vol-0ff4363a40eb3357c" # <-- your EBS volume ID
}


resource "aws_ami" "my" {
  name                = "my-custom-ami"

  virtualization_type = "hvm"
  root_device_name    = "/dev/xvda"

  ebs_block_device {
    device_name = "/dev/xvda"
    snapshot_id = aws_ebs_snapshot.snapshot.id
    volume_type = "gp2"
  }
}

resource "aws_instance" "web" {

  ami           = aws_ami.my.id  

  instance_type = "t2.micro"
  # key_name = "<your-key-name>"

  tags = {
    Name = "InstanceFromCustomAMI"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM