1. Answers
  2. Hosting Multiple Domains under one Load Balancer

How do I host multiple domains under one load balancer?

In this example, we will configure an Application Load Balancer (ALB) on AWS to serve multiple domains. An ALB can route traffic based on domain names, which makes it ideal for hosting multiple domains. We will set up an ALB with URL-based routing rules and use multiple target groups for different applications.

Let’s proceed with the configuration:

  1. Create an ALB.
  2. Create a listener for the ALB.
  3. Define target groups for each domain.
  4. Add listener rules to route traffic to the appropriate targets based on the host header.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as std from "@pulumi/std";

const myVpc = new aws.ec2.Vpc("my_vpc", {cidrBlock: "10.0.0.0/16"});
const mySubnets: aws.ec2.Subnet[] = [];
for (const range = {value: 0}; range.value < 2; range.value++) {
    mySubnets.push(new aws.ec2.Subnet(`my_subnets-${range.value}`, {
        vpcId: myVpc.id,
        cidrBlock: std.cidrsubnetOutput({
            input: myVpc.cidrBlock,
            newbits: 8,
            netnum: range.value,
        }).apply(invoke => invoke.result),
    }));
}
const albSg = new aws.ec2.SecurityGroup("alb_sg", {
    vpcId: myVpc.id,
    ingress: [{
        fromPort: 80,
        toPort: 80,
        protocol: "tcp",
        cidrBlocks: ["0.0.0.0/0"],
    }],
    egress: [{
        fromPort: 0,
        toPort: 0,
        protocol: "-1",
        cidrBlocks: ["0.0.0.0/0"],
    }],
});
const myAlb = new aws.lb.LoadBalancer("my_alb", {
    name: "my-alb",
    internal: false,
    loadBalancerType: "application",
    securityGroups: [albSg.id],
    subnets: mySubnets.map(__item => __item.id),
});
const httpListener = new aws.lb.Listener("http_listener", {
    loadBalancerArn: myAlb.arn,
    port: 80,
    protocol: "HTTP",
    defaultActions: [{
        type: "fixed-response",
        fixedResponse: {
            contentType: "text/plain",
            messageBody: "Not Found",
            statusCode: "404",
        },
    }],
});
const app1Tg = new aws.lb.TargetGroup("app1_tg", {
    name: "app1-tg",
    port: 80,
    protocol: "HTTP",
    vpcId: myVpc.id,
});
const app2Tg = new aws.lb.TargetGroup("app2_tg", {
    name: "app2-tg",
    port: 80,
    protocol: "HTTP",
    vpcId: myVpc.id,
});
const app1Rule = new aws.lb.ListenerRule("app1_rule", {
    listenerArn: httpListener.arn,
    priority: 1,
    actions: [{
        type: "forward",
        targetGroupArn: app1Tg.arn,
    }],
    conditions: [{
        hostHeader: {
            values: ["app1.example.com"],
        },
    }],
});
const app2Rule = new aws.lb.ListenerRule("app2_rule", {
    listenerArn: httpListener.arn,
    priority: 2,
    actions: [{
        type: "forward",
        targetGroupArn: app2Tg.arn,
    }],
    conditions: [{
        hostHeader: {
            values: ["app2.example.com"],
        },
    }],
});
export const albDnsName = myAlb.dnsName;

In this example, we created an ALB and configured it to route traffic based on the domain name. We set up separate target groups for app1.example.com and app2.example.com and added listener rules to forward traffic to the appropriate target group based on the host header.

The aws_lb_listener resource defines the listener’s port and protocol. The aws_lb_listener_rule resources specify the conditions and actions to forward traffic to the target groups based on the requested domain. The output provides the DNS name of the load balancer.

By following these steps, you can host multiple domains under one load balancer efficiently.

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