Terraform Cloud NetworkingInfrastructure as CodeTerraform

Article & Tutorial

Terraform VPC and Network Creation Guide: Subnets, Routes, and Security Groups

Learn Terraform network creation with VPC, subnets, route tables, internet gateway, NAT gateway, security groups, variables, modules, and best practices.

Private cloud network with subnets, routes, gateways, NAT, and security boundaries
Private cloud network with subnets, routes, gateways, NAT, and security boundaries
Contents

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.

example
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.

Read related Terraform guide

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.

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.