Terraform-provider-aws: Cannot rename ALB Target Group if Listener present

Created on 3 Aug 2017  ·  8Comments  ·  Source: hashicorp/terraform-provider-aws

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
bug

Most helpful comment

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" {

All 8 comments

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" {

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

Thanks!

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

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.

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

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}"
  ...
}
vpc_id   = "vpc-c0ffeffe"

I LOL'd 🤣

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!

Was this page helpful?
0 / 5 - 0 ratings