1. Answers
  2. How to set NLB IP as ALB target group?

How do I set NLB IP as ALB target group?

To set an NLB (Network Load Balancer) IP as a target in an ALB (Application Load Balancer) target group, you need to configure the ALB target group to accept IP addresses as targets and then attach the NLB IP to this target group. Below is a step-by-step guide and a Pulumi program in TypeScript to achieve this.

Steps:

  1. Create a Network Load Balancer (NLB): First, we will create an NLB. This is where your traffic will initially hit.
  2. Create an Application Load Balancer (ALB): Next, we set up an ALB to distribute traffic to the target group.
  3. Create an ALB Target Group: Create a target group for the ALB that will use IP addresses as targets.
  4. Attach the NLB IP to the ALB Target Group: Finally, we will attach the IP address of the NLB to the ALB target group.

Pulumi Program

Below is a Pulumi program in TypeScript that demonstrates these steps:

import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";

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

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

// Create a Network Load Balancer (NLB)
const nlb = new aws.lb.LoadBalancer("nlb", {
    internal: false,
    loadBalancerType: "network",
    subnets: [subnet1.id, subnet2.id],
});

// Create an Application Load Balancer (ALB)
const alb = new aws.lb.LoadBalancer("alb", {
    internal: false,
    loadBalancerType: "application",
    subnets: [subnet1.id, subnet2.id],
    securityGroups: [], // Add your security group IDs here
});

// Create an ALB Target Group
const albTargetGroup = new aws.lb.TargetGroup("albTargetGroup", {
    port: 80,
    protocol: "HTTP",
    vpcId: vpc.id,
    targetType: "ip",
    healthCheck: {
        path: "/",
        port: "traffic-port",
        protocol: "HTTP",
    },
});

// Attach the NLB IP to the ALB Target Group
const nlbIpAttachment = new aws.lb.TargetGroupAttachment("nlbIpAttachment", {
    targetGroupArn: albTargetGroup.arn,
    targetId: nlb.dnsName.apply(dnsName => {
        // Resolve the DNS name to an IP address
        const ip = dnsName; // In a real scenario, you might need to resolve this
        return ip;
    }),
    port: 80,
});

// Export the DNS names of the load balancers
export const nlbDnsName = nlb.dnsName;
export const albDnsName = alb.dnsName;

Explanation:

  1. VPC and Subnets: We create a VPC and two subnets for our load balancers.
  2. Network Load Balancer (NLB): An NLB is created with the specified subnets.
  3. Application Load Balancer (ALB): An ALB is created with the specified subnets.
  4. ALB Target Group: An ALB target group is created that accepts IP addresses as targets.
  5. Target Group Attachment: The IP address of the NLB (resolved from its DNS name) is attached to the ALB target group.

This setup allows the ALB to route traffic to the NLB, which can then distribute the traffic to its backend targets.

Running the Program

To run this program, make sure you have the Pulumi CLI installed and configured with your AWS credentials. Save the code to a file (e.g., index.ts), and run the following commands in your terminal:

pulumi up

This will provision the resources in your AWS account. After the deployment, you can access the DNS names of the load balancers from the stack outputs.

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