From b94e8f4dfcf21ee19cbf7ec237b1cfd66d48d56a Mon Sep 17 00:00:00 2001 From: Chris Smith <1979423+chris13524@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:45:07 -0500 Subject: [PATCH] fix: downscale (#385) --- terraform/ecs/cluster.tf | 11 +++++++---- terraform/ecs/cluster_autoscaling.tf | 6 +++++- terraform/postgres/main.tf | 2 +- terraform/res_application.tf | 4 ++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/terraform/ecs/cluster.tf b/terraform/ecs/cluster.tf index 9393f074..ef789d62 100644 --- a/terraform/ecs/cluster.tf +++ b/terraform/ecs/cluster.tf @@ -1,6 +1,9 @@ locals { image = "${var.ecr_repository_url}:${var.image_version}" + task_cpu = module.this.stage == "prod" ? var.task_cpu : 256 + task_memory = module.this.stage == "prod" ? var.task_memory : 512 + otel_port = var.port + 1 otel_cpu = 128 otel_memory = 128 @@ -12,8 +15,8 @@ locals { module "ecs_cpu_mem" { source = "app.terraform.io/wallet-connect/ecs_cpu_mem/aws" version = "1.0.0" - cpu = var.task_cpu + local.otel_cpu - memory = var.task_memory + local.otel_memory + cpu = local.task_cpu + memory = local.task_memory } #------------------------------------------------------------------------------- @@ -60,8 +63,8 @@ resource "aws_ecs_task_definition" "app_task" { { name = module.this.id, image = local.image, - cpu = var.task_cpu, - memory = var.task_memory, + cpu = local.task_cpu - local.otel_cpu, + memory = local.task_memory - local.otel_memory, essential = true, environment = [for each in [ diff --git a/terraform/ecs/cluster_autoscaling.tf b/terraform/ecs/cluster_autoscaling.tf index d20ed763..8800c3c3 100644 --- a/terraform/ecs/cluster_autoscaling.tf +++ b/terraform/ecs/cluster_autoscaling.tf @@ -1,5 +1,9 @@ +locals { + autoscaling_min_capacity = module.this.stage == "prod" ? var.autoscaling_min_capacity : 1 +} + resource "aws_appautoscaling_target" "ecs_target" { - min_capacity = var.autoscaling_min_capacity + min_capacity = local.autoscaling_min_capacity max_capacity = var.autoscaling_max_capacity resource_id = "service/${aws_ecs_cluster.app_cluster.name}/${aws_ecs_service.app_service.name}" scalable_dimension = "ecs:service:DesiredCount" diff --git a/terraform/postgres/main.tf b/terraform/postgres/main.tf index ae464a28..2a9833b8 100644 --- a/terraform/postgres/main.tf +++ b/terraform/postgres/main.tf @@ -44,7 +44,7 @@ module "db_cluster" { cloudwatch_log_group_retention_in_days = var.cloudwatch_retention_in_days serverlessv2_scaling_configuration = { - min_capacity = var.min_capacity + min_capacity = module.this.stage == "prod" ? var.min_capacity : 0.5 max_capacity = var.max_capacity } } diff --git a/terraform/res_application.tf b/terraform/res_application.tf index b09c2fbf..a0582023 100644 --- a/terraform/res_application.tf +++ b/terraform/res_application.tf @@ -35,8 +35,8 @@ module "ecs" { ecr_repository_url = local.ecr_repository_url image_version = var.image_version task_execution_role_name = aws_iam_role.application_role.name - task_cpu = 8064 # 8192 - 128 - task_memory = 16256 # 16384-128 + task_cpu = 4096 + task_memory = 8192 autoscaling_desired_count = var.app_autoscaling_desired_count autoscaling_min_capacity = var.app_autoscaling_min_capacity autoscaling_max_capacity = var.app_autoscaling_max_capacity