Skip to main content

On This Page

Provisioning AWS Networking with Terraform: A Hands-on Infrastructure as Code Guide

3 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Building My First AWS VPC with Terraform: A Beginner-Friendly Guide for Career Changers

Benjamin Tetteh demonstrates the transition from manual cloud configuration to Infrastructure as Code (IaC) by provisioning a Virtual Private Cloud (VPC) via Terraform. This method replaces error-prone AWS Console clicking with a repeatable blueprint using a CIDR block of 10.0.0.0/16.

Why This Matters

While the AWS Console provides a GUI for networking, it lacks the scalability and repeatability required for modern engineering environments. Manual configuration often leads to “snowflake” environments that are impossible to audit or replicate, whereas using Terraform (IaC) ensures that network boundaries, subnets, and routing rules are defined as version-controlled code, reducing human error during complex cloud deployments.

Key Insights

  • Terraform providers act as adapters, such as the HashiCorp AWS provider version 5.0, to bridge the gap between HCL and the AWS API.
  • A CIDR block of 10.0.0.0/16 provides a theoretical capacity of 65,536 IP addresses for a single VPC.
  • Public subnets achieve internet connectivity via an Internet Gateway (IGW) and a route table entry for 0.0.0.0/0.
  • Resource isolation is enforced by creating private subnets without an IGW route, ensuring sensitive assets like databases remain unreachable from the public internet.
  • The use of Terraform variables allows for dynamic configuration of regions and CIDR blocks, enabling environment-agnostic infrastructure deployments.

Working Examples

Core Terraform configuration for an AWS VPC with an Internet Gateway, a public subnet, and routing for external traffic.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.region
}

resource "aws_vpc" "main" {
  cidr_block = var.vpc_cidr_block
  tags = {
    Name = "main-vpc"
  }
}

resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "main-igw"
  }
}

resource "aws_subnet" "public" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  tags = { Name = "main-public-subnet" }
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.main.id
  tags = { Name = "main-public-rt" }
}

resource "aws_route" "public_igw" {
  route_table_id         = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.main.id
}

Practical Applications

  • Use Case: Standardizing multi-region deployments by using variables in Terraform to change the ‘region’ parameter in one central configuration file.
  • Pitfall: Hardcoding VPC IDs or IP ranges directly in resource blocks instead of using variables, which prevents code reusability and complicates environment scaling.
  • Use Case: Isolating database layers in private subnets with /24 masks to provide 256 addresses per zone while blocking inbound internet traffic.
  • Pitfall: Failing to associate route tables with subnets, resulting in resources that default to the main route table and potentially lose intended network isolation.

References:

Continue reading

Next article

Building ClauseGuard: A 5-Agent AI Pipeline for Legal Contract Risk Analysis

Related Content