0
0
AWScloud~5 mins

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

Choose your learning style9 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.