0
0
AwsHow-ToBeginner · 4 min read

How to Use Auto Scaling in ECS for Dynamic Container Management

To use auto scaling in AWS ECS, configure an Auto Scaling Group for your ECS cluster instances and set up Service Auto Scaling policies that adjust the number of running tasks based on metrics like CPU or memory usage. This ensures your containerized applications scale automatically to meet demand without manual intervention.
📐

Syntax

Auto scaling in ECS involves two main parts: the Auto Scaling Group (ASG) for EC2 instances and the Service Auto Scaling for ECS tasks. The ASG manages the number of EC2 instances, while Service Auto Scaling adjusts the number of running tasks.

  • Auto Scaling Group: Defines minimum, maximum, and desired EC2 instances.
  • ECS Service Auto Scaling: Uses scaling policies triggered by CloudWatch alarms.
  • Scaling Policies: Define when and how to scale based on metrics.
terraform
resource "aws_autoscaling_group" "ecs_instances" {
  name_prefix          = "ecs-asg-"
  max_size             = 5
  min_size             = 1
  desired_capacity     = 2
  launch_configuration = aws_launch_configuration.ecs_launch_config.id
  vpc_zone_identifier  = ["subnet-12345678"]
  tag {
    key                 = "Name"
    value               = "ecs-instance"
    propagate_at_launch = true
  }
}

resource "aws_appautoscaling_target" "ecs_service_target" {
  max_capacity       = 10
  min_capacity       = 1
  resource_id        = "service/cluster-name/service-name"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"
}

resource "aws_appautoscaling_policy" "cpu_scaling_policy" {
  name               = "cpu-scaling-policy"
  policy_type        = "TargetTrackingScaling"
  resource_id        = aws_appautoscaling_target.ecs_service_target.resource_id
  scalable_dimension = aws_appautoscaling_target.ecs_service_target.scalable_dimension
  service_namespace  = aws_appautoscaling_target.ecs_service_target.service_namespace

  target_tracking_scaling_policy_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ECSServiceAverageCPUUtilization"
    }
    target_value = 50.0
  }
}
💻

Example

This example shows how to set up an ECS cluster with an Auto Scaling Group for EC2 instances and a Service Auto Scaling policy that scales tasks based on CPU usage. The scaling policy keeps average CPU utilization around 50%, increasing or decreasing tasks automatically.

terraform
provider "aws" {
  region = "us-east-1"
}

resource "aws_ecs_cluster" "ecs_cluster" {
  name = "ecs-cluster"
}

resource "aws_launch_configuration" "ecs_launch_config" {
  name_prefix   = "ecs-launch-config-"
  image_id      = "ami-0c55b159cbfafe1f0"  # Amazon ECS-optimized AMI
  instance_type = "t3.micro"
  iam_instance_profile = aws_iam_instance_profile.ecs_instance_profile.name
  security_groups = [aws_security_group.ecs_sg.id]
  user_data = <<-EOF
              #!/bin/bash
              echo ECS_CLUSTER=ecs-cluster >> /etc/ecs/ecs.config
              EOF
}

resource "aws_autoscaling_group" "ecs_instances" {
  name_prefix          = "ecs-asg-"
  max_size             = 3
  min_size             = 1
  desired_capacity     = 2
  launch_configuration = aws_launch_configuration.ecs_launch_config.id
  vpc_zone_identifier  = ["subnet-abc12345"]
  tag {
    key                 = "Name"
    value               = "ecs-instance"
    propagate_at_launch = true
  }
}

resource "aws_ecs_service" "example_service" {
  name            = "example-service"
  cluster         = aws_ecs_cluster.ecs_cluster.id
  task_definition = aws_ecs_task_definition.example_task.arn
  desired_count   = 1
  launch_type     = "EC2"
}

resource "aws_appautoscaling_target" "ecs_service_target" {
  max_capacity       = 5
  min_capacity       = 1
  resource_id        = "service/${aws_ecs_cluster.ecs_cluster.name}/${aws_ecs_service.example_service.name}"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"
}

resource "aws_appautoscaling_policy" "cpu_scaling_policy" {
  name               = "cpu-scaling-policy"
  policy_type        = "TargetTrackingScaling"
  resource_id        = aws_appautoscaling_target.ecs_service_target.resource_id
  scalable_dimension = aws_appautoscaling_target.ecs_service_target.scalable_dimension
  service_namespace  = aws_appautoscaling_target.ecs_service_target.service_namespace

  target_tracking_scaling_policy_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ECSServiceAverageCPUUtilization"
    }
    target_value = 50.0
  }
}
Output
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
⚠️

Common Pitfalls

Common mistakes when using auto scaling in ECS include:

  • Not setting correct min and max values in Auto Scaling Groups, causing too few or too many instances.
  • Forgetting to link the ECS cluster name in the EC2 instance user data, so instances don't join the cluster.
  • Using incorrect resource IDs in scaling targets, which prevents scaling policies from working.
  • Not configuring CloudWatch alarms or using inappropriate metrics, leading to no scaling actions.
  • Ignoring cooldown periods, causing rapid scaling up and down (thrashing).

Always verify your scaling policies and test them with load to ensure they behave as expected.

bash
/* Wrong: Missing ECS cluster name in user data */
user_data = "#!/bin/bash\necho ECS_CLUSTER= >> /etc/ecs/ecs.config"

/* Right: Correct ECS cluster name set */
user_data = "#!/bin/bash\necho ECS_CLUSTER=ecs-cluster >> /etc/ecs/ecs.config"
📊

Quick Reference

  • Auto Scaling Group (ASG): Manages EC2 instances for ECS cluster.
  • Service Auto Scaling: Adjusts ECS task count based on metrics.
  • Scaling Policies: Use target tracking with CPU or memory metrics.
  • CloudWatch Alarms: Trigger scaling actions.
  • User Data: Set ECS_CLUSTER name for instances to join cluster.

Key Takeaways

Set up an Auto Scaling Group to manage ECS cluster EC2 instances with proper min and max sizes.
Use ECS Service Auto Scaling with target tracking policies to scale tasks automatically based on CPU or memory.
Ensure EC2 instances join the ECS cluster by setting the ECS_CLUSTER name in user data.
Configure CloudWatch alarms and scaling policies carefully to avoid scaling errors or thrashing.
Test scaling behavior under load to confirm your auto scaling setup works as expected.