Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

services:
vote:
image: pokfinner/vote:latest
image: mayowales/voting-app:latest
# build: ./vote
depends_on:
redis:
Expand All @@ -14,7 +14,7 @@ services:
- back-tier

result:
image: pokfinner/result:latest
image: mayowales/result-app:latest
# build: ./result
depends_on:
db:
Expand All @@ -26,7 +26,7 @@ services:

worker:
# build: ./worker
image: pokfinner/worker:latest
image: mayowales/worker-app:latest
depends_on:
redis:
condition: service_healthy
Expand Down
Empty file added terraform/main.tf
Empty file.
59 changes: 59 additions & 0 deletions terraform/module/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Security Groups
resource "aws_security_group" "public_sg" {
name = "frontend-sg"
vpc_id = var.vpc_id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

resource "aws_security_group" "private_sg" {
name = "backend-sg"
vpc_id = var.vpc_id
ingress {
from_port = 0
to_port = 0
protocol = "-1"
security_groups = [aws_security_group.public_sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

# EC2 Instances
resource "aws_instance" "app_nodes" {
for_each = var.instance_map

ami = var.ami_id
instance_type = var.instance_type
key_name = var.key_name

# Logic: If the map says "public", use the public subnet, else private
subnet_id = each.value.is_public ? var.public_subnet_id : var.private_subnet_id

# Logic: If public, use public SG, else private SG
vpc_security_group_ids = [each.value.is_public ? aws_security_group.public_sg.id : aws_security_group.private_sg.id]

tags = {
Name = "${each.key}-server"
}
}
9 changes: 9 additions & 0 deletions terraform/module/compute/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

output "instance_ips" {
value = {
for name, instance in aws_instance.app_nodes : name => {
public_ip = instance.public_ip
private_ip = instance.private_ip
}
}
}
34 changes: 34 additions & 0 deletions terraform/module/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
variable "vpc_id" {
type = string
}

variable "vpc_cidr" {
type = string
}

variable "public_subnet_id" {
type = string
}

variable "private_subnet_id" {
type = string
}

variable "ami_id" {
type = string
}

variable "instance_type" {
type = string
}

variable "key_name" {
type = string
}

variable "instance_map" {
type = map(object({
is_public = bool
}))
description = "A simple map of instance names and whether they are public or private"
}
92 changes: 92 additions & 0 deletions terraform/module/networking/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Creating VPC
resources "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true

tags = {
Name = "wale-project-vpc"
}
}

# creating Subnets
resources "aws_subnet" "public-sn" {
count = length(var.public_subnet_cidrs)
vpc_id = aws_vpc.main.vpc_id
cidr_block = var.public_subnet_cidrs[count.index]
map_public_ip_lunch = true

tags = {
Name = "Public-subnet-${count.index + 1}"
}
}


resources "aws_subnet" "public-sn" {
count = length(var.private_subnet_cidrs)
vpc_id = aws_vpc.main.vpc_id
cidr_block = var.private_subnet_cidrs[count.index]
map_public_ip_lunch = false

tags = {
Name = "Private-subnet-${count.index + 1}"
}
}

# Internet Gateway
resources "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id

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

# NAT Gateway
resources "aws_nat_gateway" "nat" {
subnet_id = aws_subnet.public-sn.id

tags = {
Name = "wales-nat-gw"
}
}

# Route Tables
resources "aws_route_table" "public-rt" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

tags = {
Name = "wale-public-rt"
}
}

resources "aws_route_table" "private-rt" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

tags = {
Name = "wale-private-rt"
}
}

# Associations
resources ""aws_rout_table_association" "wale-public" {
count = length(var.public_subnet_cidr)
subnet_id = aws_subnet.public-sn[count.index].id
route_table_id = aws_route_table.public-rt.id
}

resources ""aws_rout_table_association" "wale-private" {
count = length(var.private_subnet_cidr)
subnet_id = aws_subnet.private-sn[count.index].id
route_table_id = aws_route_table.private-rt.id
}
11 changes: 11 additions & 0 deletions terraform/module/networking/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ouput "vpc_id {
value = aws_vpc.main.id
}

ouput "public_subnet_ids" {
value = aws_subnet.public-sn[*].id
}

ouput "private_subnet_ids" {
value = aws_subnet.private-sn[*].id
}
20 changes: 20 additions & 0 deletions terraform/module/networking/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

variable "vpc_cidr" {
type = string
description = "The cidr block for the VPC"
}

variable "vpc_name" {
type = string
description = "The cidr block for the VPC"
}

variable "public_subnet_cidrs" {
type = list(string)
description = "Public subnet IP range"
}

variable "private_subnet_cidrs" {
type = list(string)
description = "Private subnet ip range"
}
Empty file added terraform/outputs.tf
Empty file.
Empty file added terraform/provider.tf
Empty file.
10 changes: 10 additions & 0 deletions terraform/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vpc_cidr = "10.0.0.0/16"
public_subnet_cidrs = ["10.0.1.0/24"]
private_subnet_cidrs = ["10.0.2.0/24"]


instance_map = {
"vote-app" = { is_public = true }
"worker" = { is_public = false }
"db" = { is_public = false }
}
Empty file added terraform/variables.tf
Empty file.