Terraform

terraform code (alb)

KK_Ryong 2023. 8. 31. 12:16

#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#ACM data생성------------------------------------------------------------------------------------------
resource "aws_acm_certificate" "acm_service" {
  domain_name       = "*.service.co.kr"
  validation_method = "DNS"
}
#API ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-api-alb" {
  name               = "projent-stg-api-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-api-alb-sg.id]
  enable_deletion_protection = true
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "ECS_ALB/projent-stg-api"
    enabled = true
  }
  tags = {    
    Name = "projent-stg-api-alb"
  }
}
resource "aws_lb_listener" "http_forward_api" {
  load_balancer_arn = aws_lb.projent-stg-api-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-28083-tg.arn
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-api-alb]
}

#ADMIN ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-admin-alb" {
  name               = "projent-stg-admin-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  idle_timeout = "3600"
  security_groups    = [aws_security_group.projent-stg-admin-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "ECS_ALB/projent-stg-admin"
    enabled = true
  }
  tags = {    
    Name = "projent-stg-admin-alb"
  }
}
resource "aws_lb_listener" "http_forward_admin" {
  load_balancer_arn = aws_lb.projent-stg-admin-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-28083-tg.arn
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-admin-alb]
}


#cron ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-cron-alb" {
  name               = "projent-stg-cron-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  idle_timeout = "3600"
  security_groups    = [aws_security_group.projent-stg-cron-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "ECS_ALB/projent-stg-cron"
    enabled = true
  }
  tags = {    
    Name = "projent-stg-cron-alb"
  }
}
resource "aws_lb_listener" "http_forward_cron" {
  load_balancer_arn = aws_lb.projent-stg-cron-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-28083-tg.arn
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-cron-alb]
}


#jenkins ALB생성------------------------------------------------------------------------------------------
resource "aws_lb" "projent-stg-jenkins-alb" {
  name               = "projent-stg-jenkins-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-jenkins-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "EC2_ALB/projent-stg-jenkins"
    enabled = true
  }
  tags = {
    Name = "projent-stg-jenkins-alb"
  }
}


#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#ALB 규칙 생성------------------------------------------------------------------------------------------
resource "aws_lb_listener" "http_forward" {
  load_balancer_arn = aws_lb.projent-stg-jenkins-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-18083-tg.arn
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-jenkins-alb]
}

resource "aws_lb_listener" "jenkins_forward" {
  load_balancer_arn = aws_lb.projent-stg-jenkins-alb.arn
  port              = 9090
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-TLS13-1-2-2021-06"
  certificate_arn   = "${aws_acm_certificate.acm_service.arn}"
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.projent-stg-jenkins-alb-tg.arn
    
  }
  depends_on = [aws_lb.projent-stg-jenkins-alb]
}
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#ALB Role 생성------------------------------------------------------------------------------------------
#jenkins
resource "aws_alb_listener_rule" "http_9090_redirect" {
  listener_arn = "${aws_lb_listener.http_forward.arn}"
  priority     = 2

  action {
    type             = "redirect"
    redirect {
      port        = "9090"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  condition {
    host_header {
      values = ["jenkins.service.co.kr.com"]
    }
  }
}

#prometheus ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-prometheus-alb" {
  name               = "projent-stg-prometheus-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-prometheus-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "EC2_ALB/projent-stg-prometheus"
    enabled = true
  }
  tags = {
    Name = "projent-stg-prometheus-alb"
  }
}
resource "aws_lb_listener" "http_forward_prometheus" {
  load_balancer_arn = aws_lb.projent-stg-prometheus-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-18083-tg.arn
    type = "redirect"
    redirect {
      port        = "9090"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-prometheus-alb]
}

resource "aws_lb_listener" "https_forward_prometheus" {
  load_balancer_arn = aws_lb.projent-stg-prometheus-alb.arn
  port              = 9090
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-TLS13-1-2-2021-06"
  certificate_arn   = "${aws_acm_certificate.acm_service.arn}"
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.projent-stg-prometheus-alb-tg.arn
    
  }
  depends_on = [aws_lb.projent-stg-prometheus-alb]
}
#grafana ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-grafana-alb" {
  name               = "projent-stg-grafana-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-grafana-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "EC2_ALB/projent-stg-grafana"
    enabled = true
  }
  tags = {
    Name = "projent-stg-grafana-alb"
  }
}
resource "aws_lb_listener" "http_forward_grafana" {
  load_balancer_arn = aws_lb.projent-stg-grafana-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-18083-tg.arn
    type = "redirect"
    redirect {
      port        = "3000"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-grafana-alb]
}
resource "aws_lb_listener" "https_forward_grafana" {
  load_balancer_arn = aws_lb.projent-stg-grafana-alb.arn
  port              = 3000
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-TLS13-1-2-2021-06"
  certificate_arn   = "${aws_acm_certificate.acm_service.arn}"
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.projent-stg-grafana-alb-tg.arn
    
  }
  depends_on = [aws_lb.projent-stg-grafana-alb]
}
#push ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-push-alb" {
  name               = "projent-stg-push-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-push-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "EC2_ALB/projent-stg-push"
    enabled = true
  }
  tags = {    
    Name = "projent-stg-push-alb"
  }
}
resource "aws_lb_listener" "http_forward_push" {
  load_balancer_arn = aws_lb.projent-stg-push-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-28084-tg.arn
    type = "redirect"
    redirect {
      port        = 443
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-push-alb]
}

#thumbor ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-thumbor-alb" {
  name               = "projent-stg-thumbor-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-thumbor-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "EC2_ALB/projent-stg-thumbor"
    enabled = true
  }
  tags = {    
    Name = "projent-stg-thumbor-alb"
  }
}
resource "aws_lb_listener" "http_forward_thumbor" {
  load_balancer_arn = aws_lb.projent-stg-thumbor-alb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-18083-tg.arn
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
  depends_on = [aws_lb.projent-stg-thumbor-alb]
}

resource "aws_lb_listener" "https_forward_thumbor" {
  load_balancer_arn = aws_lb.projent-stg-thumbor-alb.arn
  port              = 443
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-TLS13-1-2-2021-06"
  certificate_arn   = "${aws_acm_certificate.acm_service.arn}"
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.projent-stg-thumbor-alb-tg.arn
    
  }
  depends_on = [aws_lb.projent-stg-thumbor-alb]
}
#chatting ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-chatting-alb" {
  name               = "projent-stg-chatting-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-chatting-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "EC2_ALB/projent-stg-chatting"
    enabled = true
  }
  tags = {
    Name = "projent-stg-chatting-alb"
  }
}
resource "aws_lb_listener" "http_forward_chatting" {
  load_balancer_arn = aws_lb.projent-stg-chatting-alb.arn
  port              = 80
  protocol          = "HTTP"
  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-28083-tg.arn
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }

  depends_on = [aws_lb.projent-stg-chatting-alb]
}
#elk ALB생성------------------------------------------------------------------------------------------
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_lb" "projent-stg-elk-alb" {
  name               = "projent-stg-elk-alb"
  subnets            = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.projent-stg-elk-alb-sg.id]
  access_logs {
    bucket  = aws_s3_bucket.projent-stg-accesslog-s3.id
    prefix  = "EC2_ALB/projent-stg-elk"
    enabled = true
  }
  tags = {
    Name = "projent-stg-elk-alb"
  }
}
resource "aws_lb_listener" "http_forward_elk" {
  load_balancer_arn = aws_lb.projent-stg-elk-alb.arn
  port              = 80
  protocol          = "HTTP"
  default_action {
    #type             = "forward"
    #target_group_arn = aws_lb_target_group.projent-stg-alb-28083-tg.arn
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }

  depends_on = [aws_lb.projent-stg-elk-alb]
}