Anomaly Detection With CloudWatch
Tags: aws, performance, cloud, analytics
Published: 2021-06-30 09:52:26

As a performance engineer, I work closely with the Ops teams to proactively identify any production performance bottlenecks before they negatively impact the user experience. We employ a variety of monitoring and alerting techniques to help us with this process.

AWS CloudWatch is one of the monitoring tools we utilise. Recently, I was pleasantly surprised to see AWS anomaly detection functionality in CloudWatch. It's a fantastic addition to the CloudWatch features. It brings the capabilities of anomaly detection closer to the other tools like Splunk, AppDynamics, and SumoLogic, to mention a few. 

###What is an Anomaly?
An anomaly is also called an Outlier. An outlier is a value or point or an object attribute that exhibits an abnormal behaviour in a particular or examined context compared to the rest of the observations. 

In a separate post, I will go into more detail on the outliers. For this post, we will focus on CloudWatch.

###What is Anomaly detection?
Anomaly detection is the process of discovering values, occurrences, or observations that differ considerably from the majority of the data and raise suspicions. A variety of algorithms are available to assist in anomaly detection. K-nearest neighbour, Local Outlier Factor (LOF), and K-means are a few such examples. 

###How to set Anomaly detection using CloudWatch?
CloudWatch anomaly detection uses statistical and machine learning algorithms. These algorithms continuously evaluate system and application data, establish normal baselines, and surface anomalies With minimal human participation.

The algorithms are trained using two weeks of metric data. Even if a metric doesn't have a lot of data, you can still enable anomaly detection on it.

In the graph, the projected range of values is shown by a grey band. If the metrics actual value exceeds or drops from this band, it is shown in red. It's algorithms way of saying that this value is a potential anomaly.

To enable anomaly detection, go to the CloudWatch dashboard, pick anomaly detection from the math expressions menu, and then apply calculate band to a specific metric. As shown below.

![setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15i16n5dhddqfa2buwcr.png)

Below are some of the examples from the [AWS documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html#CloudWatch_Anomaly_Detection_Algorithm). For more information on this topic, refer to this [link](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html#CloudWatch_Anomaly_Detection_Algorithm).

![example 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3ehaj0zhjgud7twk56q.png)

![example 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8v058ijw47j96kptvk5v.png)

Follow the alert setup method to create an anomalous alert for a metric. Select the "Anomaly detection" condition rather than the "Static" threshold criterion and one of the three alarm conditions. As seen in the illustration below. 
![alert setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mek8rh0z9ssx4l13ga6.png)

You can choose to exclude specific time periods from being used to train the model when you enable anomaly detection on a metric. This way, you may keep deployments and other uncommon occurrences out of model training, resulting in the most accurate model possible. 

Once I've integrated it in our test and production environments, I'll report back on my findings.
 
**Note:** Using anomaly detection models for alarms incurs charges on your AWS account.

If you want to give it a try, refer this [AWS post](https://aws.amazon.com/blogs/mt/how-to-set-up-cloudwatch-anomaly-detection-to-set-dynamic-alarms-automate-actions-and-drive-online-sales/).

------------------------------------------------------------------

Thanks for reading!

If you enjoyed this article feel free to share on social media 🙂

Say Hello on: [Linkedin](https://www.linkedin.com/in/harinderseera/) | [Twitter](https://twitter.com/harinderseera) | [Polywork](https://www.polywork.com/harinderseera) 

Blogging: [Dev](https://dev.to/harinderseera) | [Hashnode](https://harinderseera.hashnode.dev/)

Github: [hseera](https://github.com/hseera)