0
votes

I am still very much a beginner in Terraform. I have a scenario where suppose only a single AWS component need to be recreated but is dependent on other component which does not require any changes e.g. Changes to Launch configuration component is not possible if the AutoScaling group is present. Even if the ASG is marked as tainted still terraform throws an error ""

  • aws_launch_configuration.sample-launch-configuration: 1 error(s) occurred:

  • aws_launch_configuration.sample-launch-configuration: Error creating launch configuration: AlreadyExists: Launch Configuration by this name already exists - A launch configuration already exists with the name sample-lc status code: 400, request id: 3dc2da6d-96e4-11e8-9086-cb6ff2d21a1c

    What is the way to fix these kind of dependencies without destroying the whole cluster?

EDITED: Adding source code. (Partial code sample)

 resource "aws_autoscaling_group" "sample-autoscaling-group" {
  name                 = "sample-asg"
  max_size             = "${var.max_instance_size}"
  min_size             = "${var.min_instance_size}"
  desired_capacity     = "${var.desired_capacity}"
  vpc_zone_identifier  = ["${var.private-subnets}"]
  launch_configuration = "${aws_launch_configuration.sample-launch-configuration.name}"
  health_check_type    = "EC2"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_launch_configuration" "sample-launch-configuration" {
  name                 = "sample-lc"
  image_id             = "ami-706cca12"
  instance_type        = "t2.small"
  iam_instance_profile = "${aws_iam_instance_profile.ecs-ec2-service-profile.id}"

  lifecycle {
    create_before_destroy = true
  }

  security_groups             = ["${aws_security_group.test_public_sg.id}"]
  associate_public_ip_address = "true"
  key_name                    = "${var.ecs-key-pair-name}"

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

If I change say the user_data.sh file and try to execute this it fails.

2
Can you share your Terraform code as well? - ydaetskcoR
Hi @ydaetskcoR, added the sample code. Cannot share the actual one as it belongs to the client. - Sorabh Mendiratta

2 Answers

1
votes

Re-reading the post and the attached config, I think I missed the actual issue. I'm going to leave the below explanation, because it may be helpful for someone at some point. As for your real issue, you're specifying create_before_destroy on the launch config, but it has a static name. Launch configs can't be edited after creation and must be destroyed and recreated (https://www.terraform.io/docs/providers/aws/r/launch_configuration.html), so TF is trying to create the new one first, but can't because it is using the same name as the one that is already there.


Original response (where I totally missed the real issue):

It looks like you're trying to create both the launch config and the auto scaling group (because you define both as resources). If the ASG already exists and isn't managed by terraform, you likely instead want to reference the ASG with a data source (see docs here). If you want the ASG to be managed by that terraform config above but it currently isn't, you can look at importing it (see docs here towards the bottom). If the ASG is managed by different terraform, you'll want to look at sharing state between your configs (see docs here).

1
votes

Answer to above issue is to use "name_prefix" attribute as shown below. This has resolved the issue. Many thanks to @jstill for keep coming back with possible options.

    resource "aws_launch_configuration" "sample-launch-configuration" {
      name_prefix                 = "sample-lc"
      image_id                    = "ami-706cca12"

As per terraform documentation, below snippet is given

Using with AutoScaling Groups Launch Configurations cannot be updated after creation with the Amazon Web Service API. In order to update a Launch Configuration, Terraform will destroy the existing resource and create a replacement. In order to effectively use a Launch Configuration resource with an AutoScaling Group resource, it's recommended to specify create_before_destroy in a lifecycle block. Either omit the Launch Configuration name attribute, or specify a partial name with name_prefix.

Documentation can be seen at here