Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Commit

Permalink
fix: Add autoscaling to ECS cluster (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Basty authored Aug 10, 2023
1 parent d247ce2 commit 102064e
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion terraform/ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ resource "aws_ecs_service" "app_service" {
cluster = aws_ecs_cluster.app_cluster.id
task_definition = aws_ecs_task_definition.app_task_definition.arn
launch_type = "FARGATE"
desired_count = 3
desired_count = 1
propagate_tags = "TASK_DEFINITION"

# Wait for the service deployment to succeed
Expand All @@ -139,6 +139,55 @@ resource "aws_ecs_service" "app_service" {
}
}

# Autoscaling
# We can scale by
# ECSServiceAverageCPUUtilization, ECSServiceAverageMemoryUtilization, and ALBRequestCountPerTarget
# out of the box or use custom metrics
resource "aws_appautoscaling_target" "ecs_target" {
min_capacity = 1
max_capacity = 3
resource_id = "service/${aws_ecs_cluster.app_cluster.name}/${aws_ecs_service.app_service.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}

resource "aws_appautoscaling_policy" "cpu_scaling" {
name = "${module.this.id}-application-scaling-policy-cpu"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 30
scale_in_cooldown = 180
scale_out_cooldown = 90
}
depends_on = [aws_appautoscaling_target.ecs_target]
}

resource "aws_appautoscaling_policy" "memory_scaling" {
name = "${module.this.id}-application-scaling-policy-memory"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
target_value = 30
scale_in_cooldown = 180
scale_out_cooldown = 90
}
depends_on = [aws_appautoscaling_target.ecs_target]
}


# Load Balancers & Networking
resource "aws_lb" "application_load_balancer" {
name = module.this.id
Expand Down

0 comments on commit 102064e

Please sign in to comment.