Create new autoscaling for ecs cluster

Here is an example of how to create a new autoscaling policy for an ECS cluster using the AWS CLI:

Step 1: Create a scaling policy

Create a new scaling policy for your ECS cluster using the following command:

aws autoscaling create-scaling-policy --policy-name my-scaling-policy --service-namespace ecs --resource-id <cluster-name> --scalable-dimension ecs:service:DesiredCount --adjustment-type ChangeInCapacity --adjustment-value 1 --cooldown-period 300

Replace <cluster-name> with the name of your ECS cluster.

Step 2: Create a scaling action

Create a new scaling action for your ECS cluster using the following command:

aws autoscaling put-scaling-action --policy-name my-scaling-policy --scaling-action-name my-scaling-action --service-namespace ecs --resource-id <cluster-name> --scalable-dimension ecs:service:DesiredCount --adjustment-type ChangeInCapacity --adjustment-value 1 --cooldown-period 300

Step 3: Create an autoscaling group

Create a new autoscaling group for your ECS cluster using the following command:

aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg --launch-configuration-name my-launch-config --min-size 1 --max-size 10 --desired-capacity 2 --service-namespace ecs --resource-id <cluster-name> --scalable-dimension ecs:service:DesiredCount --adjustment-type ChangeInCapacity --adjustment-value 1 --cooldown-period 300

Replace my-launch-config with the name of your launch configuration.

Step 4: Attach the scaling policy to the autoscaling group

Attach the scaling policy to the autoscaling group using the following command:

aws autoscaling put-scaling-policy-configuration --auto-scaling-group-name my-asg --policy-name my-scaling-policy --service-namespace ecs --resource-id <cluster-name> --scalable-dimension ecs:service:DesiredCount --adjustment-type ChangeInCapacity --adjustment-value 1 --cooldown-period 300

Step 5: Configure the scaling policy

Configure the scaling policy to scale based on CPU utilization using the following command:

aws cloudwatch put-metric-alarm --alarm-name my-alarm --metric-name CPUUtilization --namespace AWS/ECS --statistic Average --period 300 --evaluation-periods 2 --threshold 50 --comparison-operator GreaterThanOrEqualToThreshold

This will create an alarm that triggers when the CPU utilization of your ECS cluster exceeds 50%.

Step 6: Attach the alarm to the scaling policy

Attach the alarm to the scaling policy using the following command:

aws autoscaling put-scaling-policy-configuration --auto-scaling-group-name my-asg --policy-name my-scaling-policy --service-namespace ecs --resource-id <cluster-name> --scalable-dimension ecs:service:DesiredCount --adjustment-type ChangeInCapacity --adjustment-value 1 --cooldown-period 300 --alarm-name my-alarm

This will configure the scaling policy to scale based on the CPU utilization alarm.

That's it! Your new autoscaling policy should now be created and attached to your ECS cluster.