Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot rename ALB Target Group if Listener present #1315

Closed
shatil opened this issue Aug 2, 2017 · 8 comments
Closed

Cannot rename ALB Target Group if Listener present #1315

shatil opened this issue Aug 2, 2017 · 8 comments
Labels
bug Addresses a defect in current functionality.

Comments

@shatil
Copy link

shatil commented Aug 2, 2017

Terraform Version

Terraform v0.9.11

Affected Resource(s)

  • aws_alb_target_group
  • aws_alb_listener

Terraform Configuration Files

resource "aws_alb_target_group" "service" {
  name     = "tf-my-service"
  port     = 80
  protocol = "HTTP"
  vpc_id   = "vpc-c0ffeffe"
}

resource "aws_alb_listener" "service" {
  load_balancer_arn = "${aws_alb.service.arn}"
  port              = "80"

  default_action {
    target_group_arn = "${aws_alb_target_group.service.arn}"
    type             = "forward"
  }
}

Expected Behavior

Changes to aws_alb_target_group requiring its re-creation should cascade to dependents of aws_alb_listener(s)... or not otherwise trigger an API error.

Actual Behavior

Having changed the name of my Target Group, like this:

--- a/my-service/terraform.tf
+++ b/my-service/terraform.tf
@@ -13,7 +13,7 @@ resource "aws_alb" "service" {
 }

 resource "aws_alb_target_group" "service" {
-  name     = "tf-tg-my-service"
+  name     = "tf-my-service"
   port     = 80
   protocol = "HTTP"
   vpc_id   = "vpc-c0ffeffe"

on terraform apply, I received this error:

Error applying plan:

1 error(s) occurred:

* aws_alb_target_group.service (destroy): 1 error(s) occurred:

* aws_alb_target_group.service: Error deleting Target Group: ResourceInUse: Target group 'arn:aws:elasticloadbalancing:us-west-2:123456789101:targetgroup/tf-tg-my-service/7223db96a9656ab5' is currently in use by a listener or a rule
        status code: 400, request id: a89f3a9b-77db-11e7-a3c1-2f64ee02c6a2

Steps to Reproduce

  1. terraform apply
  2. Alter name field in aws_alb_target_group
  3. terraform apply
@shatil
Copy link
Author

shatil commented Aug 3, 2017

Using a lifecycle meta-parameter called create_before_destroy is a way around this issue:

--- a/my-service/terraform.tf
+++ b/my-service/terraform.tf
@@ -26,6 +26,10 @@ resource "aws_alb_target_group" "service" {
   port     = 80
   protocol = "HTTP"
   vpc_id   = "vpc-c0ffeffe"
+
+  lifecycle {
+    create_before_destroy = true
+  }
 }

 resource "aws_alb_listener" "service" {

@catsby
Copy link
Contributor

catsby commented Nov 16, 2017

Hey @shatil as you found, the create_before_destroy is the solution you need here.

Thanks!

@catsby catsby closed this as completed Nov 16, 2017
@jdblack
Copy link

jdblack commented Aug 14, 2018

These answers do not work with target groups that have defined names. He would have to switch to name_prefix

@julian-alarcon
Copy link

I have this issue, the problem is when you can't use name_prefix ( on my case is because prefix is larger than 6 characters), so I have to manually remove the Listener with the associated target group from the Load Balancer to be able to apply my TF templates.

@querry43
Copy link

I to have encountered this issue without a solution due to name collision.

kjgarza added a commit to datacite/mastino that referenced this issue Mar 15, 2019
@hdryx
Copy link

hdryx commented Jun 26, 2019

Based on the solutions above, i'm using this method :

# Generate a random string to add it to the name of the Target Group
resource "random_string" "alb_prefix" {
  length  = 4
  upper   = false
  special = false
}
resource "aws_alb_target_group" "alb_target_group" {
  name        = "target-group-${random_string.alb_prefix.result}"
  vpc_id      = "${var.vpc_id}"
  ...
}

@robertd
Copy link

robertd commented Jun 30, 2019

vpc_id   = "vpc-c0ffeffe"

I LOL'd 🤣

@ghost
Copy link

ghost commented Nov 3, 2019

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality.
Projects
None yet
Development

No branches or pull requests

8 participants