This project provides an automated solution for balancing ECS (Elastic Container Service) instances in AWS. It helps optimize resource utilization by monitoring and rebalancing instances when necessary.
The application uses a config.json file with the following structure:
{
"region": "<region>",
"targetClusterArn": "arn:aws:ecs:<region>:<user-id>:cluster/<cluster-name>",
"capacityProvider": "EC2",
"autoScalingGroupName": "<asg-name>"
}region: AWS region where your ECS cluster is locatedtargetClusterArn: ARN of the ECS cluster to monitorcapacityProvider: Type of capacity provider (EC2 or FARGATE)autoScalingGroupName: Name of the Auto Scaling Group associated with the cluster
The balancer follows these steps to optimize instance distribution:
- Instance Discovery: Retrieves all ECS instances in the target cluster
- Task Analysis: Gathers information about all running tasks
- Resource Calculation:
- Calculates resource usage for each instance
- Determines if rebalancing is needed based on resource utilization
- Instance Termination: If rebalancing is required, terminates the target instance
- The Auto Scaling Group will automatically launch a new instance
- ECS will redistribute tasks to the new instance
pnpm install- Copy
config.json.exampletoconfig.json:
cp config.json.example config.json- Edit
config.jsonwith your AWS ECS cluster details:
{
"region": "your-aws-region",
"targetClusterArn": "your-ecs-cluster-arn",
"capacityProvider": "EC2",
"autoScalingGroupName": "your-asg-name"
}- Start the balancer:
pnpm startThis project is written in TypeScript and uses:
- AWS SDK for interacting with ECS
- Node.js runtime environment
- pnpm as the package manager
MIT