How do I build an AWS load balancer listenerrule with Pulumi?
In this guide, we will create an AWS load balancer listener rule using Pulumi. We will define a listener rule that forwards traffic to a target group based on specific conditions.
Below is the complete Pulumi program to create an AWS load balancer listener rule:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create a VPC
const vpc = new aws.ec2.Vpc("myVpc", {
cidrBlock: "10.0.0.0/16",
});
// Create a subnet
const subnet = new aws.ec2.Subnet("mySubnet", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
});
// Create a security group
const securityGroup = new aws.ec2.SecurityGroup("mySecurityGroup", {
vpcId: vpc.id,
ingress: [{
protocol: "tcp",
fromPort: 80,
toPort: 80,
cidrBlocks: ["0.0.0.0/0"],
}],
egress: [{
protocol: "-1",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
}],
});
// Create a target group
const targetGroup = new aws.lb.TargetGroup("myTargetGroup", {
port: 80,
protocol: "HTTP",
vpcId: vpc.id,
healthCheck: {
path: "/",
protocol: "HTTP",
},
});
// Create a load balancer
const loadBalancer = new aws.lb.LoadBalancer("myLoadBalancer", {
internal: false,
securityGroups: [securityGroup.id],
subnets: [subnet.id],
loadBalancerType: "application",
});
// Create a listener
const listener = new aws.lb.Listener("myListener", {
loadBalancerArn: loadBalancer.arn,
port: 80,
protocol: "HTTP",
defaultActions: [{
type: "fixed-response",
fixedResponse: {
contentType: "text/plain",
messageBody: "404 Not Found",
statusCode: "404",
},
}],
});
// Create a listener rule
const listenerRule = new aws.lb.ListenerRule("myListenerRule", {
listenerArn: listener.arn,
priority: 100,
actions: [{
type: "forward",
targetGroupArn: targetGroup.arn,
}],
conditions: [{
pathPattern: {
values: ["/api/*"],
},
}],
});
// Export the DNS name of the load balancer
export const loadBalancerDnsName = loadBalancer.dnsName;
Key Points
- VPC and Subnet: We create a VPC and a subnet to host our load balancer and target group.
- Security Group: A security group is created to allow HTTP traffic.
- Target Group: Defines where the traffic will be forwarded.
- Load Balancer: An Application Load Balancer is created.
- Listener: Listens for incoming HTTP traffic on port 80.
- Listener Rule: A rule is defined to forward traffic to the target group based on the path pattern condition.
Summary
In this guide, we created an AWS load balancer listener rule using Pulumi. We defined a VPC, subnet, security group, target group, load balancer, listener, and listener rule. The listener rule forwards traffic to the target group based on the specified path pattern condition.
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.