
Resources:
  AgentScaleUpPolicy:
    Type: AWS::AutoScaling::ScalingPolicy
    Properties:
      AdjustmentType: ChangeInCapacity
      AutoScalingGroupName: $(AgentAutoScaleGroup)
      Cooldown: 300
      ScalingAdjustment : $(ScaleOutAdjustment)

  AgentScaleDownPolicy:
    Type : AWS::AutoScaling::ScalingPolicy
    Properties:
      AdjustmentType: ChangeInCapacity
      AutoScalingGroupName: $(AgentAutoScaleGroup)
      Cooldown : 600
      ScalingAdjustment: $(ScaleInAdjustment)

  ScheduledJobsAlarmHigh:
   Type: AWS::CloudWatch::Alarm
   Properties:
      AlarmDescription: Scale-up if ScheduledJobs > 0 for 1 minute
      MetricName: ScheduledJobsCount
      Namespace: Buildkite
      Statistic: Average
      Period: 60
      EvaluationPeriods: 1
      Threshold: 0
      AlarmActions: [ $(AgentScaleUpPolicy) ]
      Dimensions:
        - Name: Queue
          Value: $(BuildkiteQueue)
      ComparisonOperator: GreaterThanThreshold

  ScheduledJobsAlarmLow:
   Type: AWS::CloudWatch::Alarm
   Properties:
      AlarmDescription: Scale-down if ScheduledJobsCount == 0 for 5 minutes
      MetricName: ScheduledJobsCount
      Namespace: Buildkite
      Statistic: Maximum
      Period: 300
      EvaluationPeriods: 1
      Threshold: 0
      AlarmActions: [ $(AgentScaleDownPolicy) ]
      Dimensions:
        - Name: Queue
          Value: $(BuildkiteQueue)
      ComparisonOperator: LessThanOrEqualToThreshold


Resources:
  MissingMetricsAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      MetricName: ScheduledJobsCount
      Namespace: Buildkite
      Statistic: SampleCount
      Period: 300
      EvaluationPeriods: 1
      Threshold: 0
      Dimensions:
        - Name: Queue
          Value: $(BuildkiteQueue)
      ComparisonOperator: LessThanOrEqualToThreshold

  AgentScaleUpPolicy:
    Type: AWS::AutoScaling::ScalingPolicy
    Properties:
      AdjustmentType: ChangeInCapacity
      AutoScalingGroupName: $(AgentAutoScaleGroup)
      Cooldown: 300
      ScalingAdjustment : $(ScaleOutAdjustment)

  AgentScaleDownPolicy:
    Type : AWS::AutoScaling::ScalingPolicy
    Properties:
      AdjustmentType: ChangeInCapacity
      AutoScalingGroupName: $(AgentAutoScaleGroup)
      Cooldown : 600
      ScalingAdjustment: $(ScaleInAdjustment)

  ScheduledJobsAlarmHigh:
   Type: AWS::CloudWatch::Alarm
   Properties:
      AlarmDescription: Scale-up if ScheduledJobs > 0 for 1 minute
      MetricName: ScheduledJobsCount
      Namespace: Buildkite
      Statistic: Average
      Period: 60
      EvaluationPeriods: 1
      Threshold: 0
      AlarmActions: [ $(AgentScaleUpPolicy) ]
      Dimensions:
        - Name: Queue
          Value: $(BuildkiteQueue)
      ComparisonOperator: GreaterThanThreshold

  ScheduledJobsAlarmLow:
   Type: AWS::CloudWatch::Alarm
   Properties:
      AlarmDescription: Scale-down if ScheduledJobsCount == 0 for 60 minutes
      MetricName: ScheduledJobsCount
      Namespace: Buildkite
      Statistic: Maximum
      Period: 1200
      EvaluationPeriods: 3
      Threshold: 0
      AlarmActions: [ $(AgentScaleDownPolicy) ]
      Dimensions:
        - Name: Queue
          Value: $(BuildkiteQueue)
      ComparisonOperator: LessThanOrEqualToThreshold

Add a MissingMetricsAlarm and make scale down happen over 120 mins
Add a MissingMetricsAlarm and make scale down happen over 120 mins
