Skip to content

Resource Based Autoscaling with ECS

Introduction

In order to save costs services should look to implement resource based autoscaling on ECS tasks to reduce the risk of service outages and performance issues. This playbook outlines the process for implementing it.

Procedure

To add resource based autoscaling to your ecs tasks, follow these steps:

Scheduled on

Remember this should be tested on non-prod environments before implementation in production.

  • In your autoscaling.tf (assuming you have already created one for implementing it) and add the following:

For all of the below the target_value, scale_in_cooldown and scale_out_cooldown values can variabilised if you desire.

To Add autoscaling based on CPU

/* AUTOSCALING CPU BASED POLICY */

resource "aws_appautoscaling_scheduled_action" "on" {
  count = var.scheduled_auto_scaling ? 1 : 0
  name                = "service-on"
  service_namespace  = "ecs"
  resource_id        = aws_appautoscaling_target.ecs_as_target.resource_id
  scalable_dimension = "ecs:service:DesiredCount"
  schedule           = "cron(0 7 * * ? *)"
  timezone           = "Europe/London"
  scalable_target_action {
    min_capacity = var.ecs_desired_count
    max_capacity = var.ecs_max_count
  }
}

To Add autoscaling based on Memory

/* AUTOSCALING MEMORY BASED POLICY */

resource "aws_appautoscaling_policy" "ecs_as_mem_policy" {
  name               = "${var.service_name}-${var.environment}-autoscaling-policy-02"
  policy_type        = "TargetTrackingScaling"
  resource_id        = aws_appautoscaling_target.ecs_as_target.resource_id
  scalable_dimension = aws_appautoscaling_target.ecs_as_target.scalable_dimension
  service_namespace  = aws_appautoscaling_target.ecs_as_target.service_namespace
  target_tracking_scaling_policy_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ECSServiceAverageMemoryUtilization"
    }
    target_value       = 70
    scale_in_cooldown  = 60
    scale_out_cooldown = 15
  }
}

To add autoscaling based on ALB requests

/* AUTOSCALING ALB REQUEST BASED POLICY */

resource "aws_appautoscaling_policy" "ecs_as_alb_request_policy" {
  name               = "${var.service_name}-${var.environment}-autoscaling-policy-03"
  policy_type        = "TargetTrackingScaling"
  resource_id        = aws_appautoscaling_target.ecs_as_target.resource_id
  scalable_dimension = aws_appautoscaling_target.ecs_as_target.scalable_dimension
  service_namespace  = aws_appautoscaling_target.ecs_as_target.service_namespace
  target_tracking_scaling_policy_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ALBRequestCountPerTarget"
      resource_label = "${data.aws_lb.pdf_back_alb.arn_suffix}/${data.aws_lb_target_group.pdf_back_lb_tg.arn_suffix}"
    }
    target_value       = 150
    scale_in_cooldown  = 60
    scale_out_cooldown = 15
  }
}

Applying

  • Run your full pipeline for the service component and your autoscaling will be deployed.