Hooking service to a load balancer

This commit is contained in:
shockrah 2022-12-09 22:22:47 -08:00
parent 6212a7d8cc
commit d4dae7618b
2 changed files with 42 additions and 0 deletions

View File

@ -121,6 +121,12 @@ resource "aws_ecs_service" "sample" {
task_definition = aws_ecs_task_definition.sample.arn task_definition = aws_ecs_task_definition.sample.arn
desired_count = 1 desired_count = 1
launch_type = "FARGATE" launch_type = "FARGATE"
load_balancer {
target_group_arn = aws_lb_target_group.alpha_cluster.arn
container_name = "${var.athens_prefix}-sample-container"
container_port = 8080
}
network_configuration { network_configuration {
assign_public_ip = true assign_public_ip = true
subnets = [ local.subnet ] subnets = [ local.subnet ]

36
infra/load-balancer.tf Normal file
View File

@ -0,0 +1,36 @@
# Here is the application load balancer that we use for services hosted on ECS
##############################################################################
# The LB that we'll use to move traffic into our services
#########################################################
resource "aws_lb" "alpha" {
name = "alpha-lb"
internal = false
load_balancer_type = "application"
subnets = [ aws_subnet.delphi.id, aws_subnet.crete_subnet.id ]
security_groups = [ aws_security_group.ecs_web_ingress.id ]
# TODO: change this to true later
enable_deletion_protection = false
}
# ECS services manage themselves when it comes to registering to the
# target group so we only need to provide the pool
####################################################################
resource "aws_lb_target_group" "alpha_cluster" {
name = "${var.athens_prefix}-alpha-cluster"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.athens_vpc.id
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.alpha.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alpha_cluster.arn
}
}