Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
wailinoo1 committed Feb 21, 2024
1 parent ee4db78 commit 123d20d
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 0 deletions.
64 changes: 64 additions & 0 deletions terraform/ECS/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
resource "aws_ecs_task_definition" "definition" {
family = var.family_name
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = var.cpu
memory = var.memory
container_definitions = <<TASK_DEFINITION
[
{
"name": "${var.container_name}",
"image": "${var.image}",
"cpu": 1024,
"memory": 2048,
"essential": true,
"portMappings": [
{
"containerPort": 8080,
"hostPort": 8080
}
]
}
]
TASK_DEFINITION

runtime_platform {
operating_system_family = "${var.os}"
cpu_architecture = "${var.osarchitecture}"
}
task_role_arn = var.task_role_arn
execution_role_arn = var.task_role_arn
}

resource "aws_ecs_cluster" "cluster" {
name = var.ecs_cluster_name

setting {
name = "containerInsights"
value = "enabled"
}
}



resource "aws_ecs_service" "node_service" {
name = "terraform_nodejs_service"
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.definition.arn
desired_count = 2
lifecycle {
ignore_changes = [desired_count]
}

network_configuration {
subnets = [for subnet in var.subnetid : subnet]
assign_public_ip = false
security_groups = [var.ecs_sg_id]
}

load_balancer {
target_group_arn = var.tgb_ecs_arn
container_name = var.container_name
container_port = var.container_port
}
}
12 changes: 12 additions & 0 deletions terraform/ECS/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.33.0"
}
}
}

provider "aws" {
region = "ap-southeast-1"
}
58 changes: 58 additions & 0 deletions terraform/ECS/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
variable "family_name" {

}

variable "container_name" {

}

variable "cpu" {

}
variable "memory" {

}
variable "image" {

}

variable "os" {

}

variable "osarchitecture" {

}

variable "task_role_arn" {

}

variable "ecs_cluster_name" {

}

variable "subnetid" {

}

variable "vpcid" {

}
variable "ecs_tgb_name" {

}
variable "container_port" {

}

# variable "alb-name" {

# }
variable "tgb_ecs_arn" {

}

variable "ecs_sg_id" {

}
83 changes: 83 additions & 0 deletions terraform/LoadBalancer/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
resource "aws_s3_bucket" "ecs-alb-logs" {
bucket = var.ecs-alblogs3
tags = {
Name = "${var.ecs-alblogs3}"
}
}

resource "aws_s3_bucket_policy" "alb_access_logs_policy" {
bucket = aws_s3_bucket.ecs-alb-logs.bucket

policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::114774131450:root"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::${var.ecs-alblogs3}/*"
}
]
})
}
resource "aws_security_group" "ecs-sg" {
name = var.ecs_sg_name
vpc_id = var.vpcid

ingress{
from_port = 8080
to_port = 8080
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_lb" "terraform-alb" {
name = var.alb-name
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.ecs-sg.id]
subnets = [for subnet in var.public-subnetid : subnet]


enable_deletion_protection = false

access_logs {
bucket = aws_s3_bucket.ecs-alb-logs.id
enabled = true
}

tags = {
Environment = "${var.alb-name}"
}
}


resource "aws_lb_target_group" "ecs-tgb" {
name = var.ecs_tgb_name
port = 8080
protocol = "HTTP"
target_type = "ip"
vpc_id = var.vpcid
}

resource "aws_lb_listener" "listen80" {
load_balancer_arn = aws_lb.terraform-alb.arn
port = "80"
protocol = "HTTP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.ecs-tgb.arn
}
depends_on = [ aws_lb.terraform-alb , aws_lb_target_group.ecs-tgb]
}
7 changes: 7 additions & 0 deletions terraform/LoadBalancer/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "tgb_ecs_arn" {
value = aws_lb_target_group.ecs-tgb.arn
}

output "ecs_sg_id" {
value = aws_security_group.ecs-sg.id
}
12 changes: 12 additions & 0 deletions terraform/LoadBalancer/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.33.0"
}
}
}

provider "aws" {
region = "ap-southeast-1"
}
23 changes: 23 additions & 0 deletions terraform/LoadBalancer/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "ecs-alblogs3" {

}

variable "public-subnetid" {

}

variable "alb-name" {

}

variable "vpcid" {

}

variable "ecs_tgb_name" {

}

variable "ecs_sg_name" {

}
93 changes: 93 additions & 0 deletions terraform/Network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr_block
instance_tenancy = "default"
enable_dns_hostnames = "true"
enable_dns_support = "true"
tags = {
Name = "${var.vpcname}" #Naming for VPC
}
}

resource "aws_internet_gateway" "internet-gateway" {
vpc_id = aws_vpc.main.id
tags = {
Name = "${var.wlo-terraform-igw-name}"
}
depends_on = [ aws_vpc.main ]
}


locals {
subnet = cidrsubnets(var.vpc_cidr_block,11,11,8,8)
}

resource "aws_subnet" "subnet" {
count = length(local.subnet)
vpc_id = aws_vpc.main.id
cidr_block = local.subnet[count.index]

availability_zone = element(["ap-southeast-1a", "ap-southeast-1b"], count.index)
tags = {
Name = "${var.subnet-name}-${local.subnet[count.index]}"
}
depends_on = [ aws_vpc.main ]
}

resource "aws_eip" "eip" {
domain = "vpc"
depends_on = [ aws_internet_gateway.internet-gateway ]
}


resource "aws_nat_gateway" "nat-gateway" {
depends_on = [aws_subnet.subnet]
allocation_id = aws_eip.eip.id
subnet_id = aws_subnet.subnet[0].id
tags = {
Name = "${var.natgw-name}"
}
}

resource "aws_route_table" "public-subnet-routetable" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet-gateway.id
}

tags = {
Name = "${var.publicrtname}"
}
depends_on = [ aws_vpc.main ]

}

resource "aws_route_table_association" "publicassociation" {
count = 2
subnet_id = aws_subnet.subnet[count.index].id
route_table_id = aws_route_table.public-subnet-routetable.id

depends_on = [ aws_route_table.public-subnet-routetable ]
}

resource "aws_route_table" "private-subnet-routetable" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_nat_gateway.nat-gateway.id
}

tags = {
Name = "${var.privatertname}"
}
depends_on = [ aws_vpc.main ]
}
resource "aws_route_table_association" "privateassociation" {
count = 2
subnet_id = aws_subnet.subnet[count.index + 2 ].id
route_table_id = aws_route_table.private-subnet-routetable.id

depends_on = [ aws_route_table.private-subnet-routetable ]
}
13 changes: 13 additions & 0 deletions terraform/Network/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
output "subnetid" {
value = [for id in aws_route_table_association.privateassociation : id.subnet_id]

}

output "vpcid" {
value = aws_vpc.main.id
}

output "public-subnetid" {
value = [for id in aws_route_table_association.publicassociation : id.subnet_id]

}
12 changes: 12 additions & 0 deletions terraform/Network/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.33.0"
}
}
}

provider "aws" {
region = "ap-southeast-1"
}
Loading

0 comments on commit 123d20d

Please sign in to comment.