diff --git a/terraform/modules/ecs/README.md b/terraform/modules/ecs/README.md index afe73bb..5d0a154 100644 --- a/terraform/modules/ecs/README.md +++ b/terraform/modules/ecs/README.md @@ -26,6 +26,8 @@ No modules. | [aws_alb_listener.http](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/alb_listener) | resource | | [aws_alb_listener_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/alb_listener_rule) | resource | | [aws_alb_target_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/alb_target_group) | resource | +| [aws_appautoscaling_policy.ecs_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource | +| [aws_appautoscaling_target.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource | | [aws_appmesh_mesh.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appmesh_mesh) | resource | | [aws_appmesh_virtual_node.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appmesh_virtual_node) | resource | | [aws_cloudwatch_log_group.ecs_cloudwatch_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | diff --git a/terraform/modules/ecs/_local.tf b/terraform/modules/ecs/_local.tf index ab92c98..6d2c78d 100644 --- a/terraform/modules/ecs/_local.tf +++ b/terraform/modules/ecs/_local.tf @@ -14,7 +14,8 @@ locals { short_name = "ecrv", fargate_cpu = 1024, fargate_memory = 2048, - app_count = 1 + min_capacity = 1 + max_capacity = 5 app_image = var.disable_ecr == false ? "${terraform.workspace}-ecr-viewer" : "ecr-viewer", app_version = var.phdi_version, container_port = 3000, @@ -60,7 +61,8 @@ locals { short_name = "fhirc", fargate_cpu = 1024, fargate_memory = 2048, - app_count = 1 + min_capacity = 1 + max_capacity = 5 app_image = var.disable_ecr == false ? "${terraform.workspace}-fhir-converter" : "fhir-converter", app_version = var.phdi_version, container_port = 8080, @@ -73,7 +75,8 @@ locals { short_name = "inge", fargate_cpu = 1024, fargate_memory = 2048, - app_count = 1 + min_capacity = 1 + max_capacity = 5 app_image = var.disable_ecr == false ? "${terraform.workspace}-ingestion" : "ingestion", app_version = var.phdi_version, container_port = 8080, @@ -86,7 +89,8 @@ locals { short_name = "vali", fargate_cpu = 1024, fargate_memory = 2048, - app_count = 1 + min_capacity = 1 + max_capacity = 5 app_image = var.disable_ecr == false ? "${terraform.workspace}-validation" : "validation", app_version = var.phdi_version, container_port = 8080, @@ -99,7 +103,8 @@ locals { short_name = "trigcr", fargate_cpu = 1024, fargate_memory = 2048, - app_count = 1 + min_capacity = 1 + max_capacity = 5 app_image = var.disable_ecr == false ? "${terraform.workspace}-trigger-code-reference" : "trigger-code-reference", app_version = var.phdi_version, container_port = 8080, @@ -112,7 +117,8 @@ locals { short_name = "msgp", fargate_cpu = 1024, fargate_memory = 2048, - app_count = 1 + min_capacity = 1 + max_capacity = 5 app_image = var.disable_ecr == false ? "${terraform.workspace}-message-parser" : "message-parser", app_version = var.phdi_version, container_port = 8080, @@ -125,7 +131,8 @@ locals { short_name = "orch", fargate_cpu = 1024, fargate_memory = 2048, - app_count = 1 + min_capacity = 1 + max_capacity = 5 app_image = var.disable_ecr == false ? "${terraform.workspace}-orchestration" : "orchestration", app_version = var.phdi_version, container_port = 8080, diff --git a/terraform/modules/ecs/ecs.tf b/terraform/modules/ecs/ecs.tf index a09785f..2f955ab 100644 --- a/terraform/modules/ecs/ecs.tf +++ b/terraform/modules/ecs/ecs.tf @@ -43,7 +43,7 @@ resource "aws_ecs_service" "this" { name = each.key cluster = aws_ecs_cluster.dibbs_app_cluster.id task_definition = each.value.arn - desired_count = local.service_data[each.key].app_count + desired_count = local.service_data[each.key].min_capacity launch_type = "FARGATE" scheduling_strategy = "REPLICA" @@ -108,5 +108,46 @@ resource "aws_ecs_service" "this" { } } } + + lifecycle { + ignore_changes = [desired_count] + } + tags = local.tags } + + +resource "aws_appautoscaling_target" "this" { + for_each = aws_ecs_service.this + max_capacity = local.service_data[each.key].max_capacity + min_capacity = local.service_data[each.key].min_capacity + resource_id = each.value.id + scalable_dimension = "ecs:service:DesiredCount" + service_namespace = "ecs" +} + +resource "aws_appautoscaling_policy" "ecs_policy" { + for_each = aws_appautoscaling_target.this + name = "${local.service_data[each.key].short_name}-scaling" + policy_type = "StepScaling" + resource_id = each.value.resource_id + scalable_dimension = each.value.scalable_dimension + service_namespace = each.value.service_namespace + + step_scaling_policy_configuration { + adjustment_type = "ChangeInCapacity" + cooldown = 60 + metric_aggregation_type = "Maximum" + + step_adjustment { + metric_interval_lower_bound = 1.0 + metric_interval_upper_bound = 2.0 + scaling_adjustment = -1 + } + step_adjustment { + metric_interval_lower_bound = 2.0 + metric_interval_upper_bound = 3.0 + scaling_adjustment = 1 + } + } +}