Creating a VPC
Let's see how to create a VPC in Terraform.
In the Terraform course, I cover making a VPC "from scratch". In this video, we'll cut to the chase and use an official Terraform community module.
Here's the layout of the directory and files we'll be creating:
1.2├── cloudcasts.tf3└── modules4 └── vpc5 ├── main.tf6 ├── outputs.tf7 └── variables.tf
File cloudcasts.tf
Here's what it looks like to use or VPC Terraform module:
1### 2# Providers 3## 4terraform { 5 required_providers { 6 aws = { 7 source = "hashicorp/aws" 8 version = "3.41.0" 9 }10 }11 12 backend "s3" {13 bucket = "cloudcasts-courses"14 key = "best-parts/terraform.tfstate"15 profile = "cloudcasts"16 region = "us-east-2"17 }18}19 20provider "aws" {21 profile = "cloudcasts"22 region = "us-east-2"23}24 25 26###27# Variables28##29variable "infra_env" {30 type = string31 description = "infrastructure environment"32 default = "production"33}34 35variable "default_region" {36 type = string37 description = "the region this infrastructure is in"38 default = "us-east-2"39}40 41locals {42 cidr_subnets = cidrsubnets("10.0.0.0/17", 4, 4, 4, 4, 4, 4)43}44 45###46# Resources47##48module "vpc" {49 source = "./modules/vpc"50 51 infra_env = var.infra_env52 vpc_cidr = "10.0.0.0/17"53 azs = ["us-east-2a", "us-east-2b", "us-east-2c"]54 public_subnets = slice(local.cidr_subnets, 0, 3)55 private_subnets = slice(local.cidr_subnets, 3, 6)56}
File modules/vpc/main.tf
We create our own vpc
module, which itself uses the Terraform community module:
1# https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/latest 2module "vpc" { 3 source = "terraform-aws-modules/vpc/aws" 4 version = "3.0.0" 5 6 # insert the 49 required variables here 7 name = "cloudcasts-${var.infra_env}-vpc" 8 cidr = var.vpc_cidr 9 10 azs = var.azs11 12 # Single NAT Gateway, see docs linked above13 enable_nat_gateway = true14 single_nat_gateway = true15 one_nat_gateway_per_az = false16 17 private_subnets = var.private_subnets18 public_subnets = var.public_subnets19 20 tags = {21 Name = "cloudcasts-${var.infra_env}-vpc"22 Project = "cloudcasts.io"23 Environment = var.infra_env24 ManagedBy = "terraform"25 }26 27 private_subnet_tags = {28 Role = "private"29 }30 31 public_subnet_tags = {32 Role = "public"33 }34}
File modules/vpc/variables.tf
1variable "infra_env" { 2 type = string 3 description = "infrastructure environment" 4} 5 6variable "vpc_cidr" { 7 type = string 8 description = "The IP range to use for the VPC" 9 default = "10.0.0.0/16"10}11 12variable "azs" {13 type = list(string)14 description = "AZs to create subnets into"15}16 17variable "public_subnets" {18 type = list(string)19 description = "subnets to create for public network traffic, one per AZ"20}21 22variable "private_subnets" {23 type = list(string)24 description = "subnets to create for private network traffic, one per AZ"25}
File modules/vpc/outputs.tf
1output "vpc_id" { 2 value = module.vpc.vpc_id 3} 4 5output "vpc_cidr" { 6 value = module.vpc.vpc_cidr_block 7} 8 9output "vpc_public_subnets" {10 value = module.vpc.public_subnets11}12 13output "vpc_private_subnets" {14 value = module.vpc.private_subnets15}