Create Network Load Balancer

Create Network Load Balancer stack

  1. In your CDK project, create a new stack named NlbStack.java and have the NlbStack class inherit from the Stack class from the amazon.awscdk library.

    Architecture

  2. To create the NLB stack, we need the VPC that we created earlier. Therefore, we will create a record class named NlbStackProps to pass the VPC into the NLB Stack. Add the following code outside the ClusterStack class:

   record NlbStackProps(
       Vpc vpc
   ){}

Architect

  1. Inside the NLB Stack, we will create Vpc Link, Network Load Balancer, and Application Load Balancer. Therefore, we need to define three attributes corresponding to them:
   private final VpcLink vpcLink;
   private final NetworkLoadBalancer networkLoadBalancer;
   private final ApplicationLoadBalancer applicationLoadBalancer;

Architect

  1. Next, let’s create getters for the three attributes:
   public VpcLink getVpcLink() {
       return vpcLink;
   }

   public NetworkLoadBalancer getNetworkLoadBalancer() {
       return networkLoadBalancer;
   }

   public ApplicationLoadBalancer getApplicationLoadBalancer() {
       return applicationLoadBalancer;
   }

Architect

  1. Create a constructor for the NlbStack class:
   public NlbStack(final Construct scope, final String id,
                   final StackProps props, NlbStackProps nlbStackProps) {
       super(scope, id, props);
   }

Architect

Create Network Load Balancer

  1. First, let’s create a Network Load Balancer (NLB). To do this, we need to initialize the networkLoadBalancer object within the constructor:
   this.networkLoadBalancer = new NetworkLoadBalancer(this, "Nlb",
       NetworkLoadBalancerProps.builder()
           .build());

Architect

  1. Next, set the name of the NLB to ECommerceNlb using:
   .loadBalancerName("ECommerceNlb")

Architect

  1. As shown in the design diagram, we will not expose the Network Load Balancer (NLB) and Application Load Balancer (ALB) to the internet outside of the VPC. Therefore, we will configure them as follows:
.internetFacing(false)
.vpc(nlbStackProps.vpc())

Architect