Scaling your AWS ECS Cluster & Services with Simple Terraform Script

Introducing a new Terraform module designed to effortlessly scale your existing ECS Cluster and Services, with minimal parameter complexity.

Do Exploit
3 min readDec 11, 2023
Generated by stability-ai/stable-diffusion.

I started learning about Terraform when I was graduate. Simply put, Terraform helps automate setting up things like server and storage, mainly in the public cloud, like Amazon Web Services (AWS). It keeps track of changes using Git. Lately, I’ve been diving into AWS Elastic Container Service (ECS) — an another container orchestration tools like Kubernetes. Turns out, scaling AWS ECS Cluster and Services isn’t straight-forward, even when using the AWS Console. Trying to find an easy way to scale ECS, I looked for an existing Terraform module solution, but no luck so far.

Then, I started trying to scale with Terraform, without any existing module, it isn’t really easy because much of parameters and configuration needs to be included to match simple cases. These resources are used to scale AWS ECS:

Get Started with terraform-aws-simple-autoscaling

Once the Terraform module used, you only need to provide two simple things. If you want to scale the AWS ECS Service, just tell it the name of the cluster and the service. That’s all!

Scaling AWS ECS Service

module "ecs_service_basic_web" {
source = "michael-act/simple-autoscaling/aws//modules/ecs-service"

cluster_name = "ecs-cluster-exploration"
service_name = "ecs-service-basic-web"
}

Once you run terraform apply, it takes care of everything. Your services will automatically add a container if the memory usage hits 85% before it gets completely full. Also, if your services free up resources and the memory usage drops below 30%, it will remove a container for you.

Scaling AWS ECS Cluster

Let’s dive into scaling your AWS ECS Cluster, which is basically a bunch of servers (EC2 instances), usually managed by AWS Auto Scaling Group (ASG). These instances automatically join the cluster. By turning on scaling for the cluster, your cluster can respond when your ECS services need more memory resources, but the existing cluster not enough.

module "ecs_cluster_exploration" {
source = "michael-act/simple-autoscaling/aws//modules/ecs-cluster"

cluster_name = "ecs-cluster-exploration"
autoscaling_group_name = "asg-cluster-exploration"
}

Your group of EC2 instances can automatically add one more instance if the cluster's memory usage reaches 85%. Also, if the memory usage drops below 30%, it removes an instance.

Little Customization

If your cluster is more about CPU than memory, you can tweak it with a few extra settings. Take a look at this:

module "ecs_cluster_exploration" {
source = "michael-act/simple-autoscaling/aws//modules/ecs-cluster"

cluster_name = "ecs-cluster-exploration"
autoscaling_group_name = "asg-cluster-exploration"

metric = {
name = "CPUReservation"
statistic_type = "Maximum"
}
}

Feel free to explore other customizable options in variables.tf . There are more ways to tweak it to your needs!

Explore it live on https://registry.terraform.io/modules/michael-act/simple-autoscaling/aws/latest , and for those who are interested to contribute or use the original source code, see here https://github.com/michael-act/terraform-aws-simple-autoscaling/ .

--

--

Do Exploit

I share stories about what I've learned in the past and now. Let's connect to Instagram! @do.exploit