1. Answers
  2. Building an AWS Load Balancer Listener

How do I build an AWS Load Balancer Listener?

To create an AWS Load Balancer Listener using Pulumi, you need to define the load balancer, target group, and listener resources. Below is a complete example in TypeScript that demonstrates how to set up these resources.

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: "tcp", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Create an Application Load Balancer
const alb = new aws.lb.LoadBalancer("myLoadBalancer", {
    internal: false,
    securityGroups: [securityGroup.id],
    subnets: [subnet.id],
    loadBalancerType: "application",
});

// Create a Target Group
const targetGroup = new aws.lb.TargetGroup("myTargetGroup", {
    port: 80,
    protocol: "HTTP",
    vpcId: vpc.id,
});

// Create a Listener
const listener = new aws.lb.Listener("myListener", {
    loadBalancerArn: alb.arn,
    port: 80,
    protocol: "HTTP",
    defaultActions: [{
        type: "forward",
        targetGroupArn: targetGroup.arn,
    }],
});

// Export the Load Balancer's DNS name
export const albDnsName = alb.dnsName;

In this example:

  1. We create a VPC and a subnet to host the load balancer.
  2. We define a security group to allow HTTP traffic.
  3. We create an Application Load Balancer (ALB) that uses the subnet and security group.
  4. We set up a Target Group that the ALB will forward requests to.
  5. Finally, we create a Listener on the ALB that listens on port 80 and forwards requests to the Target Group.

The concluding summary of what was done is:

  • Created a VPC, subnet, and security group.
  • Set up an Application Load Balancer.
  • Configured a Target Group for the load balancer.
  • Created a Listener for the load balancer to forward HTTP traffic to the Target Group.

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