Skip to content

Commit

Permalink
add autoscaling to the ecs services
Browse files Browse the repository at this point in the history
  • Loading branch information
alismx committed Oct 17, 2024
1 parent 27d72e8 commit 80ae92e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
2 changes: 2 additions & 0 deletions terraform/modules/ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
21 changes: 14 additions & 7 deletions terraform/modules/ecs/_local.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
43 changes: 42 additions & 1 deletion terraform/modules/ecs/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 80ae92e

Please sign in to comment.