resource "aws_vpc" "vpc-vpc" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "acdl-microservice"
  }
}

output "vpc_id" {
  value = aws_vpc.vpc-vpc.id
}

resource "aws_subnet" "vpc-subnet" {
  cidr_block = "10.0.0.0/16"
  vpc_id = aws_vpc.vpc-vpc.id
  tags = {
    Name = "acdl-microservice"
  }
}

resource "aws_route_table" "vpc-routetable" {
  vpc_id = aws_vpc.vpc-vpc.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.vpc-igw.id
  }
  tags = {
    Name = "acdl-microservice-rt"
  }
}

resource "aws_ecs_cluster" "cluster" {
  name = "acdl-microservice"
}

output "cluster_arn" {
  value = aws_ecs_cluster.cluster.arn
}

output "cluster_id" {
  value = aws_ecs_cluster.cluster.id
}

resource "aws_ecr_repository" "ecr" {
  name = "acdl-microservice"
}

output "repository_url" {
  value = aws_ecr_repository.ecr.repository_url
}

output "repository_arn" {
  value = aws_ecr_repository.ecr.arn
}

resource "aws_iam_role" "roles" {
  name = "acdl-microservice-exec"
  assume_role_policy = jsonencode({"Statement": [{"Action": "sts:AssumeRole", "Effect": "Allow", "Principal": {"Service": "ecs-tasks.amazonaws.com"}}], "Version": "2012-10-17"})
  managed_policy_arns = ["arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"]
}

output "role_arn" {
  value = aws_iam_role.roles.arn
}

output "role_id" {
  value = aws_iam_role.roles.id
}

resource "aws_lb" "alb-loadbalancer" {
  name = "acdl-microservice"
  subnets = [aws_subnet.vpc-subnet.id]
  security_groups = [aws_iam_role.roles.arn]
  load_balancer_type = "application"
}

output "lb_arn" {
  value = aws_lb.alb-loadbalancer.id
}

resource "aws_lb_target_group" "alb-targetgroup" {
  name = "acdl-microservice"
  port = 8080
  target_type = "ip"
  vpc_id = aws_vpc.vpc-vpc.id
  protocol = "HTTP"
}

output "target_group_arn" {
  value = aws_lb_target_group.alb-targetgroup.arn
}

resource "aws_lb_listener" "alb-listener" {
  port = 8080
  default_action {
    type = "forward"
    target_group_arn = aws_lb_target_group.alb-targetgroup.arn
  }
  load_balancer_arn = aws_lb.alb-loadbalancer.id
}

output "listener_arn" {
  value = aws_lb_listener.alb-listener.id
}

resource "aws_ecs_task_definition" "service-taskdefinition" {
  cpu = 256
  memory = 512
  container_definitions = jsonencode([{"essential": true, "image": "581513795199.dkr.ecr.us-east-1.amazonaws.com/acdl-microservice:latest", "name": "app", "portMappings": [{"containerPort": 8080}]}])
  family = "app"
}

output "task_def_arn" {
  value = aws_ecs_task_definition.service-taskdefinition.arn
}

resource "aws_ecs_service" "service-service" {
  cluster = aws_ecs_cluster.cluster.arn
  load_balancer {
    target_group_arn = aws_lb_target_group.alb-targetgroup.arn
    container_name = "app"
    container_port = 8080
  }
  network_configuration {
    subnets = [aws_subnet.vpc-subnet.id]
    security_groups = [aws_iam_role.roles.arn]
  }
  desired_count = 1
  launch_type = "FARGATE"
  task_definition = aws_ecs_task_definition.service-taskdefinition.arn
  name = "acdl-microservice"
}

output "service_arn" {
  value = aws_ecs_service.service-service.id
}

resource "aws_internet_gateway" "vpc-igw" {
  vpc_id = aws_vpc.vpc-vpc.id
  tags = {
    Name = "acdl-microservice-igw"
  }
}

resource "aws_route_table_association" "vpc-rta" {
  subnet_id = aws_subnet.vpc-subnet.id
  route_table_id = aws_route_table.vpc-routetable.id
}
