/ terraform

Terraform Launch Configurations - don't name them!

I found that naming a launch configuration is a bad idea. Im still refamiliarising myself with Terraform but let me try to explain a little.

Imagine, if you will, you are deploying an ECS cluster with a load balancer (ALB) and some auto scaling (ASG). ALB's require a launch configuration. This can look a little like this in Terraform:

resource "aws_launch_configuration" "lc" {
  name                        = "${var.project-name}-api-${var.environment-name}"
  image_id                    = "${var.image-id}"
  instance_type               = "${var.instance-type}"
  iam_instance_profile        = "${data.aws_iam_instance_profile.app.name}"
  security_groups             = ["${data.aws_security_group.app.id}"]
  associate_public_ip_address = "true"
  key_name                    = "${local.ecs-key-pair-name}"
  user_data                   = "${template_file.user-data.rendered}"
}

resource "template_file" "user-data" {
  template = "${file("${path.module}/ec2-user-data.tpl")}"

  vars {
    ecs-cluster-name = "${data.aws_ecs_cluster.cluster.cluster_name}"
  }
}

This is all fine and dandy until you want to make a change to the ALB or ASG, at which point all hell breaks loose and you find that you can't delete the launch config as it's referred to by the ASG.

resource "aws_autoscaling_group" "asg" {
  name                = "${var.project-name}-api-${var.environment-name}"
  max_size            = "${var.max-instance-size}"
  min_size            = "${var.min-instance-size}"
  desired_capacity    = "${var.desired-capacity}"
  vpc_zone_identifier = ["${data.aws_subnet.public.id}"]

  launch_configuration = "${aws_launch_configuration.lc.name}"
  health_check_type    = "ELB"
}

The trick is not to give the launch config a name. Just let terraform create a random name for you. Then we it needs to replace/update the launch config, everything will be sweet.