feat(nlb): add Network Load Balancer module and dev-nlb environment

This commit is contained in:
Haitao Pan 2025-11-17 17:00:34 +08:00
parent 245e5d9b89
commit c05364dfe0
8 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,13 @@
name_prefix: "dev-nlb"
vpc_id: "vpc-0d0d8d822fa215104"
subnet_ids:
- "subnet-0c370f7ff7311388e"
- "subnet-0b609b5773fe957fa"
listeners:
- port: 80
protocol: "TCP"
target_group_port: 80
target_group_protocol: "TCP"

View File

@ -0,0 +1,9 @@
terraform {
backend "s3" {
bucket = "svc-plus-iac-state"
key = "account/dev/nlb/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")
)
nlb_conf = yamldecode(
file("${path.root}/../../config/resources/dev-nlb/nlb.yaml")
)
}
module "nlb" {
source = "../../modules/nlb"
name_prefix = local.nlb_conf.name_prefix
vpc_id = local.nlb_conf.vpc_id
subnet_ids = local.nlb_conf.subnet_ids
listeners = local.nlb_conf.listeners
tags = local.account.tags
}

View File

@ -0,0 +1,11 @@
output "nlb_arn" {
value = module.nlb.nlb_arn
}
output "nlb_dns" {
value = module.nlb.nlb_dns
}
output "target_group_arns" {
value = module.nlb.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,34 @@
resource "aws_lb" "this" {
name = "${var.name_prefix}-nlb"
load_balancer_type = "network"
subnets = var.subnet_ids
idle_timeout = 60
tags = merge(var.tags, {
Name = "${var.name_prefix}-nlb"
})
}
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
target_type = "instance"
vpc_id = var.vpc_id
}
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
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.tg[each.key].arn
}
}

View File

@ -0,0 +1,11 @@
output "nlb_arn" {
value = aws_lb.this.arn
}
output "nlb_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,26 @@
variable "name_prefix" {
type = string
}
variable "vpc_id" {
type = string
}
variable "subnet_ids" {
type = list(string)
}
variable "listeners" {
description = "List of listener configurations"
type = list(object({
port = number
protocol = string
target_group_port = number
target_group_protocol = string
}))
}
variable "tags" {
type = map(string)
default = {}
}