Terraform code cleanup and final reduction of containers from 2 -> 1

Now able to serve multiple websites with 1 nginx container under an LB
Cost savings not massive but still noticable at this point considering
How low the cost is with this kind of setup
This commit is contained in:
shockrah 2023-09-10 16:25:21 -07:00
parent b5e53acf0a
commit 295f933d8a
4 changed files with 16 additions and 35 deletions

View File

@ -21,14 +21,13 @@ resource "aws_lb" "alpha" {
## target group so we only need to provide the pool
#####################################################################
resource "aws_lb_target_group" "nginx" {
for_each = toset(local.buckets)
name = "${var.athens_prefix}-${replace(each.value, ".", "-")}"
name = local.nginx_name
port = var.nginx_port
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.athens_vpc.id
health_check {
interval = 120
interval = local.nginx_hp_check_interval
}
}
@ -65,19 +64,15 @@ resource "aws_lb_listener" "https" {
}
resource "aws_lb_listener_rule" "beta" {
for_each = {
for index, record in local.buckets:
index => record
}
listener_arn = aws_lb_listener.https.arn
priority = 100 + each.key
priority = 100
action {
type = "forward"
target_group_arn = aws_lb_target_group.nginx[each.value].arn
target_group_arn = aws_lb_target_group.nginx.arn
}
condition {
host_header {
values = [ each.value ]
values = local.buckets
}
}
}

View File

@ -3,5 +3,11 @@ locals {
repos = [
"reverse-proxy",
]
buckets = [
"shockrah.xyz",
"resume.shockrah.xyz"
]
nginx_name = "${var.athens_prefix}-nginx-static-content"
nginx_hp_check_interval = 300
}

View File

@ -1,8 +1,4 @@
# This nginx service will replace the beta host so that
# we leverage nginx as a reverse proxy in fargate instead
resource "aws_ecs_task_definition" "beta" {
for_each = toset(local.buckets)
family = "${var.athens_prefix}-beta"
network_mode = "awsvpc"
@ -14,18 +10,11 @@ resource "aws_ecs_task_definition" "beta" {
container_definitions = jsonencode([
{
name = "${var.athens_prefix}-${replace(each.value, ".", "-")}"
name = local.nginx_name
image = "805875567437.dkr.ecr.us-west-1.amazonaws.com/reverse-proxy:latest"
cpu = 256
memory = 512
essential = true
environment = [
{ name = "S3_ACCESS_KEY_ID", value = var.shockrah_xyz_s3_access_key_id },
{ name = "S3_SECRET_KEY", value = var.shockrah_xyz_s3_secret_key },
{ name = "S3_REGION", value = var.aws_region },
{ name = "S3_SERVER", value = "s3-${var.aws_region}.amazonaws.com" },
{ name = "S3_BUCKET_NAME", value = each.value },
]
portMappings = [
{
containerPort = var.nginx_port,
@ -49,16 +38,14 @@ resource "aws_ecs_task_definition" "beta" {
}
resource "aws_ecs_service" "beta_reverse_proxy" {
for_each = toset(local.buckets)
name = "${var.athens_prefix}-proxy-${replace(each.value, ".", "-")}"
name = local.nginx_name
cluster = aws_ecs_cluster.alpha.id
task_definition = aws_ecs_task_definition.beta[each.value].arn
task_definition = aws_ecs_task_definition.beta.arn
desired_count = 1
launch_type = "FARGATE"
load_balancer {
target_group_arn = aws_lb_target_group.nginx[each.value].arn
container_name = "${var.athens_prefix}-${replace(each.value, ".", "-")}"
target_group_arn = aws_lb_target_group.nginx.arn
container_name = local.nginx_name
container_port = var.nginx_port
}

View File

@ -1,10 +1,3 @@
locals {
buckets = [
"shockrah.xyz",
"resume.shockrah.xyz"
]
}
resource "aws_s3_bucket" "static-content" {
for_each = {
for idx, record in local.buckets: