Skip to main content

How to provision an AWS VPC with Terraform

Provision an AWS VPC, subnet, and internet gateway with Terraform. Covers the provider, resources, and the init, plan, apply, destroy lifecycle.

Difficulty
Beginner
Duration
50 minutes
Steps
6

What and why

Terraform is an infrastructure-as-code tool that declares cloud resources in configuration files and reconciles real infrastructure to match. This tutorial provisions a basic AWS network: a VPC with a public subnet and routing.

Prerequisites

  • Terraform installed and on your PATH.
  • An AWS account with credentials available to the AWS CLI environment.
  • Basic understanding of CIDR ranges and subnets.

Steps

1. Configure the provider

Create main.tf:

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

provider "aws" {
  region = "us-east-1"
}

The provider block tells Terraform which cloud and region to target.

2. Declare the VPC

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  tags = { Name = "demo-vpc" }
}

Each resource block maps to one real object.

3. Add subnets and routing

resource "aws_subnet" "public" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
  map_public_ip_on_launch = true
}

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id
}

References like aws_vpc.main.id create an implicit dependency so Terraform orders creation correctly.

4. Initialize and plan

terraform init
terraform plan

init downloads the provider; plan shows exactly what will change without making changes.

5. Apply the changes

terraform apply

Review the plan and type yes. Terraform creates the resources and records them in state.

Verification

Run terraform state list to see managed resources, or check the VPC in the AWS console. Run terraform plan again; it should report no changes, proving the real state matches your config.

Next Steps

When finished, run terraform destroy to remove everything and avoid charges. Move the configuration into a module, and store state remotely so a team can collaborate safely.

Prerequisites

  • Terraform installed
  • An AWS account with credentials
  • Basic networking concepts

Steps

  • 1
    Configure the provider
  • 2
    Declare the VPC
  • 3
    Add subnets and routing
  • 4
    Initialize and plan
  • 5
    Apply the changes
  • 6
    Verify and destroy