feat(alb): add Application Load Balancer module and dev-alb environment

This commit is contained in:
Haitao Pan 2025-11-17 17:18:21 +08:00
parent f2996804ac
commit a3f054e8e8
8 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,19 @@
name_prefix: "dev-alb"
vpc_id: "vpc-0d0d8d822fa215104"
subnet_ids:
- "subnet-0c370f7ff7311388e"
- "subnet-0b609b5773fe957fa"
listeners:
- port: 80
protocol: "HTTP"
target_group_port: 80
target_group_protocol: "HTTP"
- port: 443
protocol: "HTTPS"
certificate_arn: "arn:aws:acm:ap-northeast-1:xxxx:certificate/xxxx-xxxx"
target_group_port: 443
target_group_protocol: "HTTP"

View File

@ -0,0 +1,8 @@
terraform {
backend "s3" {
bucket = "svc-plus-iac-state"
key = "account/dev/alb/terraform.tfstate"
region = "ap-northeast-1"
dynamodb_table = "svc-plus-iac-state-dynamodb-lock"
}
}

View File

@ -0,0 +1,20 @@
locals {
account = yamldecode(
file("${path.root}/../../config/accounts/dev.yaml")
)
alb_conf = yamldecode(
file("${path.root}/../../config/resources/dev-alb/alb.yaml")
)
}
module "alb" {
source = "../../modules/alb"
name_prefix = local.alb_conf.name_prefix
vpc_id = local.alb_conf.vpc_id
subnet_ids = local.alb_conf.subnet_ids
listeners = local.alb_conf.listeners
tags = local.account.tags
}

View File

@ -0,0 +1,11 @@
output "alb_arn" {
value = module.alb.alb_arn
}
output "alb_dns" {
value = module.alb.alb_dns
}
output "target_group_arns" {
value = module.alb.target_group_arns
}

View File

@ -0,0 +1,20 @@
terraform {
required_version = ">= 1.2"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.92.0"
}
}
}
provider "aws" {
region = local.account.region
assume_role {
role_arn = "arn:aws:iam::730335654753:role/TerraformDeployRole-Dev"
session_name = "TerraformDevSession"
}
}

View File

@ -0,0 +1,44 @@
resource "aws_lb" "this" {
name = "${var.name_prefix}-alb"
load_balancer_type = "application"
subnets = var.subnet_ids
security_groups = []
tags = merge(var.tags, {
Name = "${var.name_prefix}-alb"
})
}
resource "aws_lb_target_group" "tg" {
for_each = { for l in var.listeners : "${l.port}" => l }
name = "${var.name_prefix}-tg-${each.value.port}"
port = each.value.target_group_port
protocol = each.value.target_group_protocol
vpc_id = var.vpc_id
target_type = "instance"
}
resource "aws_lb_listener" "listener" {
for_each = { for l in var.listeners : "${l.port}" => l }
load_balancer_arn = aws_lb.this.arn
port = each.value.port
protocol = each.value.protocol
dynamic "default_action" {
for_each = [1]
content {
type = "forward"
target_group_arn = aws_lb_target_group.tg[each.key].arn
}
}
dynamic "certificate_arn" {
for_each = each.value.certificate_arn != null ? [each.value.certificate_arn] : []
content {
certificate_arn = certificate_arn
}
}
}

View File

@ -0,0 +1,11 @@
output "alb_arn" {
value = aws_lb.this.arn
}
output "alb_dns" {
value = aws_lb.this.dns_name
}
output "target_group_arns" {
value = { for k, tg in aws_lb_target_group.tg : k => tg.arn }
}

View File

@ -0,0 +1,28 @@
variable "name_prefix" {
type = string
}
variable "vpc_id" {
type = string
}
variable "subnet_ids" {
type = list(string)
}
variable "listeners" {
description = "Listener definitions for ALB"
type = list(object({
port = number
protocol = string
target_group_port = number
target_group_protocol = string
certificate_arn = optional(string)
}))
}
variable "tags" {
type = map(string)
default = {}
}