|
| 1 | +resource "aws_security_group" "nomoney_alb_sg" { |
| 2 | + name = format("%s-nomoney-alb-sg", var.environment) |
| 3 | + description = "Security group for Application Load Balancer" |
| 4 | + vpc_id = var.vpc_id.id |
| 5 | + |
| 6 | + ingress { |
| 7 | + from_port = 80 |
| 8 | + to_port = 80 |
| 9 | + protocol = "tcp" |
| 10 | + cidr_blocks = ["0.0.0.0/0"] |
| 11 | + description = "Allow HTTP" |
| 12 | + } |
| 13 | + |
| 14 | + ingress { |
| 15 | + from_port = 443 |
| 16 | + to_port = 443 |
| 17 | + protocol = "tcp" |
| 18 | + cidr_blocks = ["0.0.0.0/0"] |
| 19 | + description = "Allow HTTPS" |
| 20 | + } |
| 21 | + |
| 22 | + egress { |
| 23 | + from_port = 0 |
| 24 | + to_port = 0 |
| 25 | + protocol = "-1" |
| 26 | + cidr_blocks = ["0.0.0.0/0"] |
| 27 | + description = "Allow all outbound traffic" |
| 28 | + } |
| 29 | + |
| 30 | + tags = { |
| 31 | + Environment = var.environment |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +resource "aws_lb" "nomoney_alb" { |
| 36 | + name = format("%s-nomoney-alb", var.environment) |
| 37 | + internal = false |
| 38 | + load_balancer_type = "application" |
| 39 | + security_groups = [aws_security_group.nomoney_alb_sg.id] |
| 40 | + subnets = var.public_subnet_ids |
| 41 | + |
| 42 | + enable_deletion_protection = false |
| 43 | + enable_http2 = true |
| 44 | + |
| 45 | + tags = { |
| 46 | + Environment = var.environment |
| 47 | + } |
| 48 | +} |
| 49 | +resource "aws_lb_target_group" "nomoney_tg" { |
| 50 | + name = format("%s-nomoney-tg-blue", var.environment) |
| 51 | + port = 8080 |
| 52 | + protocol = "HTTP" |
| 53 | + vpc_id = var.vpc_id.id |
| 54 | + target_type = "instance" |
| 55 | + |
| 56 | + health_check { |
| 57 | + enabled = true |
| 58 | + healthy_threshold = 2 |
| 59 | + unhealthy_threshold = 2 |
| 60 | + timeout = 5 |
| 61 | + interval = 30 |
| 62 | + path = "/ping" |
| 63 | + protocol = "HTTP" |
| 64 | + matcher = "200" |
| 65 | + } |
| 66 | + |
| 67 | + deregistration_delay = 30 |
| 68 | + |
| 69 | + tags = { |
| 70 | + Environment = var.environment |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +resource "aws_lb_listener" "nomoney_http" { |
| 75 | + load_balancer_arn = aws_lb.nomoney_alb[0].arn |
| 76 | + port = "80" |
| 77 | + protocol = "HTTP" |
| 78 | + |
| 79 | + default_action { |
| 80 | + type = var.environment == "prod" ? "redirect" : "forward" |
| 81 | + |
| 82 | + # Redirect to HTTPS for Production |
| 83 | + dynamic "redirect" { |
| 84 | + for_each = var.environment == "prod" ? [1] : [] |
| 85 | + content { |
| 86 | + port = "443" |
| 87 | + protocol = "HTTPS" |
| 88 | + status_code = "HTTP_301" |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + # Forward to target group for non-Production |
| 93 | + target_group_arn = var.environment != "prod" ? aws_lb_target_group.nomoney_tg.arn : null |
| 94 | + } |
| 95 | + |
| 96 | + tags = { |
| 97 | + Environment = var.environment |
| 98 | + } |
| 99 | + |
| 100 | + lifecycle { |
| 101 | + ignore_changes = [default_action] |
| 102 | + } |
| 103 | +} |
0 commit comments