Terraform Series - Creating a VPC


In this post we will see how to create a private subnet using Terraform.

I am going to create 3 files.

1) VPC Variables file.

2) VPC Module file.

3) VPC Output file.

VPC variable holds all the variables like vpc name, cidr, subnet and az details which are passed as a variables.

# Creating variable for VPC Name - testvpc
variable "vpc_name" {
  description = "VPC Name"
  type = string
  default = "testvpc"  
}

# VPC CIDR
variable "vpc_cidr" {
  type = string
  default = "10.0.0.0/16"
}
 
# VPC Availability Zone
variable "vpc_az" {
  type = list(string)
  default = ["us-east-1a", "us-east-1b"]
}

# VPC Private Subnet
variable "vpc_private_subnets" {
  type = list(string)
  default = ["10.0.1.0/24", "10.0.2.0/24"]
}


Next file contains the core VPC module file which uses the variable file to create a VPC.

# Create VPC Terraform Module
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.78.0"

  # VPC Details
  name = var.vpc_name
  cidr = var.vpc_cidr
  azs = var.vpc_az
  private_subnets = var.vpc_private_subnets

}

Finally, We have output file to record the required outputs.

# VPC Output for reference

output "vpc_id" {
  value = module.vpc.vpc_id
}

output "vpc_cidr" {
    value = module.vpc.vpc_cidr_block  
}

output "private_subnets" {
    value = module.vpc.private_subnets
}

output "azs" {
    value = module.vpc.azs
 
}

You should see the VPC created post > terraform apply.






Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping