How do I build an AWS VPC network with Pulumi?
In this guide, we will create an AWS VPC network using Pulumi in TypeScript. We will define a VPC, subnets, an internet gateway, and route tables to set up a basic network infrastructure.
Key Points
- Define a VPC with a specified CIDR block.
- Create public and private subnets within the VPC.
- Set up an Internet Gateway for public subnets.
- Configure route tables and route table associations.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create a VPC
const vpc = new aws.ec2.Vpc("my-vpc", {
cidrBlock: "10.0.0.0/16",
enableDnsSupport: true,
enableDnsHostnames: true,
tags: {
Name: "my-vpc",
},
});
// Create an Internet Gateway
const internetGateway = new aws.ec2.InternetGateway("internet-gateway", {
vpcId: vpc.id,
tags: {
Name: "internet-gateway",
},
});
// Create a public subnet
const publicSubnet = new aws.ec2.Subnet("public-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
mapPublicIpOnLaunch: true,
tags: {
Name: "public-subnet",
},
});
// Create a private subnet
const privateSubnet = new aws.ec2.Subnet("private-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.2.0/24",
availabilityZone: "us-west-2a",
tags: {
Name: "private-subnet",
},
});
// 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,
},
],
tags: {
Name: "public-route-table",
},
});
// Associate the public subnet with the public route table
const publicRouteTableAssociation = new aws.ec2.RouteTableAssociation("public-route-table-association", {
subnetId: publicSubnet.id,
routeTableId: publicRouteTable.id,
});
// Create a route table for the private subnet
const privateRouteTable = new aws.ec2.RouteTable("private-route-table", {
vpcId: vpc.id,
tags: {
Name: "private-route-table",
},
});
// Associate the private subnet with the private route table
const privateRouteTableAssociation = new aws.ec2.RouteTableAssociation("private-route-table-association", {
subnetId: privateSubnet.id,
routeTableId: privateRouteTable.id,
});
// Export the VPC ID
export const vpcId = vpc.id;
// Export the public and private subnet IDs
export const publicSubnetId = publicSubnet.id;
export const privateSubnetId = privateSubnet.id;
Summary
In this guide, we created an AWS VPC with public and private subnets using Pulumi in TypeScript. We set up an Internet Gateway for the public subnet and configured route tables for both subnets. The resulting infrastructure is a basic VPC network ready for further customization and use.
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.