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 ## target group so we only need to provide the pool
##################################################################### #####################################################################
resource "aws_lb_target_group" "nginx" { resource "aws_lb_target_group" "nginx" {
for_each = toset(local.buckets) name = local.nginx_name
name = "${var.athens_prefix}-${replace(each.value, ".", "-")}"
port = var.nginx_port port = var.nginx_port
protocol = "HTTP" protocol = "HTTP"
target_type = "ip" target_type = "ip"
vpc_id = aws_vpc.athens_vpc.id vpc_id = aws_vpc.athens_vpc.id
health_check { 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" { resource "aws_lb_listener_rule" "beta" {
for_each = {
for index, record in local.buckets:
index => record
}
listener_arn = aws_lb_listener.https.arn listener_arn = aws_lb_listener.https.arn
priority = 100 + each.key priority = 100
action { action {
type = "forward" type = "forward"
target_group_arn = aws_lb_target_group.nginx[each.value].arn target_group_arn = aws_lb_target_group.nginx.arn
} }
condition { condition {
host_header { host_header {
values = [ each.value ] values = local.buckets
} }
} }
} }

View File

@ -3,5 +3,11 @@ locals {
repos = [ repos = [
"reverse-proxy", "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" { resource "aws_ecs_task_definition" "beta" {
for_each = toset(local.buckets)
family = "${var.athens_prefix}-beta" family = "${var.athens_prefix}-beta"
network_mode = "awsvpc" network_mode = "awsvpc"
@ -14,18 +10,11 @@ resource "aws_ecs_task_definition" "beta" {
container_definitions = jsonencode([ 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" image = "805875567437.dkr.ecr.us-west-1.amazonaws.com/reverse-proxy:latest"
cpu = 256 cpu = 256
memory = 512 memory = 512
essential = true 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 = [ portMappings = [
{ {
containerPort = var.nginx_port, containerPort = var.nginx_port,
@ -49,16 +38,14 @@ resource "aws_ecs_task_definition" "beta" {
} }
resource "aws_ecs_service" "beta_reverse_proxy" { resource "aws_ecs_service" "beta_reverse_proxy" {
for_each = toset(local.buckets) name = local.nginx_name
name = "${var.athens_prefix}-proxy-${replace(each.value, ".", "-")}"
cluster = aws_ecs_cluster.alpha.id 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 desired_count = 1
launch_type = "FARGATE" launch_type = "FARGATE"
load_balancer { load_balancer {
target_group_arn = aws_lb_target_group.nginx[each.value].arn target_group_arn = aws_lb_target_group.nginx.arn
container_name = "${var.athens_prefix}-${replace(each.value, ".", "-")}" container_name = local.nginx_name
container_port = var.nginx_port container_port = var.nginx_port
} }

View File

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