Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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:

  • Creating a Linux AMI from a snapshot

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"
  }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!