terraform apply error after changing user data in aws_launch_config

Created on 28 Oct 2015  ·  10Comments  ·  Source: hashicorp/terraform

Hi,

I changed the user_data template and try to do plan its perfect, but re apply causes error as its not destroying the current AWS Launch Config but trying to create new one. which is causing error.

root@centos-test:/root/terraform-templates/guacamole$terraform plan
Refreshing Terraform state prior to plan...

aws_elb.elb-guacamole-app: Refreshing state... (ID: elb-guacamole-app)
aws_elb.elb-guacamole-web: Refreshing state... (ID: elb-guacamole-web)
aws_lb_cookie_stickiness_policy.elb-stickness-guacamole-web: Refreshing state... (ID: elb-guacamole-web:80:elb-stickness-guaca
mole-web)
template_file.guacamole_sh: Refreshing state... (ID: 21b0928298fbf277f25761d2ca725136f503b5060e5421037b6e8ba8a322e081)
aws_lb_cookie_stickiness_policy.elb-stickness-guacamole-app: Refreshing state... (ID: elb-guacamole-app:80:elb-stickness-guaca
mole-app)
aws_launch_configuration.as-conf-guacamole-web: Refreshing state... (ID: as-conf-guacamole-web)
aws_autoscaling_group.asg-guacamole-web: Refreshing state... (ID: asg-guacamole-web)

The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

-/+ aws_launch_configuration.as-conf-guacamole-web
associate_public_ip_address: "false" => "0"
ebs_block_device.#: "0" => ""
ebs_optimized: "false" => ""
enable_monitoring: "true" => "1"
image_id: "ami-12243640" => "ami-12243640"
instance_type: "t2.small" => "t2.small"
key_name: "309342" => "309342"
name: "as-conf-guacamole-web" => "as-conf-guacamole-web"
root_block_device.#: "0" => ""
security_groups.#: "1" => "1"
security_groups.1383371234: "sg-940b45f1" => "sg-940b45f1"
user_data: "2b6d54050867f7c143292bb117b618503d5b9b7c" => "931c62185885552be65c0518050aa7e3b913ee47" (forces new resource)

  • template_file.guacamole_sh
    filename: "" => "guacamole.sh"
    rendered: "" => ""
    vars.#: "" => "1"
    vars.elb: "" => "internal-elb-guacamole-app-1079268464.ap-southeast-1.elb.amazonaws.com"

Plan: 2 to add, 0 to change, 1 to destroy.
root@centos-test:/root/terraform-templates/guacamole$terraform apply
aws_elb.elb-guacamole-app: Refreshing state... (ID: elb-guacamole-app)
aws_elb.elb-guacamole-web: Refreshing state... (ID: elb-guacamole-web)
aws_lb_cookie_stickiness_policy.elb-stickness-guacamole-web: Refreshing state... (ID: elb-guacamole-web:80:elb-stickness-guaca
mole-web)
aws_lb_cookie_stickiness_policy.elb-stickness-guacamole-app: Refreshing state... (ID: elb-guacamole-app:80:elb-stickness-guaca
mole-app)
aws_launch_configuration.as-conf-guacamole-web: Refreshing state... (ID: as-conf-guacamole-web)
aws_autoscaling_group.asg-guacamole-web: Refreshing state... (ID: asg-guacamole-web)
template_file.guacamole_sh: Creating...
filename: "" => "guacamole.sh"
rendered: "" => ""
vars.#: "" => "1"
vars.elb: "" => "internal-elb-guacamole-app-1079268464.ap-southeast-1.elb.amazonaws.com"
template_file.guacamole_sh: Creation complete
aws_launch_configuration.as-conf-guacamole-web: Creating...
associate_public_ip_address: "" => "0"
ebs_block_device.#: "" => ""
ebs_optimized: "" => ""
enable_monitoring: "" => "1"
image_id: "" => "ami-12243640"
instance_type: "" => "t2.small"
key_name: "" => "309342"
name: "" => "as-conf-guacamole-web"
root_block_device.#: "" => ""
security_groups.#: "" => "1"
security_groups.1383371234: "" => "sg-940b45f1"
user_data: "" => "86a5feff658162b807cb05f60ac8bfe90b4c850b"
Error applying plan:

1 error(s) occurred:

  • aws_launch_configuration.as-conf-guacamole-web: Error creating launch configuration: AlreadyExists: Launch Configuration by
    this name already exists - A launch configuration already exists with the name as-conf-guacamole-web
    status code: 400, request id: 86eaf96f-7d50-11e5-a3fc-1b68652df451

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

bug provideaws

Most helpful comment

thanks @kumarsarath588, then that is definitely the case. you cannot use a name in an aws_launch_configuration when you are trying to use an aws_autoscaling_group

This is in the documentation as a section called Using with AutoScaling Groups (https://terraform.io/docs/providers/aws/r/launch_configuration.html)

If you take the name out, then Terraform will autogenerate it and the world will be a better place :)

All 10 comments

@kumarsarath588 can you show me the declaration of your launch_config? From the error, I am assuming you are giving the Launch Config a name parameter rather than letting terraform autogenerate one - is this correct?

If so, then you cannot do that if you want to update it: https://terraform.io/docs/providers/aws/r/launch_configuration.html

resource "aws_launch_configuration" "as-conf-guacamole-app" {
    name = "as-conf-guacamole-app"
    image_id = "ami-eda7608e"
    instance_type = "t2.small"
    key_name = "309342"
    security_groups = [ "sg-8b85f5ee"]
    lifecycle {
      create_before_destroy = true
    }
}

/* This resource create a aws autoscaling group in TechM vpc private 
   subnets with min instances of 2 and max instaces of 4 */

resource "aws_autoscaling_group" "asg-guacamole-app" {
  name = "asg-guacamole-app"
  vpc_zone_identifier = ["subnet-fd57d78a", "subnet-d4da43b1"]
  max_size = 4
  min_size = 2
  health_check_grace_period = 300
  health_check_type = "ELB"
  desired_capacity = 2
  force_delete = true
  launch_configuration = "${aws_launch_configuration.as-conf-guacamole-app.name}"
  load_balancers = ["${aws_elb.elb-g-app.name}"]

  tag {
    key = "name"
    value = "guacmole-app"
    propagate_at_launch = true
  }
}

thanks @kumarsarath588, then that is definitely the case. you cannot use a name in an aws_launch_configuration when you are trying to use an aws_autoscaling_group

This is in the documentation as a section called Using with AutoScaling Groups (https://terraform.io/docs/providers/aws/r/launch_configuration.html)

If you take the name out, then Terraform will autogenerate it and the world will be a better place :)

Hey all – as @stack72 mentioned, it's _recommended_ you omit the name parameter in your Launch Configuration, and use create_before_destroy for best use here.
In the next release of Terraform, you'll be able to supply a name_prefix, to prefix the auto generated name, but for now simply omit the name and Terraform will generate a new one for you.

I'm going to close this for now, please let me know if you need anything else or have other questions.
Thanks!

@catsby @stack72 Thanks for name_prefix parameter. I am on v0.9.1 and able to update the launch configuration but at the same time, I also want by Autoscaling Group to trigger so that new instances could be added (using new launch configuration) old ones could be deleted like a blue-green deployment. Is this possible?

Hi @vikas027

Unfortunately this is not possible with Terraform as of the current release.

thanks

Paul

Hey @stack72 ,

I just found this and is working perfectly for me.

resource "aws_launch_configuration" "jenkins_slave" {
  name_prefix          = "${var.name}-"
...
...
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "jenkins_slave_cluster" {
  name                 = "${aws_launch_configuration.jenkins_slave.name}"
...
...
  lifecycle {
    create_before_destroy = true
  }
}

The key here was to use :

  • lifecycle rule
  • use name_prefix in launch configuration (so a random name could be generated)
  • use the name of launch configuration in ASG name (so that it triggers the ASG resource)

Are there any downsides using this approach?

Cheers,
Vikas

Hi @vikas027

This will not recreate the instances for you - it will only update the underlying launch_config / autoscaling group. The changes will only be ingested by the instances on their next creation

Paul

Hey @vikas027 – yes that's the recommended approach, using name_prefix and create_before_destroy on Launch Configurations, as well as tying them into the AutoScaling Group. Modifying the LC will then cause the ASG to get updated, but as @stack72 points out, doing so will not update or rotate your _existing_ instances.

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings