Configure AWS Network Load Balancer

Create a Network Listener

  1. First, initialize a NetworkListener object:
   NetworkListener networkListener = productsServiceProps.networkLoadBalancer()
       .addListener("ProductsServiceNlbListener", BaseNetworkListenerProps.builder()
           .build());

Architect

  1. Now, configure the networkListener to listen on port 8081:
.port(8081)

Architect

  1. Specify the protocol that the listener uses to listen for connections. In this case, we use TCP.
 .protocol(software.amazon.awscdk.services.elasticloadbalancingv2.Protocol.TCP)

software.amazon.awscdk.services.elasticloadbalancingv2.Protocol.TCP: This is the way to specify the TCP protocol using CDK. Protocol.TCP is a constant in CDK representing the TCP protocol.

Architect

Configure NLB Target Group

  1. Use the addTargets method called on the networkListener object to add new targets to the listener previously configured on the Network Load Balancer (NLB).
   networkListener.addTargets("ProductsServiceNlbTarget",
       AddNetworkTargetsProps.builder()
           .build()
   );

Architect

  1. Next, specify that connections will be sent to port 8081 on the targets.
.port(8081)

Architect

  1. Connect to the targets using the TCP protocol.
.protocol(software.amazon.awscdk.services.elasticloadbalancingv2.Protocol.TCP)

Architect

  1. Next, specify the name of the target group to which the targets will be added as productsServiceNlb.
.targetGroupName("productsServiceNlb")

Architect

  1. Next, we will specify the list of specific targets that need to be added using the targets method.
.targets(Collections.singletonList(/* List of targets */))

Architect

  1. Tạo ra một target sử dụng cho dịch vụ Fargate. Một target Fargate sẽ được tạo để xử lý các kết nối gửi đến từ NLB.
.targets(Collections.singletonList(
    fargateService.loadBalancerTarget(LoadBalancerTargetOptions.builder()
        .build())
))

Architect

  1. Next, we will define the name of the container in the Fargate service to which connections will be sent as productsService.
   .targets(Collections.singletonList(
       fargateService.loadBalancerTarget(LoadBalancerTargetOptions.builder()
           .containerName("productsService")
           .build())
   ))

Architect

  1. Finally, configure port 8081 of the container in the Fargate service to which connections will be sent, using the TCP protocol.
   .targets(Collections.singletonList(
       fargateService.loadBalancerTarget(LoadBalancerTargetOptions.builder()
           .containerName("productsService")
           .containerPort(8081)
           .protocol(Protocol.TCP)
           .build())
   ))

Architect