Skip to content

Commit

Permalink
Added myvpc from existing sources as module
Browse files Browse the repository at this point in the history
  • Loading branch information
qtkhajacloud committed Aug 12, 2021
1 parent 9492a1d commit 08dd57b
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Aug21/ntierawswithmodules/maint.tf
@@ -0,0 +1,28 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}

# Configure the AWS Provider
provider "aws" {
region = "ap-south-1"
}

module "myvpc" {
source = "./modules/myvpc"
ntier_cidr = "10.1.0.0/16"
ntier_subnet_tags = ["ntier-web1", "ntier-app1", "ntier-db1", "ntier-web2", "ntier-app2", "ntier-db2"]
ntier_subnet_azs = ["ap-south-1a","ap-south-1a","ap-south-1a", "ap-south-1b","ap-south-1b","ap-south-1b"]
web_subnet_indexes = [ 0,3]
other_subnet_indexes = [ 1,2,4,5]

}

output "vpcid" {
value = module.myvpc.vpcid

}
132 changes: 132 additions & 0 deletions Aug21/ntierawswithmodules/modules/myvpc/main.tf
@@ -0,0 +1,132 @@
resource "aws_vpc" "ntiervpc" {
cidr_block = var.ntier_cidr

tags = {
"Name" = "ntier"
}

}
# aws_vpc.ntiervpc.id

# depending on subnet cidr variables
resource "aws_subnet" "subnets" {

count = length(var.ntier_subnet_azs)

cidr_block = cidrsubnet(var.ntier_cidr, 8, count.index)
availability_zone = var.ntier_subnet_azs[count.index]
tags = {
"Name" = var.ntier_subnet_tags[count.index]
}
vpc_id = aws_vpc.ntiervpc.id

depends_on = [
aws_vpc.ntiervpc
]

}

# Create an internet gateway and attach to vpc

resource "aws_internet_gateway" "ntierigw" {
vpc_id = aws_vpc.ntiervpc.id

tags = {
"Name" = "ntier-igw"
}

depends_on = [
aws_vpc.ntiervpc
]

}

# create a public route table

resource "aws_route_table" "publicrt" {
vpc_id = aws_vpc.ntiervpc.id
route = [ ]

tags = {
"Name" = "ntier-publicrt"
}

depends_on = [
aws_vpc.ntiervpc,
aws_subnet.subnets
]
}

resource "aws_route" "publicroute" {
route_table_id = aws_route_table.publicrt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.ntierigw.id
}

resource "aws_route_table_association" "publicrtassociations" {
count = length(var.web_subnet_indexes)
subnet_id = aws_subnet.subnets[var.web_subnet_indexes[count.index]].id
route_table_id = aws_route_table.publicrt.id
}

resource "aws_security_group" "websg" {
name = "openhttp"
description = "Open http and ssh"
vpc_id = aws_vpc.ntiervpc.id

tags = {
"Name" = "Openhttp"
}
depends_on = [
aws_vpc.ntiervpc,
aws_subnet.subnets,
aws_route_table.publicrt,
aws_route_table.ntierprivatert
]

}

resource "aws_security_group_rule" "websghttp" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.websg.id


}

resource "aws_security_group_rule" "websgssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.websg.id



}


resource "aws_route_table" "ntierprivatert" {
vpc_id = aws_vpc.ntiervpc.id
route = [ ]

tags = {
"Name" = "ntier-privatert"
}

}

resource "aws_route_table_association" "privatertassociations" {
count = length(var.other_subnet_indexes)
subnet_id = aws_subnet.subnets[var.other_subnet_indexes[count.index]].id
route_table_id = aws_route_table.ntierprivatert.id

depends_on = [
aws_subnet.subnets,
aws_route_table.ntierprivatert
]
}
4 changes: 4 additions & 0 deletions Aug21/ntierawswithmodules/modules/myvpc/outputs.tf
@@ -0,0 +1,4 @@
output "vpcid" {
value = aws_vpc.ntiervpc.id
}

31 changes: 31 additions & 0 deletions Aug21/ntierawswithmodules/modules/myvpc/variables.tf
@@ -0,0 +1,31 @@
variable "ntier_cidr" {
type = string
default = "10.10.0.0/16"
}

variable "ntier_region" {
type = string
default = "ap-south-1"
}


variable "ntier_subnet_azs" {
default = ["ap-south-1a","ap-south-1a","ap-south-1a"]

}

variable "ntier_subnet_tags" {
default = ["ntier-web1", "ntier-app1", "ntier-db1"]

}

variable "web_subnet_indexes" {
type = list(number)
default = [ 0 ]
}

variable "other_subnet_indexes" {
type = list(number)
default = [ 1,2 ]

}

0 comments on commit 08dd57b

Please sign in to comment.