Last updated
Was this helpful?
Was this helpful?
$ tree
.
└── hello
├── outputs.tf
├── alb.tf
├── var_lb.tf
├── var_sg.tf
└── variables.tf ############ Security Group For External LB
resource "aws_security_group" "external_lb" {
name = "${var.service_name}-${var.vpc_name}-ext"
description = "${var.service_name} external LB SG"
vpc_id = var.target_vpc
# Only allow access from IPs or SGs you specifiy in ext_lb_ingress_cidrs variables
# If you don't want to use HTTPS then remove this block
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.ext_lb_ingress_cidrs
description = "External service https port"
}
# Allow 80 port
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = var.ext_lb_ingress_cidrs
description = "External service http port"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.0.0.0/8"]
description = "Internal outbound any traffic"
}
tags = var.sg_variables.external_lb.tags[var.shard_id]
}
#################### External ALB
resource "aws_lb" "external" {
name = "${var.service_name}-${var.shard_id}-ext"
subnets = var.public_subnets
internal = false
# For external LB,
# Home SG (Includes Office IPs) could be added if this service is internal service.
security_groups = [
aws_security_group.external_lb.id,
]
# For HTTP service, application LB is recommended.
# You could use other load_balancer_type if you want.
load_balancer_type = "application"
tags = var.lb_variables.external_lb.tags[var.shard_id]
}
#################### External LB Target Group
resource "aws_lb_target_group" "external" {
name = "${var.service_name}-${var.shard_id}-ext"
port = var.service_port
protocol = "HTTP"
vpc_id = var.target_vpc
slow_start = var.lb_variables.target_group_slow_start[var.shard_id]
deregistration_delay = var.lb_variables.target_group_deregistration_delay[var.shard_id]
# Change the health check setting
health_check {
interval = 15
port = var.healthcheck_port
path = "/"
timeout = 3
healthy_threshold = 3
unhealthy_threshold = 2
matcher = "200"
}
tags = var.lb_variables.external_lb_tg.tags[var.shard_id]
}
#################### Listener for HTTP service
resource "aws_lb_listener" "internal_80" {
load_balancer_arn = aws_lb.external.arn
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_lb_target_group.external.arn
type = "forward"
}
}# terraform/application/hello/_module/hello/var_lb.tf
variable "lb_variables" {
default = {
target_group_slow_start = {}
target_group_deregistration_delay = {}
internal_lb = {
tags = {}
}
internal_lb_tg = {
tags = {}
}
external_lb = {
tags = {}
}
external_lb_tg = {
tags = {}
}
}
}# terraform/application/hello/devartd_apnortheast2/var_lb.tf
variable "lb_variables" {
default = {
target_group_slow_start = {
devartdapne2 = 0
artdapne2 = 0
artpapne2 = 0
}
target_group_deregistration_delay = {
devartdapne2 = 0
artdapne2 = 60
artpapne2 = 60
}
external_lb = {
tags = {
devartdapne2 = {
Name = "hello-devartd_apnortheast2-external-lb"
app = "hello"
project = "hello"
env = "dev"
stack = "devartd_apnortheast2"
},
artdapne2 = {
Name = "hello-artd_apnortheast2-external-lb"
app = "hello"
project = "hello"
env = "dev"
stack = "artd_apnortheast2"
},
artpapne2 = {
Name = "hello-artp_apnortheast2-external-lb"
app = "hello"
project = "hello"
env = "prod"
stack = "artp_apnortheast2"
}
}
}
external_lb_tg = {
tags = {
devartdapne2 = {
Name = "hello-devartd_apnortheast2-external-tg"
app = "hello"
project = "hello"
env = "dev"
stack = "devartd_apnortheast2"
},
artdapne2 = {
Name = "hello-artd_apnortheast2-external-tg"
app = "hello"
project = "hello"
env = "dev"
stack = "artd_apnortheast2"
},
artpapne2 = {
Name = "hello-artp_apnortheast2-external-tg"
app = "hello"
project = "hello"
env = "prod"
stack = "artp_apnortheast2"
}
}
}
}
}