Bird
Raised Fist0
AWScloud~5 mins

Network Load Balancer (NLB) in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes your app needs to handle many users at once without slowing down. A Network Load Balancer helps by spreading user requests across multiple servers quickly and reliably. It works well for apps that need very fast and stable connections.
When your app must handle sudden spikes of many users without crashing.
When you want to balance traffic for apps that use TCP or UDP protocols.
When you need a load balancer that can handle millions of requests per second.
When you want to keep connections open for a long time without interruption.
When you want to route traffic based on IP addresses and ports.
Config File - nlb-setup.tf
nlb-setup.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_lb" "example_nlb" {
  name               = "example-nlb"
  internal           = false
  load_balancer_type = "network"
  subnets            = ["subnet-0bb1c79de3EXAMPLE", "subnet-064f5c6b3EXAMPLE"]
  enable_deletion_protection = false
}

resource "aws_lb_target_group" "example_tg" {
  name     = "example-tg"
  port     = 80
  protocol = "TCP"
  vpc_id   = "vpc-0bb1c79de3EXAMPLE"
}

resource "aws_lb_listener" "example_listener" {
  load_balancer_arn = aws_lb.example_nlb.arn
  port              = 80
  protocol          = "TCP"

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.example_tg.arn
  }
}

This Terraform file creates a Network Load Balancer named example-nlb in the us-east-1 region.

It uses two subnets to spread traffic across availability zones.

The target group example-tg listens on port 80 using TCP protocol and is linked to a VPC.

The listener listens on port 80 and forwards incoming traffic to the target group.

Commands
This command prepares Terraform to work with AWS by downloading necessary plugins and setting up the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command creates the Network Load Balancer and related resources in AWS as defined in the configuration file.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_lb.example_nlb: Creating... aws_lb_target_group.example_tg: Creating... aws_lb.example_nlb: Creation complete after 20s [id=arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/example-nlb/50dc6c495c0c9188] aws_lb_target_group.example_tg: Creation complete after 10s [id=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/example-tg/6d0ecf831eec9f09] aws_lb_listener.example_listener: Creating... aws_lb_listener.example_listener: Creation complete after 5s [id=arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/net/example-nlb/50dc6c495c0c9188/6d0ecf831eec9f09] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
→
-auto-approve - Automatically approves the plan without asking for confirmation
This command checks that the Network Load Balancer named example-nlb exists and shows its details.
Terminal
aws elbv2 describe-load-balancers --names example-nlb
Expected OutputExpected
{ "LoadBalancers": [ { "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/example-nlb/50dc6c495c0c9188", "DNSName": "example-nlb-1234567890.us-east-1.elb.amazonaws.com", "CanonicalHostedZoneId": "Z35SXDOTRQ7X7K", "CreatedTime": "2024-06-01T12:00:00.000Z", "LoadBalancerName": "example-nlb", "Scheme": "internet-facing", "VpcId": "vpc-0bb1c79de3EXAMPLE", "State": { "Code": "active" }, "Type": "network", "AvailabilityZones": [ { "ZoneName": "us-east-1a", "SubnetId": "subnet-0bb1c79de3EXAMPLE" }, { "ZoneName": "us-east-1b", "SubnetId": "subnet-064f5c6b3EXAMPLE" } ] } ] }
→
--names - Specifies the name of the load balancer to describe
Key Concept

If you remember nothing else from this pattern, remember: a Network Load Balancer quickly spreads network traffic across servers using IP and port, making your app fast and reliable.

Common Mistakes
Using an Application Load Balancer configuration instead of Network Load Balancer for TCP traffic.
Application Load Balancers work at a higher level and may not support the fast, low-level TCP traffic needed.
Use 'load_balancer_type = "network"' in your configuration to create a Network Load Balancer.
Not specifying the correct subnets in different availability zones.
This limits the load balancer's ability to distribute traffic and reduces fault tolerance.
Always specify subnets in at least two availability zones for high availability.
Forgetting to create a target group and listener for the NLB.
Without a target group and listener, the NLB has no place to send traffic.
Define a target group and listener that forwards traffic to your servers.
Summary
Initialize Terraform to prepare AWS provider plugins.
Apply the Terraform configuration to create the Network Load Balancer, target group, and listener.
Verify the Network Load Balancer exists and is active using AWS CLI.

Practice

(1/5)
1. What is the primary purpose of an AWS Network Load Balancer (NLB)?
easy
A. To efficiently distribute TCP/UDP traffic at the connection level
B. To manage HTTP requests with advanced routing rules
C. To store and retrieve data in a scalable way
D. To provide a user interface for managing AWS resources

Solution

  1. Step 1: Understand NLB's traffic handling

    NLB works at the connection level and handles TCP/UDP traffic efficiently.
  2. Step 2: Compare with other AWS services

    Unlike Application Load Balancer, NLB does not manage HTTP features but focuses on low latency and high throughput for TCP/UDP.
  3. Final Answer:

    To efficiently distribute TCP/UDP traffic at the connection level -> Option A
  4. Quick Check:

    NLB purpose = TCP/UDP load balancing [OK]
Hint: NLB handles TCP/UDP, not HTTP features [OK]
Common Mistakes:
  • Confusing NLB with Application Load Balancer
  • Thinking NLB manages HTTP routing
  • Assuming NLB stores data
2. Which of the following is a valid characteristic of an AWS Network Load Balancer (NLB)?
easy
A. Only supports HTTP and HTTPS protocols
B. Requires manual scaling for high traffic
C. Automatically modifies HTTP headers
D. Supports static IP addresses for the load balancer

Solution

  1. Step 1: Identify NLB features

    NLB supports static IP addresses, which is useful for fixed IP requirements.
  2. Step 2: Eliminate incorrect options

    NLB does not only support HTTP/HTTPS (it supports TCP/UDP), does not modify HTTP headers, and scales automatically.
  3. Final Answer:

    Supports static IP addresses for the load balancer -> Option D
  4. Quick Check:

    Static IP support = true for NLB [OK]
Hint: NLB supports static IPs, unlike ALB [OK]
Common Mistakes:
  • Thinking NLB only supports HTTP/HTTPS
  • Assuming NLB modifies HTTP headers
  • Believing NLB requires manual scaling
3. Given an NLB configured to balance TCP traffic across three targets, what happens if one target becomes unhealthy?
medium
A. NLB stops sending traffic to the unhealthy target and continues with healthy ones
B. NLB sends traffic evenly to all targets regardless of health
C. NLB shuts down until the unhealthy target recovers
D. NLB redirects traffic to a backup Application Load Balancer

Solution

  1. Step 1: Understand NLB health checks

    NLB performs health checks and routes traffic only to healthy targets.
  2. Step 2: Analyze behavior on unhealthy target

    If a target is unhealthy, NLB excludes it from routing until it recovers.
  3. Final Answer:

    NLB stops sending traffic to the unhealthy target and continues with healthy ones -> Option A
  4. Quick Check:

    Health checks exclude unhealthy targets [OK]
Hint: NLB routes only to healthy targets [OK]
Common Mistakes:
  • Assuming NLB sends traffic to unhealthy targets
  • Thinking NLB stops completely on failure
  • Believing NLB redirects to ALB automatically
4. You configured an NLB but notice it is not distributing UDP traffic as expected. What is the most likely cause?
medium
A. Health checks are disabled for the target group
B. The target group is configured for TCP instead of UDP
C. NLB does not support UDP traffic
D. NLB requires HTTP listeners for UDP traffic

Solution

  1. Step 1: Check protocol configuration

    NLB supports UDP, but the target group must be set to UDP to handle UDP traffic.
  2. Step 2: Identify misconfiguration

    If the target group is set to TCP, UDP traffic won't be routed correctly.
  3. Final Answer:

    The target group is configured for TCP instead of UDP -> Option B
  4. Quick Check:

    Target group protocol must match traffic [OK]
Hint: Match target group protocol to traffic type [OK]
Common Mistakes:
  • Thinking NLB does not support UDP
  • Ignoring target group protocol settings
  • Assuming health checks cause traffic issues
5. You need to design a highly scalable system that requires static IP addresses and low latency for millions of TCP connections. Which AWS load balancer should you choose and why?
hard
A. Gateway Load Balancer, because it manages HTTP and TCP traffic with static IPs
B. Application Load Balancer, because it supports HTTP routing and static IPs
C. Network Load Balancer, because it supports static IPs and handles millions of TCP connections with low latency
D. Classic Load Balancer, because it is the newest and fastest option

Solution

  1. Step 1: Identify requirements

    The system needs static IPs, low latency, and support for millions of TCP connections.
  2. Step 2: Match requirements to load balancer features

    NLB supports static IPs, is designed for extreme scalability, and handles TCP traffic efficiently with low latency.
  3. Step 3: Eliminate incorrect options

    ALB focuses on HTTP, Classic LB is older and less scalable, Gateway LB is for network appliances, not general TCP load balancing.
  4. Final Answer:

    Network Load Balancer, because it supports static IPs and handles millions of TCP connections with low latency -> Option C
  5. Quick Check:

    Static IP + TCP + scalability = NLB [OK]
Hint: For static IP and TCP scale, choose NLB [OK]
Common Mistakes:
  • Choosing ALB for TCP static IP needs
  • Confusing Classic LB as newest
  • Assuming Gateway LB handles general TCP load balancing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard