Fix NLB listener -> target group association for TF & CF

The old code made the incorrect assumption that the NLB's list of TargetGroup tasks is in the same order as the NLB's list of listeners for their associations.
Because the model adds them in opposite orders this resulted in the TLS listener being forwarded to the TCP TG and vice versa.

This updates the terraform and cloudformation generation code to search the NLB's list of target groups by name for the target group that should be associated with the listener.
This matches the logic used in the "direct" target.
This commit is contained in:
Peter Rifel 2021-01-12 23:21:55 -06:00
parent 7df2521a09
commit 580d73bdc7
No known key found for this signature in database
GPG Key ID: BC6469E5B16DB2B6
3 changed files with 28 additions and 8 deletions

View File

@ -1206,7 +1206,7 @@
{
"Type": "forward",
"TargetGroupArn": {
"Ref": "AWSElasticLoadBalancingV2TargetGrouptcpcomplexexamplecomvpjolq"
"Ref": "AWSElasticLoadBalancingV2TargetGrouptlscomplexexamplecom5nursn"
}
}
],
@ -1225,7 +1225,7 @@
{
"Type": "forward",
"TargetGroupArn": {
"Ref": "AWSElasticLoadBalancingV2TargetGrouptlscomplexexamplecom5nursn"
"Ref": "AWSElasticLoadBalancingV2TargetGrouptcpcomplexexamplecomvpjolq"
}
}
],

View File

@ -460,7 +460,7 @@ resource "aws_launch_template" "nodes-complex-example-com" {
resource "aws_lb_listener" "api-complex-example-com-443" {
certificate_arn = "arn:aws:acm:us-test-1:000000000000:certificate/123456789012-1234-1234-1234-12345678"
default_action {
target_group_arn = aws_lb_target_group.tcp-complex-example-com-vpjolq.id
target_group_arn = aws_lb_target_group.tls-complex-example-com-5nursn.id
type = "forward"
}
load_balancer_arn = aws_lb.api-complex-example-com.id
@ -471,7 +471,7 @@ resource "aws_lb_listener" "api-complex-example-com-443" {
resource "aws_lb_listener" "api-complex-example-com-8443" {
default_action {
target_group_arn = aws_lb_target_group.tls-complex-example-com-5nursn.id
target_group_arn = aws_lb_target_group.tcp-complex-example-com-vpjolq.id
type = "forward"
}
load_balancer_arn = aws_lb.api-complex-example-com.id

View File

@ -722,14 +722,24 @@ func (_ *NetworkLoadBalancer) RenderTerraform(t *terraform.TerraformTarget, a, e
return err
}
for i, listener := range e.Listeners {
for _, listener := range e.Listeners {
var listenerTG *TargetGroup
for _, tg := range e.TargetGroups {
if aws.StringValue(tg.Name) == listener.TargetGroupName {
listenerTG = tg
break
}
}
if listenerTG == nil {
return fmt.Errorf("target group not found for NLB listener %+v", e)
}
listenerTF := &terraformNetworkLoadBalancerListener{
LoadBalancer: e.TerraformLink(),
Port: int64(listener.Port),
DefaultAction: []terraformNetworkLoadBalancerListenerAction{
{
Type: elbv2.ActionTypeEnumForward,
TargetGroupARN: e.TargetGroups[i].TerraformLink(),
TargetGroupARN: listenerTG.TerraformLink(),
},
},
}
@ -805,14 +815,24 @@ func (_ *NetworkLoadBalancer) RenderCloudformation(t *cloudformation.Cloudformat
return err
}
for i, listener := range e.Listeners {
for _, listener := range e.Listeners {
var listenerTG *TargetGroup
for _, tg := range e.TargetGroups {
if aws.StringValue(tg.Name) == listener.TargetGroupName {
listenerTG = tg
break
}
}
if listenerTG == nil {
return fmt.Errorf("target group not found for NLB listener %+v", e)
}
listenerCF := &cloudformationNetworkLoadBalancerListener{
LoadBalancerARN: e.CloudformationLink(),
Port: int64(listener.Port),
DefaultActions: []cloudformationNetworkLoadBalancerListenerAction{
{
Type: elbv2.ActionTypeEnumForward,
TargetGroupARN: e.TargetGroups[i].CloudformationLink(),
TargetGroupARN: listenerTG.CloudformationLink(),
},
},
}