VPC module for creating VPC with all the necessary components:
a) Declare variable for VPC
variable "create_vpc" {
description = "Controls if VPC should be created (it affects almost all resources)"
type = bool
default = true
}
variable "name" {
description = "Name to be used on all the resources as identifier"
type = string
default = "Workshop01_VPC"
}
#CIDR block
variable "cidr" {
description = "(Optional) The IPv4 CIDR block for the VPC. CIDR can be explicitly set or it can be derived from IPAM using `ipv4_netmask_length` & `ipv4_ipam_pool_id`"
type = string
default = "10.0.0.0/16"
}
# availability zones
variable "azs" {
description = "A list of availability zones names or ids in the region"
type = list(string)
default = ["us-east-1a", "us-east-1b"]
}
# VPC Public Subnets
variable "vpc_public_subnets" {
description = "VPC Public Subnets"
type = list(string)
default = ["10.0.101.0/24","10.0.102.0/24"]
}
# VPC Private Subnets
variable "vpc_private_subnets" {
description = "VPC Private Subnets"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
# VPC Database Subnets
variable "vpc_database_subnets" {
description = "VPC Private Subnets"
type = list(string)
default = ["10.0.3.0/24", "10.0.4.0/24"]
}
b) Create VPC module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.5.1"
#Asset VPC Details
name = var.name
cidr = var.cidr
azs = var.azs
private_subnets = var.vpc_private_subnets
public_subnets = var.vpc_public_subnets
database_subnets = var.vpc_database_subnets
# VPC DNS Parameters
enable_dns_hostnames = true
enable_dns_support = true
# NAT gateway
enable_nat_gateway = true
public_subnet_tags = {
Type = "Public Subnets"
}
private_subnet_tags = {
Type = "Private Subnets"
}
database_subnet_tags = {
Type = "Database Subnets"
}
}
c) Output VPC value
# VPC ID
output "vpc_id" {
description = "The ID of the VPC"
value = module.vpc.vpc_id
}
# VPC Private Subnets
output "private_subnets" {
description = "List of IDs of private subnets"
value = module.vpc.private_subnets
}
# VPC Public Subnets
output "public_subnets" {
description = "List of IDs of public subnets"
value = module.vpc.public_subnets
}
# VPC AZs
output "azs" {
description = "A list of availability zones spefified as argument to this module"
value = module.vpc.azs
}