🎓 A1Learner
🔍
0 of 50 lessons completed

Networks: A VPC

A VPC is your own private network in the AWS cloud. It holds subnets, route tables, and security groups, and it keeps your resources apart from everyone else.

One private network

The CIDR block sets the address range. 10.0.0.0/16 holds about 65,000 addresses.

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

A slice of the network

The subnet takes a smaller range from the VPC. It belongs to the VPC through its id.

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

A rule that allows web traffic

The ingress rule allows traffic on port 80 from anywhere. All other traffic in is refused.

resource "aws_security_group" "web" {
  name   = "lesson-web"
  vpc_id = aws_vpc.main.id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

Example

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.72.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  endpoints {
    ec2 = "http://localhost:5000"
  }
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "lesson-vpc"
  }
}

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

  tags = {
    Name = "lesson-public"
  }
}

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

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.main.id
  }
}

resource "aws_route_table_association" "public" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.public.id
}

resource "aws_security_group" "web" {
  name   = "lesson-web"
  vpc_id = aws_vpc.main.id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

output "vpc_id" {
  value = aws_vpc.main.id
}

output "subnet_id" {
  value = aws_subnet.public.id
}

Key points

  • A VPC is a private network with its own address range.
  • A subnet is a slice of that range, in one availability zone.
  • A route table decides where traffic from a subnet goes.
  • An internet gateway connects a VPC to the internet.
  • A security group is a firewall that guards a resource.
  • Rules in a security group allow traffic in and out.

The configuration builds a VPC, one public subnet, an internet gateway, a route table that sends traffic to it, and a security group that allows web traffic in. The VPC and its subnet exist only inside moto_server. Destroy at the finish takes the whole network down.

Note

The VPC is built in the local emulator. It is not a real network in AWS.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What makes a subnet public?