Terraform Model For AWS VPC — PART I

Sridharan r.g
2 min readOct 5, 2022

HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.

An AWS Partner Network (APN) Advanced Technology Partner and member of the AWS DevOps Competency, is an “infrastructure as code” tool similar to AWS CloudFormation that allows you to create, update, and version your Amazon Web Services (AWS) infrastructure.

In this Example we required IAM user with admin Access along the credential. Configure the credential in awscli.

In this example installed in Linux OS.

Follow the link to install Terraform. https://learn.hashicorp.com/tutorials/terraform/install-cli

Create a directory mkdir aws_network.

In the directory create two file main.tf and variable.tf.

First we have to use variable.tf file in this file define the variable.

vi variable.tf

variable “env” {

default = “Dev”

}

variable “aws_vpc” {

default = “10.0.0.0/16”

}

variable “aws_public_subnet_cidr” {

default = [

“10.0.1.0/24”,

“10.0.2.0/24”

]

}

variable “aws_private_subnet_cidr” {

default = [

“10.0.3.0/24”,

“10.0.4.0/24”

]

}

variable “tags” {

default = {

Owner = “Sridharan.RG”

Project = “Test”

}

}

Save the variable file.

Next we use main file

vi main.tf

data “aws_avilability_zone” “avilable” {}

### VPC Creation ###

resource “aws_vpc” “main” {

cidr_block = var.aws_vpc

tags = merge(var.tags, { Name = “${var.env}-vpc”})

}

### Internet Gateway Creation ###

resource “aws_internet_gateway” “main” {

vpc_id = aws_vpc.main.id

tags = merge(var.tags, { Name = “${var.env}-igw”})

}

### Public Subnet Creation ###

resource “aws_subnet” “public_subnets” {

vpc_id = aws_vpc.main.id

count = lenght(var.aws_public_subnet_cidr)

availability_zone = data.aws_avilability_zone.avilable.names[count.index]

map_public_ip_on_launch = true

tags = merge(var.tags, { Name = “${var.env}-public-${count.index +1}”})

}

### Public Subnet Route Tables ###

resource “aws_route_table” “public_subnets” {

vpc_id = aws_vpc.main.id

route = {

cidr_block = “0.0.0.0/0”

gateway_id = aws_internet_gateway.main.id

}

tags = merge(var.tags, { Name = “${var.env}-rt”})

}

### Public Route Table assocaition ###

resource “aws_route_table_association” “public_routes” {

count = lenght(aws_subnet.public_subnets[*].id)

route_table_id = aws_route_table.public_subnets.id

subnet_id = aws_subnet.private_subnets[count.index].id

}

### Elastic IP for NAT ###

resource “aws_eip” “nat” {

subnet_id = var.aws_private_subnet_cidr

vpc = true

tags = merge(var.tags, { Name = “${var.env}-nat-gw”})

}

### NAT Gateway Creation ###

resource “aws_nat_gateway” “nat” {

subnet_id = aws_subnet.public_subnets.id

tags = merge(var.tags, { Name = “${var.env}-nat-gw”})

}

### private Subnet Creation ###

resource “aws_subnet” “private_subnets” {

vpc_id = aws_vpc.main.id

count = lenght(var.aws_private_subnet_cidr)

availability_zone = data.aws_avilability_zone.avilable.names[count.index]

tags = merge(var.tags, { Name = “${var.env}-private-subnet-${count.index +1}”})

}

### Private Subnet Route Tables ###

resource “aws_route_table” “private_subnets” {

vpc_id = aws_vpc.main.id

route = {

cidr_blocks = “0.0.0.0/16”

nat_gateway_id = aws_nat_gateway.nat.id

}

tags = merge(var.tags, { Name = “${var.env}-rt-private-${count.index +1}”})

}

### Private Route Table Association ###

resource “aws_route_table_association” “private_subnets” {

count = lenght(aws_subnet.private_subnets[*].subnet_id)

route_table_id = aws_roue_table.private_subnets[count.indes].id

subnet_id = aws_subnet.private_subnets[count.index].id

}

Save the file.

Once its done init the folder.

terraform init

terraform plan

terraform apply

--

--