How Do I Host Multiple Domains Under One Load Balancer?
Introduction
In modern web architecture, it is often necessary to host multiple domains under a single infrastructure setup. This can be efficiently achieved by using an Application Load Balancer (ALB) on AWS, which can route traffic based on domain names. This guide will walk you through the process of configuring an ALB to serve multiple domains using URL-based routing rules and target groups for different applications.
Step-by-Step Explanation
To set up an ALB for hosting multiple domains, follow these steps:
- Create an ALB: Start by setting up an Application Load Balancer in your AWS environment.
- Create a Listener for the ALB: Define a listener that will handle incoming traffic on specified ports and protocols.
- Define Target Groups for Each Domain: Set up target groups that will handle requests for each specific domain.
- Add Listener Rules: Configure listener rules to route traffic to the appropriate target group 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;
Key Points
- ALB Configuration: The ALB is set up to handle HTTP traffic and route it based on domain names.
- Target Groups: Separate target groups are defined for each domain, facilitating domain-specific routing.
- Listener Rules: These rules determine how incoming requests are distributed to target groups based on the request’s host header.
Conclusion
By following the steps outlined above, you can efficiently host multiple domains under a single AWS Application Load Balancer. This setup allows for flexible routing and management of traffic across different applications, enhancing both performance and scalability. The provided code example serves as a practical guide to implementing this configuration in your AWS environment.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.