<p>Recently AWS just announced <strong>CloudWatch Metric Math</strong> which should be suitable for your use case.</p>

<p><em>References:-</em></p>

<ul>
<li><a href="https://aws.amazon.com/about-aws/whats-new/2018/04/amazon-cloudwatch-adds-metric-math-to-enable-custom-operations-on-metrics/" rel="noreferrer">Amazon CloudWatch Adds Metric Math to Enable Custom Operations on Metrics</a></li>
<li><a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create-alarm-on-metric-math-expression.html" rel="noreferrer">Create a CloudWatch Alarm Based on a Metric Math Expression</a></li>
</ul>

CC BY-SA 4.0
2018-11-29T10:36:18.983Z
2018-11-29T10:36:18.983Z
onelaview
<p>For the last overall goal, I would set the alarm on <code>ApproximateNumberOfMessagesVisible</code> from Cloudwatch <a href="http://docs.aws.amazon.com/fr_fr/AmazonCloudWatch/latest/monitoring/sqs-metricscollected.html" rel="noreferrer">documentation on SQS</a> this is:</p>

<blockquote>
  <p>The number of messages available for retrieval from the queue.</p>
  
  <p>Units: Count</p>
  
  <p>Valid Statistics: Average, Minimum, Maximum, Sum, Data Samples
  (displays as Sample Count in the Amazon SQS console)</p>
</blockquote>

<hr>

<p>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.</p>

<p><a href="https://gist.github.com/fbrnc/5739f8bec042ac3326ad" rel="noreferrer">Here</a> is an example lambda gathering data from Cloudwatch to push to elasticsearch. With the <a href="http://docs.aws.amazon.com/fr_fr/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#publishingDataPoints" rel="noreferrer">examples</a> on the documentation you should be able to push metrics back from the lambda.</p>

<p>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 <a href="http://docs.aws.amazon.com/fr_fr/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html" rel="noreferrer">limits</a> to be aware of to avoid breaking your budget/workflow.</p>

CC BY-SA 3.0
2017-03-02T10:22:47.930Z
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
CC BY-SA 3.0
2017-03-02T10:34:30.377Z
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
CC BY-SA 3.0
2017-03-02T10:00:36.450Z
2017-03-02T10:00:36.450Z
Tensibai
<p>For example, I want to trigger a CloudWatch alarm for the following:</p>

<p><code>Number of messages sent</code> - <code>Number of messages deleted</code> >= 6</p>

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

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

<p><em>[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</em></p>

CC BY-SA 3.0
2017-03-02T10:12:22.210Z
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.
Michaël Le Barbier
<amazon-web-services><amazon-sqs><amazon-cloudwatch>
Is it possible to set CloudWatch alarm for a combination of metrics?
Dawny33
# Is it possible to set CloudWatch alarm for a combination of metrics?

(Asked by: username_0 on 2017-03-02)

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*

**Tags:** amazon-web-services, amazon-sqs, amazon-cloudwatch

> **Comments:**
> **username_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.  

---

## 2 answers

### Answer by username_3 on 2017-03-02. Score: 9 | Accepted answer

For the last overall goal, I would set the alarm on `ApproximateNumberOfMessagesVisible` from Cloudwatch [documentation on SQS](http://docs.aws.amazon.com/fr_fr/AmazonCloudWatch/latest/monitoring/sqs-metricscollected.html) 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](https://gist.github.com/fbrnc/5739f8bec042ac3326ad) is an example lambda gathering data from Cloudwatch to push to elasticsearch. With the [examples](http://docs.aws.amazon.com/fr_fr/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#publishingDataPoints) 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](http://docs.aws.amazon.com/fr_fr/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html) to be aware of to avoid breaking your budget/workflow.

> **Comments:**
> **username_0**: 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?  
> **username_3**: 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.  

---

### Answer by username_2 on 2018-11-29. Score: 9

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

*References:-*

* [Amazon CloudWatch Adds Metric Math to Enable Custom Operations on Metrics](https://aws.amazon.com/about-aws/whats-new/2018/04/amazon-cloudwatch-adds-metric-math-to-enable-custom-operations-on-metrics/)
* [Create a CloudWatch Alarm Based on a Metric Math Expression](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create-alarm-on-metric-math-expression.html)


---

