Skip to content

Commit

Permalink
Added a template to use existing modules
Browse files Browse the repository at this point in the history
  • Loading branch information
qtsathish committed Jan 2, 2022
1 parent fc3f765 commit 5b19c9f
Show file tree
Hide file tree
Showing 19 changed files with 665 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Dec21/activity5/env/dev.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create_db = "no"
build_id = "10"
vnet_range = "10.10.0.0/16"
region = "centralus"
vpc_cidr = "192.168.0.0/16"
instance_type = "t2.micro"
subnet_azs = ["us-west-2a", "us-west-2b", "us-west-2a", "us-west-2b"]
subnet_names = ["Web-1", "Web-2", "DB-1", "DB-2"]
47 changes: 47 additions & 0 deletions Dec21/activity5/inputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variable "vpc_cidr" {
default = "192.168.0.0/16"
description = "This is the VPC cidr"
type = string
}

variable "instance_type" {
default = "t2.micro"
}


variable "subnet_cidrs" {
default = ["192.168.0.0/24","192.168.1.0/24","192.168.2.0/24","192.168.3.0/24"]
description = "These are subnet cidr ranges"
}

variable "subnet_azs" {
default = ["us-west-2a", "us-west-2b", "us-west-2a", "us-west-2b"]
description = "Availability Zones for the subnets"
}

variable "subnet_names" {
default = ["Web-1", "Web-2", "DB-1", "DB-2"]
description = "Names of subnets"

}

variable "vnet_range" {
default = "192.168.0.0/16"
description = "Address space of the vnet"

}

variable "region" {
default = "eastus"
}


variable "build_id" {
default = 1

}

variable "create_db" {
default = "yes"

}
28 changes: 28 additions & 0 deletions Dec21/activity5/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module "awsntier" {
source = "./modules/awsntier"
vpc_cidr = var.vpc_cidr
instance_type = var.instance_type
subnet_cidrs = var.subnet_cidrs
subnet_azs = var.subnet_azs
subnet_names = var.subnet_names

}

module "azurentier" {
source = "./modules/azurentier"
vnet_range = var.vnet_range
region = var.region
subnet_names = var.subnet_names
build_id = var.build_id
create_db = var.create_db

}

output "awswebip" {
value = module.awsntier.web1_publicip

}

output "azurewebip" {
value = module.azurentier.azure_publicip
}
26 changes: 26 additions & 0 deletions Dec21/activity5/modules/awsntier/database.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Db subnet group
resource "aws_db_subnet_group" "ntier_db" {
name = format("ntier-%s", lower(terraform.workspace))
subnet_ids = [aws_subnet.subnets[2].id, aws_subnet.subnets[3].id]

}

# db instance

resource "aws_db_instance" "db" {
count = terraform.workspace == "UAT"? 1 : 0
allocated_storage = 20
apply_immediately = true
auto_minor_version_upgrade = false
backup_retention_period = 0
db_subnet_group_name = aws_db_subnet_group.ntier_db.name
engine = "postgres"
identifier = "qtrdsfortf"
instance_class = "db.t3.micro"
multi_az = false
name = "instacook"
username = "postgres"
password = "postgres"
vpc_security_group_ids = [aws_security_group.dbsg.id]
skip_final_snapshot = true
}
26 changes: 26 additions & 0 deletions Dec21/activity5/modules/awsntier/inputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
variable "vpc_cidr" {
default = "192.168.0.0/16"
description = "This is the VPC cidr"
type = string
}

variable "instance_type" {
default = "t2.micro"
}


variable "subnet_cidrs" {
default = ["192.168.0.0/24","192.168.1.0/24","192.168.2.0/24","192.168.3.0/24"]
description = "These are subnet cidr ranges"
}

variable "subnet_azs" {
default = ["us-west-2a", "us-west-2b", "us-west-2a", "us-west-2b"]
description = "Availability Zones for the subnets"
}

variable "subnet_names" {
default = ["Web-1", "Web-2", "DB-1", "DB-2"]
description = "Names of subnets"

}
8 changes: 8 additions & 0 deletions Dec21/activity5/modules/awsntier/localvalues.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
locals {
anywhere = "0.0.0.0/0"
ssh_port = 22
http_port = 80
https_port = 443
pg_port = 5432
tcp = "TCP"
}
144 changes: 144 additions & 0 deletions Dec21/activity5/modules/awsntier/network.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
resource "aws_vpc" "primary_vpc" {
cidr_block = var.vpc_cidr
tags = {
Name = "primary",
Env = terraform.workspace
}
}


resource "aws_subnet" "subnets" {
count = length(var.subnet_cidrs)

vpc_id = aws_vpc.primary_vpc.id
availability_zone = var.subnet_azs[count.index]
cidr_block = var.subnet_cidrs[count.index]

tags = {
Name = var.subnet_names[count.index],
Env = terraform.workspace
}
}

# Web Security Groups

resource "aws_security_group" "websg" {
vpc_id = aws_vpc.primary_vpc.id

ingress {
description = "Open SSH For all"
from_port = local.ssh_port
to_port = local.ssh_port
protocol = local.tcp
cidr_blocks = [ local.anywhere ]

}

ingress {
description = "Open HTTP For all"
from_port = local.http_port
to_port = local.http_port
protocol = local.tcp
cidr_blocks = [ local.anywhere ]
}

ingress {
from_port = local.https_port
to_port = local.https_port
protocol = local.tcp
cidr_blocks = [ local.anywhere ]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [ local.anywhere ]
ipv6_cidr_blocks = ["::/0"]
}

tags = {
Name = "WebSg",
Env = terraform.workspace
}

}

resource "aws_security_group" "dbsg" {
vpc_id = aws_vpc.primary_vpc.id

ingress {
description = "Open Postgres within VPC"
from_port = local.pg_port
to_port = local.pg_port
protocol = local.tcp
cidr_blocks = [ var.vpc_cidr ]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}

tags = {
Name = "DB Sg"
}

}

# Creating internet gateway
resource "aws_internet_gateway" "ntier_igw" {
tags = {
Name = "ntier-igw"
}
vpc_id = aws_vpc.primary_vpc.id

}

# create a route table
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.primary_vpc.id
tags = {
Name = "public"
}

route {
cidr_block = local.anywhere
gateway_id = aws_internet_gateway.ntier_igw.id
}

}

resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.primary_vpc.id
tags = {
Name = "private",
Env = terraform.workspace
}

}

# Associate public route table with web subnets

resource "aws_route_table_association" "web1_public_association" {
route_table_id = aws_route_table.public_rt.id
subnet_id = aws_subnet.subnets[0].id
}

resource "aws_route_table_association" "web2_public_association" {
route_table_id = aws_route_table.public_rt.id
subnet_id = aws_subnet.subnets[1].id
}

resource "aws_route_table_association" "db1_private_association" {
route_table_id = aws_route_table.private_rt.id
subnet_id = aws_subnet.subnets[2].id
}

resource "aws_route_table_association" "db2_private_association" {
route_table_id = aws_route_table.private_rt.id
subnet_id = aws_subnet.subnets[3].id
}
34 changes: 34 additions & 0 deletions Dec21/activity5/modules/awsntier/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
output "web1_publicip" {
value = aws_instance.web_instance_1[0].public_ip
}

output "db_endpoint" {
value = terraform.workspace == "UAT"?aws_db_instance.db[0].endpoint:""
}
output "vpc_id" {
value = aws_vpc.primary_vpc.id
}
output "web1_subnet_id" {
value = aws_subnet.subnets[0].id
}
output "web2_subnet_id" {
value = aws_subnet.subnets[1].id
}
output "db1_subnet_id" {
value = aws_subnet.subnets[2].id
}
output "db2_subnet_id" {
value = aws_subnet.subnets[3].id
}
output "web_security_group_id" {
value = aws_security_group.websg.id
}
output "db_security_group_id" {
value = aws_security_group.dbsg.id
}
output "web_url" {
value = format("http://%s", aws_instance.web_instance_1[0].public_ip )
}
output "ssh_command" {
value = format("ssh -i %s.pem ubuntu@%s","fortf", aws_instance.web_instance_1[0].public_ip)
}
14 changes: 14 additions & 0 deletions Dec21/activity5/modules/awsntier/web.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resource "aws_instance" "web_instance_1" {
count = terraform.workspace == "UAT"?2:1
ami = "ami-0892d3c7ee96c0bf7"
associate_public_ip_address = true
instance_type = var.instance_type
key_name = "fortf"
vpc_security_group_ids = [aws_security_group.websg.id]
subnet_id = aws_subnet.subnets[count.index].id

tags = {
Name = format("Web-%d", count.index),
Env = terraform.workspace
}
}
Loading

0 comments on commit 5b19c9f

Please sign in to comment.