skip to Main Content

I’m new to Terraform and AWS auto scaling. I’m basically just trying to get a hang of things using terraform and AWS.

In my main.tf Terraform file, I’ve set up a launch template and an auto scaling group to manage new EC2 instances. However, I’m encountering an issue where new instances created by auto scaling are empty—they don’t have any of the files, software and/or folders that were installed and/or copied over during the setup of the original instance.

The user_data script which is referenced in the launch template as (main.sh) is supposed to start my application server from one of those files and folders I copied over when setting up the first and original instance.

I expect that the auto scaled instances should automatically have all the files, software and folders I provisioned into the very first instance, so they can execute the user_data script to start the app from one of the folders.

But to my greatest surprise, all the new instances are completely empty. Nothing is inside them. This is not what I want.

Here’s a snippet of my Terraform configuration (main.tf):

# Autoscaling Group for EC2 instances, using Launch Template
resource "aws_autoscaling_group" "sample_asg" {
  desired_capacity    = 1
  max_size            = 5
  min_size            = 1
  vpc_zone_identifier = aws_subnet.sample_main[*].id

  launch_template {
    id      = aws_launch_template.sample_template.id
    version = "$Latest"
  }
}

# Launch Template for configuring EC2 instances
resource "aws_launch_template" "sample_template" {
  # Configuration details omitted for brevity
  user_data  = filebase64("../scripts/main.sh") # This user_data script will only work from one of the files and folders that were copied over from the very first instance that used this launch template
}

# The very first EC2 Instance resource using the Launch Template
resource "aws_instance" "sample_gen_server" {
  launch_template {
    id      = aws_launch_template.sample_template.id
    version = "$Latest"
  }
}

# Null resource for setting up the first instance environment. I expect that auto scaled instances should have this folder I just created now
resource "null_resource" "setup_instance" {
  provisioner "remote-exec" {
    inline = [
      "sudo mkdir -p apps/sample-gen-server",
      "sudo chown -R ubuntu:ubuntu apps/sample-gen-server",
    ]
    connection {
      type        = "ssh"
      user        = "ubuntu"
      private_key = file("../sample-key.pem")
      host        = aws_instance.sample_gen_server.public_ip
    }
  }

  provisioner "local-exec" {
    command = local.folder_sync_command
  }

  provisioner "remote-exec" {
    inline = [
      "chmod +x apps/sample-gen-server/scripts/setup_instance.sh",
      "apps/sample-gen-server/scripts/setup_instance.sh",
    ]
    connection {
      type        = "ssh"
      user        = "ubuntu"
      private_key = file("../sample-key.pem")
      host        = aws_instance.sample_gen_server.public_ip
    }
  }

  depends_on = [aws_instance.sample_gen_server]
}

# Null resource for installing dependencies for the first instance, then I expect that auto scaled instances should have all of the files, folders and software I am installing using the script in install_deps.sh
resource "null_resource" "install_dependencies" {
  provisioner "remote-exec" {
    inline = [
      "chmod +x apps/sample-gen-server/scripts/install_deps.sh",
      "apps/sample-gen-server/scripts/install_deps.sh",
    ]
    connection {
      type        = "ssh"
      user        = "ubuntu"
      private_key = file("../sample-key.pem")
      host        = aws_instance.sample_gen_server.public_ip
    }
  }
  triggers = {
    always_run = "${timestamp()}"
  }
  depends_on = [null_resource.sync_app_folder]
}

Thanks for any suggestions or solution to this.

2

Answers


  1. I do not have the reputation yet to comment. I recommend that you do the following:

    1. Check the AMI that is being used by the Launch Template. Is this AMI the base AMI or the AMI that you have taken from your initial setup.
    2. If it is indeed the AMI that you have taken from your initial setup, launch a new instance from that AMI manually and confirm that it has the expected files and folders.
    Login or Signup to reply.
  2. Terraform is not involved in the auto-scaling action performed by AWS. The Terraform null_resource ran once, after the resource "aws_instance" "sample_gen_server" was created. It installed things on that one server. That instance was not created by your auto-scaling group, but was a single, one-off instance that you created via Terraform, entirely separately from the auto-scaling group.

    The auto-scaling group will have an AMI ID associated with it. That AMI is the "snapshot" of an EC2 instance that the auto-scaling group will use to create new EC2 instances.

    In your Terraform code, you do not appear to be creating an AMI from the EC2 instance you created. It is unclear what AMI you are using to create either of the instances, but you have this comment in your code:

    # This user_data script will only work from one of the files and folders that were copied over from the very first instance that used this launch template
    

    That comment is completely wrong. Each time an instance is created from a launch template it is created from the AMI, not from "the very first instance that used this launch template". Each time an instance is created by the auto-scaling group, it is like it is creating that first "empty" instance all over again. Launch templates simply don’t work the way you have stated in your code comment. Amazon doesn’t track "this is the first time this person is using this launch template, so let’s somehow save all the changes they make and apply those changes to other instances of the launch template".

    You will need to create an instance with all the appropriate files once. Then create a new AMI from that instance. Then finally you will need to configure your launch template to use that new AMI.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search