1. Answers
  2. Using aws alb with s3-bucket

How do I set up an AWS Application Load Balancer (ALB) with an S3 bucket?

To set up an AWS Application Load Balancer (ALB) with an S3 bucket using Pulumi, we’ll follow these steps:

  1. Create an S3 Bucket: This bucket will be used to host static content.
  2. Create a Target Group: This will be used by the ALB to forward requests to the S3 bucket.
  3. Create an ALB: This load balancer will distribute incoming traffic to our target group.
  4. Create a Listener for the ALB: This listener will handle incoming traffic on a specific port and forward it to the target group.

Here’s how you can achieve this using Pulumi in TypeScript:

Step-by-Step Explanation

  1. Create an S3 Bucket:

    • We create an S3 bucket to store our static website content.
  2. Create a Target Group:

    • We define a target group that the ALB will use to route requests. Since S3 buckets can’t be directly targeted, we will use a dummy target (like a Lambda function or an EC2 instance) for demonstration purposes.
  3. Create an ALB:

    • We set up an ALB in a specific VPC and subnets.
  4. Create a Listener for the ALB:

    • We configure a listener on the ALB to forward HTTP requests to our target group.

Pulumi Program in TypeScript

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

// Create an S3 bucket to store static content
const s3Bucket = new aws.s3.Bucket("my-bucket", {
    website: {
        indexDocument: "index.html",
        errorDocument: "error.html",
    },
});

// Create a VPC to deploy the ALB
const vpc = new aws.ec2.Vpc("my-vpc", {
    cidrBlock: "10.0.0.0/16",
    enableDnsSupport: true,
    enableDnsHostnames: true,
});

// Create subnets for the ALB
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 security group for the ALB
const albSecurityGroup = new aws.ec2.SecurityGroup("alb-sg", {
    vpcId: vpc.id,
    description: "Allow HTTP traffic",
    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 ALB
const alb = new aws.lb.LoadBalancer("my-alb", {
    securityGroups: [albSecurityGroup.id],
    subnets: [subnet1.id, subnet2.id],
    loadBalancerType: "application",
});

// Create a target group (using a dummy target)
const targetGroup = new aws.lb.TargetGroup("my-target-group", {
    port: 80,
    protocol: "HTTP",
    vpcId: vpc.id,
    targetType: "instance", // Change this to "lambda" if using a Lambda function
});

// Create a listener for the ALB
const listener = new aws.lb.Listener("my-listener", {
    loadBalancerArn: alb.arn,
    port: 80,
    defaultActions: [{
        type: "forward",
        targetGroupArn: targetGroup.arn,
    }],
});

// Export the S3 bucket URL and ALB DNS name
export const bucketUrl = pulumi.interpolate`http://${s3Bucket.websiteEndpoint}`;
export const albDnsName = alb.dnsName;

Explanation

  • S3 Bucket: The bucket is configured to serve a static website with index.html and error.html.
  • VPC and Subnets: We create a VPC and two subnets to host the ALB.
  • Security Group: The security group allows HTTP traffic on port 80.
  • ALB: The ALB is created in the specified subnets and associated with the security group.
  • Target Group: The target group is set up to forward traffic to a dummy target (like an EC2 instance or Lambda function).
  • Listener: The listener forwards incoming HTTP requests to the target group.

This setup provides a basic infrastructure where an ALB distributes traffic to a target group, and the S3 bucket serves static content. Adjust the target group configuration as needed to match your actual use case.

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