How To Create AWS VPC Using Terraform ?
Last Updated :
23 Jul, 2025
Terraform is an IAAC tool used to automate programmatic infrastructure provisioning. Here in this guide, I will first discuss what is AWS VPC. Then I will discuss terraform. After this, I will walk you through the different steps to write a code using Terraform to create a custom AWS VPC using subnet, internet gateway, and routing tables.
What is AWS VPC?
AWS VPC is a service that helps users create a virtual network on the AWS cloud platform. In the VPC, users can create their own public or private subnets, routing tables, internet gateways, and NAT gateways. Users can create a security group associated with a VPC for better security, here users define inbound and outbound rules. NACLs are used at the subnet level to allow or deny particular IPs when trying to access the subnet. AWS VPC gives users complete control over the virtual network on the AWS cloud platform. Overall we can say that this level of control enables users to create a custom virtual network to build a secure and scalable architecture for applications.
Terraform is an Infrastructure as Code(IAAC) tool that is used to define and provision infrastructure using a declarative configurational language called HashiCorp Configuration language(HCL). It has a simple syntax that helps to provision infrastructure in multiple cloud platforms. Using terraform increases the speed and reliability. It helps organizations to automate programmatically their infrastructure provisioning. Terraform's version control feature enables teams in an organization to manage infrastructure configurations as code, facilitating collaboration and also ensuring the tracing of changes over time. Its simplicity, cross-platform compatibility, and automation capabilities make it an essential tool for an organization to maintain control, reliability, and scalability.
The following are the steps that guides you on how to create AWS VPC using Terraform:
Step 1: First mention the provider and region in which you want to create VPC.
provider.tf
provider "aws" {
region = "us-east-1"
}

Step 2 : Create a VPC . Here mention the CIDR block and give a name to the VPC .
create_vpc.tf
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "vpc"
}
}

Step 3 : Then create a subnet inside the VPC with the following subnet.tf file:
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch=true
tags = {
Name = "Public-Subnet"
}
}

Step 4 : But the subnet is isolated now . If you create an EC2 instance inside this subnet then you can not connect the EC2 instance as it is present in an isolated environment . So you need an internet gateway .
internet_gateway.tf
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "IGW"
}
}

Step 5 : Create a route table and associate the route table with subnet . Here in the route all the traffic is passed through internet gateway .
route_table.tf
resource "aws_route_table" "rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "route_table"
}
}

route_subnet_association.tf
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.main.id
route_table_id = aws_route_table.rt.id
}

Step 6 : After this execute all these terraform files using the below commands one by one .
terraform init
terraform plan
terraform apply

Step 7: Check on your AWS console whether the VPC is created or not

- Now if you want to delete all the resources created through terraform , then write this command .
terraform destroy
The following are the best practices of using Terraform:
- Modularize Your Code: It helps in breaking your Terraform configuration into reusable modules for improving readability, manageability, and reusability of your infrastructure code.
- Use Version Control: To keep your Terraform code in a version control system like Git for tracking changes and collaborations with others, and rollback if necessary.
- State Management: It helps in using the remote state backends, such as S3 with DynamoDB for state locking, to manage and secure your Terraform state files effectively.
- Environment Segregation: It helps in creating a separate workspaces or use different state files for different environments (development, staging, production) to avoid conflicts and manage infrastructure lifecycle better.
The following are the trouble shooting issues of Terraform:
- Syntax Errors: On using
terraform fmt
and terraform validate
we can check for the syntax errors and ensure your configuration is well-formed before applying changes. - Resource Conflicts: By Identifying and resolving the resource conflicts by carefully managing resource naming and using the
terraform plan
command we can review the changes before applying them. - State File Issues: For fixing the state file corruption or inconsistencies can be done by using
terraform state
commands to inspect, modify, or repair the state file as needed. - Dependency Management: On handling the dependencies correctly by using Terraform's built-in mechanisms like
depends_on
to explicitly define resource dependencies and ensure proper resource creation order.
Conclusion
Here first we learned basics about AWS VPC and terraform . Then followed the steps to create an AWS VPC . Here inside the VPC we have created a public subnet , an internet gateway which helps the traffic to go in and out of the subnet and finally created a route table and associated with the subnet.
Similar Reads
DevOps Tutorial DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle.The goals of DevOps are:Faster and continuous software releases.Reduces manual errors through a
7 min read
Introduction
What is DevOps ?DevOps is a modern way of working in software development in which the development team (who writes the code and builds the software) and the operations team (which sets up, runs, and manages the software) work together as a single team.Before DevOps, the development and operations teams worked sepa
10 min read
DevOps LifecycleThe DevOps lifecycle is a structured approach that integrates development (Dev) and operations (Ops) teams to streamline software delivery. It focuses on collaboration, automation, and continuous feedback across key phases planning, coding, building, testing, releasing, deploying, operating, and mon
10 min read
The Evolution of DevOps - 3 Major Trends for FutureDevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Version Control
Continuous Integration (CI) & Continuous Deployment (CD)
Containerization
Orchestration
Infrastructure as Code (IaC)
Monitoring and Logging
Microsoft Teams vs Slack Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read
Security in DevOps