1 Answer
- Newest
- Most votes
- Most comments
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 a year ago
Relevant content
- asked 7 months ago
- asked 5 months ago
- Accepted Answerasked 3 years ago
- Accepted Answerasked a year ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years 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 ..... ...