Search code examples
amazon-cloudwatchamazon-ecscloudwatch-alarms

Set the Cloudwatch Alarm High and Low thresholds for AWS Fargate


I have created an autoscaling target and a policy which is attached to it.

AutoScalingPolicy:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: !Join ['', [!Ref ServiceName, auto-scaling-policy]]
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref AutoScalingTarget
      TargetTrackingScalingPolicyConfiguration:
        PredefinedMetricSpecification:
          PredefinedMetricType: ECSServiceAverageCPUUtilization
        ScaleInCooldown: 10
        ScaleOutCooldown: 10
        # Keep things at or lower than 50% CPU utilization, for example
        TargetValue: !Ref AutoScalingTargetValue

This is creating cloudwatch alarms as:

High: 3 datapoints within 3 minutes

Low: 15 datapoints within 15 minutes

I want to customize this to:

High: 1 datapoint within 1 minute

Low: 1 datapoint within 1 minute

I am able to do this manually from AWS console. However, struggling to find a way to do the same using cloudformation template.


Solution

  • Target tracking does not actually have a "high" and "low" threshold. With target tracking you set the target CPU utilization percent value you want to try to keep it at, and autoscaling automatically updates the number tasks up and down to try to keep the CPU utilization at that value.

    If you want to have more fine grained control, you'd need to use a "step scaling policy". This allows you to set specific values like "if CPU percent is between 0 and 10% over my target of 50% then increase by 1", and "if CPU percent is between 10% and 20% over my target of 50% then increase by 2".

    You can see an example of such a step scaling policy, in a downloadable open source CloudFormation template here: https://containersonaws.com/architecture/autoscaling-service-containers/ It is too to long include directly in this answer, but you can use these official AWS sample templates as a starting point for your step scaling policy.