When building a network load balancer with the cloud formation, I got the following error:
- you cannot specify multiple target groups in a single action with a load balancer of type 'network'
I am adding two target groups in a single default action for a network listener, the part of cloud formation is at the bottom of the page.
If I create a NLB with 2 AZs, how can I add two target groups for 2 AZs into one listener of the NLB? I thought this would make the NLB fault-tolerant, but elastic load balancing doesn't allow this.
I have to work around this by adding targets in 2 AZs into one target group associating with the listener, but this will make load balancing cross the AZs which might cause higher latency and data transfer fees for NLB.
NLB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Sub "${NetworkStackName}-nlb"
Scheme: "internet-facing"
SecurityGroups:
- Fn::ImportValue:
'Fn::Sub': '${NetworkStackName}-PubSecurityGroupID'
Subnets:
- Fn::ImportValue:
'Fn::Sub': '${NetworkStackName}-PublicSubnetID-1'
- Fn::ImportValue:
'Fn::Sub': '${NetworkStackName}-PublicSubnetID-2'
Tags:
- Key: Name
Value: !Sub "${NetworkStackName}-nlb"
Type: "network"
NLBListener1:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
ForwardConfig:
TargetGroups:
- TargetGroupArn:
Fn::ImportValue:
'Fn::Sub': '${NetworkStackName}-nlb-target-group-1'
- Type: forward
ForwardConfig:
TargetGroups:
- TargetGroupArn:
Fn::ImportValue:
'Fn::Sub': '${NetworkStackName}-nlb-target-group-2'
LoadBalancerArn: !Ref NLB
Port: 80
Protocol: TCP
I might misunderstand the Target Group definition. I thought it was AZ-related but not. It seems a Target Group is just a virtual container including the targets from different AZs. Regarding the NLB, cross-zone load balancing is off by default, so the traffic is always routed to the targets in the same AZ.
Is this a correct understanding?
You are correct with your understanding. A target group isn’t specific per AZ and multi AZ is disabled by default on a network load balancer.