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

[Bug]: Importing aws_lb_listener and aws_lb_target_group incorrectly sets defaults for absent optional values #38861

Closed
flostadler opened this issue Aug 14, 2024 · 5 comments · Fixed by #39413
Assignees
Labels
bug Addresses a defect in current functionality. service/elbv2 Issues and PRs that pertain to the elbv2 service.
Milestone

Comments

@flostadler
Copy link
Contributor

flostadler commented Aug 14, 2024

Terraform Core Version

1.5.7

AWS Provider Version

5.62.0

Affected Resource(s)

aws_lb_listener, aws_alb_listener, aws_lb_target_group, aws_alb_target_group

Expected Behavior

When importing a Listener, optional attributes like default_action.forward.stickiness.duration are getting set to the types default value (e.g. 0 for number or "" for string).

Actual Behavior

Those attributes shouldn't be set at all when their corresponding field in the SDK response is not set because the default values are not valid for most of the attributes.
E.g.

This is happening because fields of the API response are casted to primitive types before first checking whether they're non-nil. E.g.

names.AttrEnabled: aws.ToBool(config.Enabled),
names.AttrDuration: aws.ToInt32(config.DurationSeconds),

A similar bug existed for create/update and was fixed here: https://github.com/hashicorp/terraform-provider-aws/pull/35671/files#diff-eeba380b8533b2e177f0bab8c04a32da3d34c417a7655baef7b4aabdeb244564R933-R938

Running terraform apply after the import corrects the attributes that were wrongly set to state.

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region = "eu-central-1"
}

variable "vpc_id" {
  description = "The VPC ID"
  type        = string
}

resource "aws_security_group" "allowTls" {
  description = "Allow TLS inbound traffic and all outbound traffic"
  vpc_id      = var.vpc_id

  tags = {
    Name = "allow_tls"
  }
}

data "aws_subnets" "public" {
  filter {
    name   = "vpc-id"
    values = [var.vpc_id]
  }

  tags = {
    SubnetType = "Public"
  }
}

resource "aws_lb" "my_lb" {
  name               = "my-lb"
  internal           = true
  load_balancer_type = "application"
  security_groups    = [aws_security_group.allowTls.id]
  subnets            = toset(data.aws_subnets.public.ids)
}

resource "aws_lb_target_group" "my_tg" {
  name        = "my-tg"
  port        = 80
  protocol    = "HTTP"
  vpc_id      = var.vpc_id
  target_type = "ip"
}

resource "aws_lb_listener" "my_listener" {
  load_balancer_arn = aws_lb.my_lb.arn
  port              = 80

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.my_tg.arn
  }
}

# resource "aws_lb_listener" "test_import" {
#     load_balancer_arn = aws_lb.my_lb.arn
#     port              = 80

#     default_action {
#         type             = "forward"
#         target_group_arn = aws_lb_target_group.my_tg.arn
#     }
# }

# import {
#     to = aws_lb_listener.test_import
#     id = aws_lb_listener.my_listener.arn
# }

Steps to Reproduce

Run terraform apply, then uncomment the resource at the bottom and run import with the output of the listener ARN.
E.g:

terraform import aws_lb_listener.test_import "arn:aws:elasticloadbalancing:eu-central-1:REDACTED:listener/app/my-lb/e672f33357b85da8/9c0e1f22b3e3c10e"

Now run terraform apply again and you'll see the that attributes like stickiness duration have been wrongly set in the state and show a diff:

  # aws_lb_listener.test_import will be updated in-place
  ~ resource "aws_lb_listener" "test_import" {
        id                = "arn:aws:elasticloadbalancing:eu-central-1:616138583583:listener/app/my-lb/e672f33357b85da8/9c0e1f22b3e3c10e"
        tags              = {}
        # (5 unchanged attributes hidden)

      ~ default_action {
          + target_group_arn = "arn:aws:elasticloadbalancing:eu-central-1:616138583583:targetgroup/my-tg/10da087e29abe9cd"
            # (2 unchanged attributes hidden)

          - forward {
              - stickiness {
                  - duration = 0 -> null
                  - enabled  = false -> null
                }
              - target_group {
                  - arn    = "arn:aws:elasticloadbalancing:eu-central-1:616138583583:targetgroup/my-tg/10da087e29abe9cd" -> null
                  - weight = 1 -> null
                }
            }
        }
    }

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

@flostadler flostadler added the bug Addresses a defect in current functionality. label Aug 14, 2024
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added service/elbv2 Issues and PRs that pertain to the elbv2 service. service/vpc Issues and PRs that pertain to the vpc service. labels Aug 14, 2024
@terraform-aws-provider terraform-aws-provider bot added the needs-triage Waiting for first response or review from a maintainer. label Aug 14, 2024
@flostadler flostadler changed the title [Bug]: Importing aws_lb_listener incorrectly sets defaults for absent optional values [Bug]: Importing aws_lb_listener and aws_lb_target_group incorrectly sets defaults for absent optional values Aug 14, 2024
@flostadler
Copy link
Contributor Author

flostadler commented Aug 14, 2024

A similar thing happens for aws_lb_target_group when the target_type is set to lambda. healthcheck.protocol and healthcheck.port get set to empty strings.

The wrong values aren't surfaced to users though because they only get inserted into the state. Those attributes are ignored when computing the diff and when applying it .

Here pointers are converted to their primitives, which yields "" for port and protocol:

names.AttrEnabled: aws.ToBool(apiObject.HealthCheckEnabled),
"healthy_threshold": aws.ToInt32(apiObject.HealthyThresholdCount),
names.AttrInterval: aws.ToInt32(apiObject.HealthCheckIntervalSeconds),
names.AttrPort: aws.ToString(apiObject.HealthCheckPort),
names.AttrProtocol: apiObject.HealthCheckProtocol,
names.AttrTimeout: aws.ToInt32(apiObject.HealthCheckTimeoutSeconds),
"unhealthy_threshold": aws.ToInt32(apiObject.UnhealthyThresholdCount),

Copy link

Warning

This issue has been closed, meaning that any additional comments are hard for our team to see. Please assume that the maintainers will not see them.

Ongoing conversations amongst community members are welcome, however, the issue will be locked after 30 days. Moving conversations to another venue, such as the AWS Provider forum, is recommended. If you have additional concerns, please open a new issue, referencing this one where needed.

@github-actions github-actions bot added this to the v5.69.0 milestone Sep 20, 2024
@github-actions github-actions bot removed the prioritized Part of the maintainer teams immediate focus. To be addressed within the current quarter. label Sep 26, 2024
Copy link

This functionality has been released in v5.69.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 28, 2024
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. service/elbv2 Issues and PRs that pertain to the elbv2 service.
Projects
None yet
3 participants