Terraform VPC and Network Creation Guide
Learn how to design and create cloud networking with Terraform, including VPCs, virtual networks, subnets, routes, security groups, and reusable modules.
Network building blocks
- VPC or virtual network.
- Public and private subnets.
- Route tables.
- Internet gateway or NAT gateway.
- Security groups or network security groups.
- DNS and private endpoints where required.
Example AWS VPC resource
resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags = { Name = "demo-vpc" } }
Example subnet
resource "aws_subnet" "public" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" }
Best practices
- Plan CIDR ranges before writing Terraform.
- Separate public and private subnets.
- Use modules for repeatable network patterns.
- Use clear names and tags.
- Do not expose private services publicly.
Related guides
- Terraform Learning Path
- Terraform Modules Explained
Subnet planning before provisioning
A VPC alone does not supply Internet access. Route tables, internet/NAT gateways, security groups and subnet associations must be designed separately. Check CIDR overlap with existing VPCs, VPNs and peering connections before adding routes; example is a partial fragment.
resource "aws_vpc" "example" {
cidr_block = "10.42.0.0/16"
enable_dns_hostnames = true
tags = { Name = "example-vpc" }
}Related Melochip article: https://melochip.com/posts/terraform-state-file-explained. Commands are illustrative and have not been independently executed.
Practical guidance and verification
Design your address plan first
Choose CIDR ranges that do not overlap networks you may later connect through VPN, peering or a transit gateway. Separate public-facing subnets from private application and database subnets, ideally across multiple availability zones where reliability needs justify it. A subnet is not inherently private: effective exposure depends on routing, gateways, security groups and network ACLs.
Routing and least-privilege rules
An internet gateway enables routed internet connectivity for correctly configured resources; a NAT gateway commonly supports outbound connections from private IPv4 subnets without exposing them directly to unsolicited inbound internet traffic. Scope security-group ingress by port, source and workload role. Avoid broad 0.0.0.0/0 access to administrative ports and understand the differences between stateful security groups and stateless network ACLs.
Test the actual network path
Validate route tables, subnet associations, DNS settings, VPC endpoints, flow logs and security controls rather than judging connectivity from CIDRs alone. Cost estimates should include NAT gateways, data processing and cross-zone traffic where relevant. Review plan output before replacing foundational networking, since VPC and subnet changes can cascade across many resources. Official reference: https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Learn Linux from the ground up with simple explanations of the kernel, distributions, filesystem, commands, pe...
5 min read · Updated Nov 16, 2026
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026
Troubleshoot Kubernetes issues including Pending Pods, CrashLoopBackOff, ImagePullBackOff, service routing, in...
5 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.