What Is the Process of Setting Up Route Tables for AWS With Both Public and Private Subnets in TypeScript
Setting Up Route Tables for AWS with Public and Private Subnets in TypeScript
In this guide, we will set up route tables for AWS using Pulumi in TypeScript. We will create both public and private subnets, and associate them with the appropriate route tables. The key services involved are AWS VPC, Subnets, Internet Gateway, NAT Gateway, and Route Tables.
Step-by-Step Explanation
1. Create a VPC
First, we need to create a Virtual Private Cloud (VPC) to host our subnets.
2. Create Subnets
We will create two subnets: one public and one private.
3. Create an Internet Gateway
An Internet Gateway is required for the public subnet to have internet access.
4. Create a NAT Gateway
A NAT Gateway is required for the private subnet to access the internet.
5. Create Route Tables
We will create two route tables: one for the public subnet and one for the private subnet.
6. Associate Route Tables with Subnets
Finally, we will associate the route tables with the respective subnets.
Summary and Conclusion
In this guide, we set up route tables for AWS with both public and private subnets using Pulumi in TypeScript. We created a VPC, subnets, an Internet Gateway, a NAT Gateway, and route tables, and associated the route tables with the subnets. This setup ensures that the public subnet has internet access, while the private subnet can access the internet via the NAT Gateway.
Full Code Example
import * as aws from "@pulumi/aws";
// Create a VPC
const vpc = new aws.ec2.Vpc("my-vpc", {
cidrBlock: "10.0.0.0/16",
});
// Create a public subnet
const publicSubnet = new aws.ec2.Subnet("public-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
mapPublicIpOnLaunch: true,
});
// Create a private subnet
const privateSubnet = new aws.ec2.Subnet("private-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.2.0/24",
});
// Create an Internet Gateway
const internetGateway = new aws.ec2.InternetGateway("internet-gateway", {
vpcId: vpc.id,
});
// Create a NAT Gateway
const eip = new aws.ec2.Eip("nat-eip", {
vpc: true,
});
const natGateway = new aws.ec2.NatGateway("nat-gateway", {
subnetId: publicSubnet.id,
allocationId: eip.id,
});
// Create a route table for the public subnet
const publicRouteTable = new aws.ec2.RouteTable("public-route-table", {
vpcId: vpc.id,
routes: [
{
cidrBlock: "0.0.0.0/0",
gatewayId: internetGateway.id,
},
],
});
// Create a route table for the private subnet
const privateRouteTable = new aws.ec2.RouteTable("private-route-table", {
vpcId: vpc.id,
routes: [
{
cidrBlock: "0.0.0.0/0",
natGatewayId: natGateway.id,
},
],
});
// Associate the route tables with the subnets
new aws.ec2.RouteTableAssociation("public-route-table-association", {
subnetId: publicSubnet.id,
routeTableId: publicRouteTable.id,
});
new aws.ec2.RouteTableAssociation("private-route-table-association", {
subnetId: privateSubnet.id,
routeTableId: privateRouteTable.id,
});
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.