0

I'm trying to autoscale Fargate Service in AWS ECS based on CloudWatch Alarm (Namespace - AWS/SQS, Metric name - ApproximateNumberOfMessagesVisible). I managed to do that in the AWS Console but not via code (in Pulumi).

My suggestion for code is below:

const vpc = new awsx.ec2.Vpc("vpc", {
  subnets: [{ type: "public" }],
});

const my_cluster = new awsx.ecs.Cluster("my-cluster", { vpc });

const task = new awsx.ecs.FargateTaskDefinition(...)

const my_fargateService = new awsx.ecs.FargateService(
  "my-fargateService",
  {
    cluster: my_cluster,
    taskDefinition: task,
    desiredCount: 0,
  }
);

const sqs_queue = new aws.sqs.Queue(
  "fargateServiceQueque"
);
const autoscaling_Policy1 = new aws.autoscaling.Policy("autoscaling_Policy1", {
  policyType: "StepScaling",
  adjustmentType: "ExactCapacity",
  stepAdjustments: [{
    metricIntervalUpperBound: 1,
    scalingAdjustment: 0
  }],
  // I think problem here
  autoscalingGroupName: autoScalingGroupFromFargateService.name // ???
});
const sqs_less_than_1 = new aws.cloudwatch.MetricAlarm("sqs_less_than_1_message_visible", {
  comparisonOperator: "LessThanThreshold",
  evaluationPeriods: "2",
  metricName: "approximateNumberOfMessagesVisible",
  namespace: "AWS/SQS",
  period: "120",
  statistic: "Maximum",
  threshold: "1",
  dimensions: {
      QueueName: sqs_queue.name,
  },
  alarmDescription: "This metric monitors number of messages in sqs queue",
  alarmActions: [] // I think it should be -> [autoscaling_Policy1.arn],
});

The problem - I don't know how to retrieve auto-scaling group ("autoScalingGroupFromFargateService.name") from my_fargateService.

In AWS Console I do that as shown on images below: enter image description here enter image description here

And I'm getting this result: enter image description here

SAndriy
  • 670
  • 2
  • 15
  • 25
  • I think Fargate doesn't have a feature to trigger the scaling according to the SQS queue message. I can only see the option with CPU and Memory metrics in both scaling (Target tracking and Step Scaling). If you have figure out a way please let me know as well. Thanks – pawan19 Jun 15 '21 at 14:45
  • I was able to scale down and up based on the number of messages in SQS. The question was about that - I was not able to write all my code in Pulumi and some configuration was done manually in the console. – SAndriy Jun 15 '21 at 16:32
  • Cool, To get the SQS based scaling you set the Alarm first from the Cloudwatch then used the same existing in the scaling, is that right? – pawan19 Jun 16 '21 at 04:42
  • Excuse me for the late reply. Yes, I created - two metric alarms and used them to create autoscaling for service in ECS. One metric was used to scale down to 0 Fargate task and another to scale up to 2 Fargate tasks. I used the "Step scaling" type of dynamic scaling. – SAndriy Jun 26 '21 at 15:35

1 Answers1

0

To set up autoscaling for ECS Fargate you can use Pulumi aws.appautoscaling(docs).

Tymofii
  • 1
  • 1