How do I integrate AWS EC2 Transit Gateway and Application Load Balancer?
This guide demonstrates how to integrate an AWS EC2 Transit Gateway with an Application Load Balancer using Pulumi. We will create a VPC, an Application Load Balancer (ALB), and an EC2 Transit Gateway, and then associate the VPC with the Transit Gateway.
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",
tags: {
Name: "myVpc",
},
});
// Create subnets
const subnet1 = new aws.ec2.Subnet("mySubnet1", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
tags: {
Name: "mySubnet1",
},
});
const subnet2 = new aws.ec2.Subnet("mySubnet2", {
vpcId: vpc.id,
cidrBlock: "10.0.2.0/24",
availabilityZone: "us-west-2b",
tags: {
Name: "mySubnet2",
},
});
// Create an Internet Gateway
const igw = new aws.ec2.InternetGateway("myIgw", {
vpcId: vpc.id,
tags: {
Name: "myIgw",
},
});
// Create a route table
const routeTable = new aws.ec2.RouteTable("myRouteTable", {
vpcId: vpc.id,
routes: [
{
cidrBlock: "0.0.0.0/0",
gatewayId: igw.id,
},
],
tags: {
Name: "myRouteTable",
},
});
// Associate the route table with the subnets
new aws.ec2.RouteTableAssociation("myRouteTableAssoc1", {
subnetId: subnet1.id,
routeTableId: routeTable.id,
});
new aws.ec2.RouteTableAssociation("myRouteTableAssoc2", {
subnetId: subnet2.id,
routeTableId: routeTable.id,
});
// Create a Security Group
const securityGroup = new aws.ec2.SecurityGroup("mySecurityGroup", {
vpcId: vpc.id,
description: "Allow HTTP and HTTPS traffic",
ingress: [
{
protocol: "tcp",
fromPort: 80,
toPort: 80,
cidrBlocks: ["0.0.0.0/0"],
},
{
protocol: "tcp",
fromPort: 443,
toPort: 443,
cidrBlocks: ["0.0.0.0/0"],
},
],
egress: [
{
protocol: "tcp",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
},
],
tags: {
Name: "mySecurityGroup",
},
});
// Create an Application Load Balancer
const alb = new aws.lb.LoadBalancer("myAlb", {
internal: false,
securityGroups: [securityGroup.id],
subnets: [subnet1.id, subnet2.id],
tags: {
Name: "myAlb",
},
});
// Create a Target Group
const targetGroup = new aws.lb.TargetGroup("myTargetGroup", {
port: 80,
protocol: "HTTP",
vpcId: vpc.id,
targetType: "instance",
tags: {
Name: "myTargetGroup",
},
});
// Create a Listener
const listener = new aws.lb.Listener("myListener", {
loadBalancerArn: alb.arn,
port: 80,
defaultActions: [
{
type: "forward",
targetGroupArn: targetGroup.arn,
},
],
});
// Create an EC2 Transit Gateway
const transitGateway = new aws.ec2transitgateway.TransitGateway("myTransitGateway", {
description: "My EC2 Transit Gateway",
tags: {
Name: "myTransitGateway",
},
});
// Create a Transit Gateway Attachment
const tgwAttachment = new aws.ec2transitgateway.VpcAttachment("myTgwAttachment", {
subnetIds: [subnet1.id, subnet2.id],
transitGatewayId: transitGateway.id,
vpcId: vpc.id,
tags: {
Name: "myTgwAttachment",
},
});
// Export the necessary outputs
export const vpcId = vpc.id;
export const albDnsName = alb.dnsName;
export const transitGatewayId = transitGateway.id;
Key Points:
- Created a VPC with subnets and an Internet Gateway.
- Set up an Application Load Balancer (ALB) with a listener and a target group.
- Created an EC2 Transit Gateway and attached the VPC to it.
Summary:
In this guide, we integrated an AWS EC2 Transit Gateway with an Application Load Balancer using Pulumi. This setup allows for scalable and high-availability network architecture.
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.