|
| 1 | +data "aws_elb_service_account" "main" {} |
| 2 | + |
| 3 | +# S3 bucket for ALB access logs |
| 4 | +resource "aws_s3_bucket" "lb_access_logs" { |
| 5 | + bucket = var.aws_elb_access_log_bucket_name |
| 6 | + force_destroy = true |
| 7 | + tags = { |
| 8 | + Name = var.aws_elb_access_log_bucket_name |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +resource "aws_s3_bucket_lifecycle_configuration" "lb_access_logs_lifecycle" { |
| 13 | + count = tonumber(var.aws_elb_access_log_expire) > 0 ? 1 : 0 |
| 14 | + bucket = aws_s3_bucket.lb_access_logs.id |
| 15 | + rule { |
| 16 | + id = "ExpirationRule" |
| 17 | + status = "Enabled" |
| 18 | + expiration { |
| 19 | + days = tonumber(var.aws_elb_access_log_expire) |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_s3_bucket_policy" "allow_access_from_elb_account" { |
| 25 | + bucket = aws_s3_bucket.lb_access_logs.id |
| 26 | + policy = <<POLICY |
| 27 | +{ |
| 28 | + "Version": "2012-10-17", |
| 29 | + "Id": "Policy", |
| 30 | + "Statement": [ |
| 31 | + { |
| 32 | + "Effect": "Allow", |
| 33 | + "Principal": { |
| 34 | + "AWS": ["${data.aws_elb_service_account.main.arn}"] |
| 35 | + }, |
| 36 | + "Action": ["s3:PutObject"], |
| 37 | + "Resource": "arn:aws:s3:::${var.aws_elb_access_log_bucket_name}/*" |
| 38 | + } |
| 39 | + ] |
| 40 | +} |
| 41 | +POLICY |
| 42 | + lifecycle { |
| 43 | + ignore_changes = [policy] |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +# ALB Security Group |
| 48 | +resource "aws_security_group" "alb_security_group" { |
| 49 | + name = var.aws_elb_security_group_name != "" ? "${var.aws_elb_security_group_name}-alb" : "SG for ${var.aws_resource_identifier} - ALB" |
| 50 | + description = "SG for ${var.aws_resource_identifier} - ALB" |
| 51 | + vpc_id = var.aws_vpc_selected_id |
| 52 | + egress { |
| 53 | + from_port = 0 |
| 54 | + to_port = 0 |
| 55 | + protocol = "-1" |
| 56 | + cidr_blocks = ["0.0.0.0/0"] |
| 57 | + } |
| 58 | + tags = { |
| 59 | + Name = "${var.aws_resource_identifier}-alb" |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +# Security group rule to allow traffic from ALB to target |
| 64 | +resource "aws_security_group_rule" "incoming_alb" { |
| 65 | + type = "ingress" |
| 66 | + from_port = 0 |
| 67 | + to_port = 0 |
| 68 | + protocol = -1 |
| 69 | + source_security_group_id = aws_security_group.alb_security_group.id |
| 70 | + security_group_id = var.aws_elb_target_sg_id |
| 71 | +} |
| 72 | + |
| 73 | +# ALB Security Group Rules for incoming connections |
| 74 | +resource "aws_security_group_rule" "incoming_alb_ports" { |
| 75 | + count = local.aws_ports_amount |
| 76 | + type = "ingress" |
| 77 | + from_port = local.aws_alb_listen_port[count.index] |
| 78 | + to_port = local.aws_alb_listen_port[count.index] |
| 79 | + protocol = "tcp" |
| 80 | + cidr_blocks = ["0.0.0.0/0"] |
| 81 | + security_group_id = aws_security_group.alb_security_group.id |
| 82 | +} |
| 83 | + |
| 84 | +# Application Load Balancer |
| 85 | +resource "aws_lb" "alb" { |
| 86 | + name = var.aws_resource_identifier_supershort |
| 87 | + internal = false |
| 88 | + load_balancer_type = "application" |
| 89 | + security_groups = [aws_security_group.alb_security_group.id] |
| 90 | + subnets = local.alb_subnets |
| 91 | + |
| 92 | + enable_deletion_protection = false |
| 93 | + |
| 94 | + access_logs { |
| 95 | + bucket = aws_s3_bucket.lb_access_logs.id |
| 96 | + prefix = "alb" |
| 97 | + enabled = true |
| 98 | + } |
| 99 | + |
| 100 | + tags = { |
| 101 | + Name = "${var.aws_resource_identifier_supershort}" |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +# ALB Target Group |
| 106 | +resource "aws_lb_target_group" "alb_targets" { |
| 107 | + count = length(local.aws_alb_app_port) |
| 108 | + name = "${var.aws_resource_identifier_supershort}${count.index}" |
| 109 | + port = local.aws_alb_app_port[count.index] |
| 110 | + protocol = local.alb_app_protocol[count.index] |
| 111 | + vpc_id = var.aws_vpc_selected_id |
| 112 | + |
| 113 | + health_check { |
| 114 | + enabled = true |
| 115 | + healthy_threshold = 2 |
| 116 | + unhealthy_threshold = 2 |
| 117 | + timeout = 5 |
| 118 | + interval = 30 |
| 119 | + path = local.health_check_path[count.index] |
| 120 | + matcher = "200" |
| 121 | + protocol = local.alb_app_protocol[count.index] |
| 122 | + port = "traffic-port" |
| 123 | + } |
| 124 | + |
| 125 | + tags = { |
| 126 | + Name = "${var.aws_resource_identifier_supershort}-tg-${count.index}" |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +# ALB Target Group Attachment |
| 131 | +resource "aws_lb_target_group_attachment" "alb_target_attachment" { |
| 132 | + count = length(local.aws_alb_app_port) |
| 133 | + target_group_arn = aws_lb_target_group.alb_targets[count.index].arn |
| 134 | + target_id = var.aws_instance_server_id |
| 135 | + port = local.aws_alb_app_port[count.index] |
| 136 | +} |
| 137 | + |
| 138 | +# ALB Listeners |
| 139 | +resource "aws_lb_listener" "alb_listener" { |
| 140 | + count = length(local.listener_for_each) |
| 141 | + load_balancer_arn = aws_lb.alb.arn |
| 142 | + port = local.aws_alb_listen_port[count.index] |
| 143 | + protocol = local.alb_listen_protocol[count.index] |
| 144 | + ssl_policy = local.alb_ssl_available && local.alb_listen_protocol[count.index] == "HTTPS" ? "ELBSecurityPolicy-TLS13-1-2-2021-06" : null |
| 145 | + certificate_arn = local.alb_ssl_available && local.alb_listen_protocol[count.index] == "HTTPS" ? var.aws_certificates_selected_arn : null |
| 146 | + |
| 147 | + default_action { |
| 148 | + type = "forward" |
| 149 | + target_group_arn = aws_lb_target_group.alb_targets[count.index].arn |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +# Locals for processing variables |
| 154 | +locals { |
| 155 | + # Check if there is a cert available |
| 156 | + alb_ssl_available = var.aws_certificates_selected_arn != "" ? true : false |
| 157 | + |
| 158 | + # Transform CSV values into arrays |
| 159 | + aws_alb_listen_port = var.aws_elb_listen_port != "" ? [for n in split(",", var.aws_elb_listen_port) : tonumber(n)] : (local.alb_ssl_available ? [443] : [80]) |
| 160 | + aws_alb_app_port = var.aws_elb_app_port != "" ? [for n in split(",", var.aws_elb_app_port) : tonumber(n)] : var.aws_elb_listen_port != "" ? local.aws_alb_listen_port : [3000] |
| 161 | + aws_alb_app_protocol = var.aws_elb_app_protocol != "" ? [for n in split(",", var.aws_elb_app_protocol) : upper(n)] : [] |
| 162 | + |
| 163 | + # Store the lowest array length |
| 164 | + aws_ports_amount = length(local.aws_alb_listen_port) < length(local.aws_alb_app_port) ? length(local.aws_alb_listen_port) : length(local.aws_alb_app_port) |
| 165 | + |
| 166 | + # Store the shortest array for listener creation |
| 167 | + listener_for_each = length(local.aws_alb_listen_port) < length(local.aws_alb_app_port) ? local.aws_alb_listen_port : local.aws_alb_app_port |
| 168 | + |
| 169 | + # Protocol handling |
| 170 | + alb_app_protocol = length(local.aws_alb_app_protocol) < local.aws_ports_amount ? [for _ in range(local.aws_ports_amount) : "HTTP"] : local.aws_alb_app_protocol |
| 171 | + alb_listen_protocol = local.alb_ssl_available ? [for _ in range(local.aws_ports_amount) : "HTTPS"] : [for _ in range(local.aws_ports_amount) : "HTTP"] |
| 172 | + |
| 173 | + # Health check path extraction from healthcheck string |
| 174 | + health_check_path = [for i in range(length(local.aws_alb_app_port)) : |
| 175 | + can(regex("^HTTP:", var.aws_elb_healthcheck)) ? |
| 176 | + try(split(":", var.aws_elb_healthcheck)[1], "/") : |
| 177 | + "/" |
| 178 | + ] |
| 179 | + |
| 180 | + # ALB subnets - use provided subnets or fall back to single subnet |
| 181 | + alb_subnets = length(var.aws_alb_subnets) > 0 ? var.aws_alb_subnets : [var.aws_vpc_subnet_selected] |
| 182 | +} |
| 183 | + |
| 184 | +# Outputs |
| 185 | +output "aws_elb_dns_name" { |
| 186 | + value = aws_lb.alb.dns_name |
| 187 | +} |
| 188 | + |
| 189 | +output "aws_elb_zone_id" { |
| 190 | + value = aws_lb.alb.zone_id |
| 191 | +} |
| 192 | + |
| 193 | +output "alb_arn" { |
| 194 | + value = aws_lb.alb.arn |
| 195 | +} |
| 196 | + |
| 197 | +output "alb_target_group_arns" { |
| 198 | + value = aws_lb_target_group.alb_targets[*].arn |
| 199 | +} |
0 commit comments