23

For example, I want to trigger a CloudWatch alarm for the following:

Number of messages sent - Number of messages deleted >= 6

The above is for AWS SQS metrics. And I want that metric to trigger AWS Lambda if that alarm is set off.

Is there any way I can do it? If not, then is there any work-around [Aiming for a serverless paradigm]

[I want to do that, cause there's no metric which tells me the number of messages currently (available) in the queue]. So, if there's a metric which can give me that, it'd be very helpful

1
  • That's a good question, but most of the times, pure metrics are not enough to set alarms and intermediary quantities need to be built and exported to CloudWatch. e.g. maximum of a set of metrics, variation rate of a metric, delta to a goal value. Commented Mar 2, 2017 at 10:12

2 Answers 2

9

For the last overall goal, I would set the alarm on ApproximateNumberOfMessagesVisible from Cloudwatch documentation on SQS this is:

The number of messages available for retrieval from the queue.

Units: Count

Valid Statistics: Average, Minimum, Maximum, Sum, Data Samples (displays as Sample Count in the Amazon SQS console)


For the more generic computed metric question I would go with a lambda running priodically, gathering metrics from cloudwatch, doing the maths and pushing back to cloudwatch.

Here is an example lambda gathering data from Cloudwatch to push to elasticsearch. With the examples on the documentation you should be able to push metrics back from the lambda.

Be aware that calling cloudwatch API to gather and push metrics has a cost when you got over 1M request per month, there's other limits to be aware of to avoid breaking your budget/workflow.

2
  • Thank You (+1). I figured it out a few mins later :D . However, I wasn't sure about the term Approximate in it. Why approximate, when it can tell the exact number of messages available?
    – Dawny33
    Commented Mar 2, 2017 at 10:22
  • Because the metrics are pulled periodically, so they can't guarantee that between the collect time and presentation time there was no change in the queue.
    – Tensibai
    Commented Mar 2, 2017 at 10:34
9

Recently AWS just announced CloudWatch Metric Math which should be suitable for your use case.

References:-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.