1. Answers
  2. Setting up a public network load balancer on port 12345

How do I set up a public network load balancer on port 12345?

In this guide, we will set up a public network load balancer on AWS that listens on port 12345 using Pulumi. A network load balancer distributes incoming traffic across multiple targets, such as EC2 instances, in one or more Availability Zones. This setup helps improve the availability and scalability of your application.

The following Pulumi program creates:

  1. A VPC with public subnets.
  2. A security group to allow traffic on port 12345.
  3. A network load balancer that listens on port 12345.
  4. A target group and a listener for the load balancer.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create a new VPC
const vpc = new aws.ec2.Vpc("vpc", {
    cidrBlock: "10.0.0.0/16",
    enableDnsSupport: true,
    enableDnsHostnames: true,
});

// Create public subnets
const subnet1 = new aws.ec2.Subnet("subnet1", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: "us-west-2a",
    mapPublicIpOnLaunch: true,
});

const subnet2 = new aws.ec2.Subnet("subnet2", {
    vpcId: vpc.id,
    cidrBlock: "10.0.2.0/24",
    availabilityZone: "us-west-2b",
    mapPublicIpOnLaunch: true,
});

// Create a security group to allow traffic on port 12345
const securityGroup = new aws.ec2.SecurityGroup("securityGroup", {
    vpcId: vpc.id,
    ingress: [
        {
            protocol: "tcp",
            fromPort: 12345,
            toPort: 12345,
            cidrBlocks: ["0.0.0.0/0"],
        },
    ],
    egress: [
        {
            protocol: "-1",
            fromPort: 0,
            toPort: 0,
            cidrBlocks: ["0.0.0.0/0"],
        },
    ],
});

// Create a network load balancer
const nlb = new aws.lb.LoadBalancer("nlb", {
    internal: false,
    loadBalancerType: "network",
    subnets: [subnet1.id, subnet2.id],
    securityGroups: [securityGroup.id],
});

// Create a target group
const targetGroup = new aws.lb.TargetGroup("targetGroup", {
    port: 12345,
    protocol: "TCP",
    targetType: "instance",
    vpcId: vpc.id,
});

// Create a listener for the load balancer
const listener = new aws.lb.Listener("listener", {
    loadBalancerArn: nlb.arn,
    port: 12345,
    protocol: "TCP",
    defaultActions: [
        {
            type: "forward",
            targetGroupArn: targetGroup.arn,
        },
    ],
});

// Export the DNS name of the load balancer
export const loadBalancerDnsName = nlb.dnsName;

Key Points:

  • A VPC is created with two public subnets.
  • A security group is configured to allow traffic on port 12345.
  • A network load balancer is set up to listen on port 12345.
  • A target group and a listener are created to forward traffic to the target instances.
  • The DNS name of the load balancer is exported for easy access.

Summary:

This Pulumi program sets up a public network load balancer on AWS that listens on port 12345. It includes creating a VPC, configuring security groups, setting up the load balancer, and creating a target group and listener. This setup ensures that incoming traffic on port 12345 is distributed across multiple targets, enhancing the availability and scalability of your application.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up