How to Create a VPC With One Public and Two Private Subnets?
Introduction
In this solution, we will create a Virtual Private Cloud (VPC) with one public subnet and two private subnets using Pulumi in TypeScript. A VPC is a virtual network dedicated to your AWS account, allowing you to launch AWS resources in a logically isolated section of the AWS cloud. The key services involved in this solution are AWS VPC, Subnets, and Internet Gateway.
Step-by-Step Explanation
Step 1: Create a New Pulumi Project
First, create a new Pulumi project using the Pulumi CLI. This will set up the necessary files and directories for your Pulumi program.
Step 2: Install AWS Pulumi Package
Install the AWS Pulumi package to interact with AWS resources. You can do this by running npm install @pulumi/aws
.
Step 3: Import Required Modules
In your Pulumi program, import the required modules from the Pulumi and AWS Pulumi packages.
Step 4: Create a VPC
Create a new VPC resource with the desired CIDR block.
Step 5: Create Subnets
Create one public subnet and two private subnets within the VPC. Specify the CIDR blocks for each subnet.
Step 6: Create an Internet Gateway
Create an Internet Gateway and attach it to the VPC. This will allow the public subnet to communicate with the internet.
Step 7: Create Route Tables
Create a route table for the public subnet and associate it with the Internet Gateway. Also, create route tables for the private subnets.
Step 8: Associate Subnets with Route Tables
Associate the public subnet with the public route table and the private subnets with their respective route tables.
Key Points
- A VPC is a virtual network dedicated to your AWS account.
- Subnets are subdivisions of a VPC’s IP address range that allow you to group resources based on security and operational needs.
- An Internet Gateway allows communication between the VPC and the internet.
- Route tables control the routing of traffic within the VPC.
Conclusion
By following these steps, you can create a VPC with one public subnet and two private subnets using Pulumi in TypeScript. This setup provides a secure and scalable network infrastructure for your AWS resources.
Full Code Example
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 a public subnet
const publicSubnet = new aws.ec2.Subnet("public-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
mapPublicIpOnLaunch: true,
availabilityZone: "us-west-2a",
tags: { Name: "public-subnet" },
});
// Create two private subnets
const privateSubnet1 = new aws.ec2.Subnet("private-subnet-1", {
vpcId: vpc.id,
cidrBlock: "10.0.2.0/24",
availabilityZone: "us-west-2a",
tags: { Name: "private-subnet-1" },
});
const privateSubnet2 = new aws.ec2.Subnet("private-subnet-2", {
vpcId: vpc.id,
cidrBlock: "10.0.3.0/24",
availabilityZone: "us-west-2b",
tags: { Name: "private-subnet-2" },
});
// Create an Internet Gateway
const internetGateway = new aws.ec2.InternetGateway("internet-gateway", {
vpcId: vpc.id,
tags: { Name: "internet-gateway" },
});
// 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
new aws.ec2.RouteTableAssociation("public-route-table-association", {
subnetId: publicSubnet.id,
routeTableId: publicRouteTable.id,
});
// Create route tables for the private subnets
const privateRouteTable1 = new aws.ec2.RouteTable("private-route-table-1", {
vpcId: vpc.id,
tags: { Name: "private-route-table-1" },
});
const privateRouteTable2 = new aws.ec2.RouteTable("private-route-table-2", {
vpcId: vpc.id,
tags: { Name: "private-route-table-2" },
});
// Associate the private subnets with their respective route tables
new aws.ec2.RouteTableAssociation("private-route-table-association-1", {
subnetId: privateSubnet1.id,
routeTableId: privateRouteTable1.id,
});
new aws.ec2.RouteTableAssociation("private-route-table-association-2", {
subnetId: privateSubnet2.id,
routeTableId: privateRouteTable2.id,
});
// Export the IDs of the created resources
export const vpcId = vpc.id;
export const publicSubnetId = publicSubnet.id;
export const privateSubnet1Id = privateSubnet1.id;
export const privateSubnet2Id = privateSubnet2.id;
export const internetGatewayId = internetGateway.id;
export const publicRouteTableId = publicRouteTable.id;
export const privateRouteTable1Id = privateRouteTable1.id;
export const privateRouteTable2Id = privateRouteTable2.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.