autoscale tasks in fargate based on metrics

0

Hi team,

I have a Node.js application deployed within an AWS Fargate cluster.

This application launches multiple SQS consumers responsible for retrieving and processing messages from an SQS queue.

all the infrastructure cluster, service,.... are defined with AWS CDK

I don't have a load balancer on my infrastructure, only consumers that continuously poll an SQS queue, process the messages and subsequently send them to another SQS queue.

I would like to implement an autoscaling mechanism for the Fargate service based on the message count in the input SQS queue. For example, if the queue's message count reaches 1000, the service scales horizontally and an additional task is automatically created.

but according to the console UI the only ECS service metrics available are :

  • ECSServiceAverageCPUUtilization
  • ECSServiceMemoryUtilization
  • ALBRequestCounterPerTarget (I don't have ALB)

Can I implement auto-scaling for my Fargate service based on the message count in the SQS queue, considering that I'm using AWS CDK to deploy my infrastructure? If so, please what are the steps to set up this scaling logic?

i saw this https://github.com/aws/aws-cdk/issues/20706

but it seems there is a bug on the autoscaling

I added this to my CDk code :

const service = new FargateService(this, "myFargateSvc", {
      cluster,
      minHealthyPercent: 0, 
      desiredCount: 2,
      taskDefinition: myTaskDef,
      securityGroups: [
        new ec2.SecurityGroup(this, "mySG", {
          securityGroupName: "ecsTask_SG",
          vpc: cluster.vpc,
        }),
      ],
    }); 

// ===> configure autoscaling at the container based on SQS queue message count
    const scalingTarget = service.autoScaleTaskCount({
      minCapacity: 2,
      maxCapacity: 10,
    });

    scalingTarget.scaleOnMetric("ScalingBasedOnVisibleMessagesOnTheQueue", {
      metric: virusScanQueue.metricApproximateNumberOfMessagesVisible(),
      scalingSteps: [
        { upper: 0, change: 0 },
        { lower: 1, change: +1 },
      ],
    });

==> Is this the correct code for my above request, Does this avoid this bug: https://github.com/aws/aws-cdk/issues/20706 How can I specify the number of messages to kick off the scaling (i.e.: scale out when 1000 messages are in the queue)?

how can I scale-in

One other question please, My application running in Fargate throws Errors throw new Error(exceptions....)

those error are crucial and needs to be monitored and inspected as they arrive, What would be the best way to receive notifications in case of an error thrown from the application running in Fargate?

Thank you for you help

1 Answer
0

Here's some sample code that addresses this use case. Create a custom metric using the SQS ApproximateNumberOfMessagesVisible metric to set up alarms. This alarm is used to trigger the ECS service autoscaling policy (using step scaling in in the sample code).

const service = new ecs.FargateService(this, "myFargateSvc", {
  cluster: props.cluster,
  taskDefinition: taskDef,
  desiredCount: 0,
  assignPublicIp: false,
  serviceName: 'example-service'
})

// Grant permissions to consume messages off queue
props.queue.grantConsumeMessages(service.taskDefinition.taskRole)

// Generating metric
let scalingMetric = new cw.MathExpression({
  expression: "messagesVisible",
  usingMetrics: {
    messagesVisible: props.queue.metricApproximateNumberOfMessagesVisible()   
  }
})

// Scaling based on Queue Depth
const scalingTarget = service.autoScaleTaskCount({ maxCapacity: 100, minCapacity: 0 });
scalingTarget.scaleOnMetric('QueueMessagesScaling', {
  metric: scalingMetric,
  scalingSteps: [
    { lower: 0, upper: 0, change: -10 }, 
    { lower: 1, upper: 10, change: +1 }, 
    { lower: 10, change: +5 },
    { lower: 30, change: +10 },
    { lower: 50, change: +50 }],
  cooldown: Duration.minutes(5)
})
answered 8 months ago
  • Thank you for your answer!

    if i want to scale by 1 task every new 50 messages on the queue how can I do that? change: +1 change: +1 change: +1 ...... or change: +1 change: +2 change: +4 change: +4 ..... ...

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