diff --git a/docker-compose.yml b/docker-compose.yml index f2e4b91b..0aa483fa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ services: vote: - image: pokfinner/vote:latest + image: mayowales/voting-app:latest # build: ./vote depends_on: redis: @@ -14,7 +14,7 @@ services: - back-tier result: - image: pokfinner/result:latest + image: mayowales/result-app:latest # build: ./result depends_on: db: @@ -26,7 +26,7 @@ services: worker: # build: ./worker - image: pokfinner/worker:latest + image: mayowales/worker-app:latest depends_on: redis: condition: service_healthy diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 00000000..e69de29b diff --git a/terraform/module/compute/main.tf b/terraform/module/compute/main.tf new file mode 100644 index 00000000..8fb72067 --- /dev/null +++ b/terraform/module/compute/main.tf @@ -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" + } +} diff --git a/terraform/module/compute/outputs.tf b/terraform/module/compute/outputs.tf new file mode 100644 index 00000000..6c91fdfc --- /dev/null +++ b/terraform/module/compute/outputs.tf @@ -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 + } + } +} diff --git a/terraform/module/compute/variables.tf b/terraform/module/compute/variables.tf new file mode 100644 index 00000000..f3cd3d0e --- /dev/null +++ b/terraform/module/compute/variables.tf @@ -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" +} diff --git a/terraform/module/networking/main.tf b/terraform/module/networking/main.tf new file mode 100644 index 00000000..d3eaf989 --- /dev/null +++ b/terraform/module/networking/main.tf @@ -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 +} diff --git a/terraform/module/networking/outputs.tf b/terraform/module/networking/outputs.tf new file mode 100644 index 00000000..6ce0f61b --- /dev/null +++ b/terraform/module/networking/outputs.tf @@ -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 +} diff --git a/terraform/module/networking/variables.tf b/terraform/module/networking/variables.tf new file mode 100644 index 00000000..6a5d5073 --- /dev/null +++ b/terraform/module/networking/variables.tf @@ -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" +} diff --git a/terraform/outputs.tf b/terraform/outputs.tf new file mode 100644 index 00000000..e69de29b diff --git a/terraform/provider.tf b/terraform/provider.tf new file mode 100644 index 00000000..e69de29b diff --git a/terraform/terraform.tfvars b/terraform/terraform.tfvars new file mode 100644 index 00000000..89d01221 --- /dev/null +++ b/terraform/terraform.tfvars @@ -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 } +} diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 00000000..e69de29b