fix(elbv2): prevent duplicate listener creation on same port/protocol #44814
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rollback Plan
If a change needs to be reverted, we will publish an updated version of the library.
Changes to Security Controls
No changes to security controls. This change adds defensive validation to prevent duplicate resource creation but does not modify access controls, encryption, or logging.
Description
Adds defensive validation to prevent duplicate listener creation on AWS load balancers when the AWS API fails to raise a
DuplicateListenererror.Problem: The AWS
CreateListenerAPI sometimes silently returns an existing listener instead of raising an error when attempting to create a listener with the same port and protocol combination. This causes Terraform/Pulumi to incorrectly treat the operation as successful, leading to state inconsistencies where multiple listener resources point to the same ARN. When one listener is subsequently deleted, all resources referencing that ARN are affected.Solution: Before attempting to create a listener, query existing listeners on the load balancer and explicitly check for port/protocol conflicts. If a duplicate is detected, return an error immediately. This follows the same defensive pattern already implemented in
load_balancer.goandtarget_group.go.Tradeoff: Adds one
DescribeListenersAPI call before each listener creation (~50-200ms additional latency), but prevents silent data corruption and state inconsistencies.Relations
Closes #35121
References
[Bug]: Implicit import of aws_lb_listener instead of error "A listener already exists on this port for this load balancer" #35121
internal/service/elbv2/load_balancer.go:385-395internal/service/elbv2/target_group.go:424-432Output from Acceptance Testing
Note: Acceptance tests require AWS IAM permissions not available in contributor's account. Tests will need to be run by maintainers with appropriate access.
Expected Test Results:
The following tests should pass when run with proper AWS credentials: