How can I use Math for CustomizedMetricSpecification using application-autoscaling for ECS

0

Hi guys,

I've found that is possible to use math in a target tracking scaling policy for Amazon EC2 Auto Scaling (https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-target-tracking-metric-math.html)

I've tried to use same config in application auto-scaling for service-namespace ECS without success

This is my config

{
    "CustomizedMetricSpecification": {
        "Metrics": [
            {
                "Label": "ack_total",
                "Id": "m1",
                "MetricStat": {
                    "Metric": {
                        "MetricName": "rabbitmq_queue_messages_ack_total[switch_events]",
                        "Namespace": "Prometheus"
                    },
                    "Stat": "Average"
                },
                "ReturnData": false
            },
            {
                "Label": "published_total",
                "Id": "m2",
                "MetricStat": {
                    "Metric": {
                        "MetricName": "rabbitmq_queue_messages_published_total[switch_events]",
                        "Namespace": "Prometheus"
                    },
                    "Stat": "Average"
                },
                "ReturnData": false
            },
            {
                "Label": "Relation (ack_total + 1) / (published_total + 1)",
                "Id": "e1",
                "Expression": "(m1 + 1)/(m2 + 1)",
                "ReturnData": true
            }
        ]
    },
    "TargetValue": 1.0
}

This is the command I've used:

aws application-autoscaling put-scaling-policy --service-namespace ecs --policy-name rabbitmq-pub-ack-scaling-policy --scalable-dimension ecs:service:DesiredCount --resource-id "service/XXXXX/events"  --policy-type TargetTrackingScaling --target-tracking-scaling-policy-configuration file://alarm-definition.json

I'm getting this error:

Parameter validation failed:
Missing required parameter in TargetTrackingScalingPolicyConfiguration.CustomizedMetricSpecification: "MetricName"
Missing required parameter in TargetTrackingScalingPolicyConfiguration.CustomizedMetricSpecification: "Namespace"
Missing required parameter in TargetTrackingScalingPolicyConfiguration.CustomizedMetricSpecification: "Statistic"
Unknown parameter in TargetTrackingScalingPolicyConfiguration.CustomizedMetricSpecification: "Metrics", must be one of: MetricName, Namespace, Dimensions, Statistic, Unit

based on doc available in https://docs.aws.amazon.com/autoscaling/application/APIReference/API_CustomizedMetricSpecification.html seems impossible to use a custom metric with math

Best regards.

4 Answers
0
Accepted Answer

Hello,

Metric math support for EC2 AutoScaling (ASG) target tracking policies was just recently launched: https://aws.amazon.com/about-aws/whats-new/2022/12/amazon-ec2-auto-scaling-supports-metric-math-target-tracking-policies/

While there are currently plans to add this to Application AutoScaling in the future, its not currently supported and we're not able to comment on if/when the feature will be released. In the meantime, for your usecase you would need to either:

  1. create a custom metric in cloudwatch which represents the expression, and base the target tracking policy on that single metric
  2. Use StepScaling with the math expression
AWS
answered a year ago
  • Thank you, that was the question I had and you managed to clear it up for me. I was wondering if there was something missing from the docs or this feature was just not implemented. I needed to resolve this question before doing something custom. Now I'm going to customize it as you suggested.

0

You can use math expressions in the CustomizedMetricSpecification parameter of the RegisterScalableTarget action of the Application Auto Scaling service to specify the metric to scale on and the dimensions for the metric. Here is an example of how to use math expressions in the CustomizedMetricSpecification parameter:

CustomizedMetricSpecification: MetricName: "mymetric" Namespace: "mynamespace" Dimensions: - Name: "InstanceId" Value: !Ref MyEC2Instance Statistic: "Average" Unit: "Count" MetricScale: 1 MetricType: "Custom" ResourceLabel: !Sub "${ECS_CLUSTER_NAME}/task/${TASK_DEFINITION_NAME}"

In this example, the MetricScale parameter specifies a scale factor to apply to the metric. The ResourceLabel parameter uses the !Sub intrinsic function to substitute variables in a string template with their actual values.

You can use other intrinsic functions, such as !If, !Join, and !Ref, in the CustomizedMetricSpecification parameter to customize the metric specification.

SeanSi
answered a year ago
  • Could you reformat the example?

0

@Shahad_C the docs imply support for math expression with EC2 ASG Target Tracking. But I couldn't find any way to use this with "CustomizedMetricSpecification" (i'm getting the same error as mentioned here). The only thing I did find, is the support for math expression with "Advanced predictive scaling policy" (e.g. PredictiveScaling) but not for TargetTrackingScaling.

Are the docs wrong? can you share an example of an TargetTrackingScaling policy using an alarm based on math expression? Thanks!

stam
answered a year ago
  • Hi @stam - this is a very new feature in EC2 AutoScaling, so my guess is your CLI version is out of date and doesn't support these new parameters yet if your error message is similar to the one shared in this post. Try updating the CLI and seeing if you still get the same errors

0

I was having similar issues. I tried both with the AWS CLI and boto3. Then I noticed a footnote in https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-target-tracking-metric-math.html that said, "If this command throws an error, make sure that you have updated the AWS CLI locally to the latest version.". I updated to the latest version of the AWS CLI v2, and it worked!

Kc F
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions