SECRET OF CSS

Visualize your Amazon Lookout for Metrics anomaly results with Amazon QuickSight


One of the challenges encountered by teams using Amazon Lookout for Metrics is quickly and efficiently connecting it to data visualization. The anomalies are presented individually on the Lookout for Metrics console, each with their own graph, making it difficult to view the set as a whole. An automated, integrated solution is needed for deeper analysis.

In this post, we use a Lookout for Metrics live detector built following the Getting Started section from the AWS Samples, Amazon Lookout for Metrics GitHub repo. After the detector is active and anomalies are generated from the dataset, we connect Lookout for Metrics to Amazon QuickSight. We create two datasets: one by joining the dimensions table with the anomaly table, and another by joining the anomaly table with the live data. We can then add these two datasets to a QuickSight analysis, where we can add charts in a single dashboard.

We can provide two types of data to the Lookout for Metrics detector: continuous and historical. The AWS Samples GitHub repo offers both, though we focus on the continuous live data. The detector monitors this live data to identify anomalies and writes the anomalies to Amazon Simple Storage Service (Amazon S3) as they’re generated. At the end of a specified interval, the detector analyzes the data. Over time, the detector learns to more accurately identify anomalies based on patterns it finds.

Lookout for Metrics uses machine learning (ML) to automatically detect and diagnose anomalies in business and operational data, such as a sudden dip in sales revenue or customer acquisition rates. The service is now generally available as of March 25, 2021. It automatically inspects and prepares data from a variety of sources to detect anomalies with greater speed and accuracy than traditional methods used for anomaly detection. You can also provide feedback on detected anomalies to tune the results and improve accuracy over time. Lookout for Metrics makes it easy to diagnose detected anomalies by grouping together anomalies related to the same event and sending an alert that includes a summary of the potential root cause. It also ranks anomalies in order of severity so you can prioritize your attention to what matters the most to your business.

QuickSight is a fully-managed, cloud-native business intelligence (BI) service that makes it easy to connect to your data to create and publish interactive dashboards. Additionally, you can use Amazon QuickSight to get instant answers through natural language queries.

You can access serverless, highly scalable QuickSight dashboards from any device, and seamlessly embed them into your applications, portals, and websites. The following screenshot is an example of what you can achieve by the end of this post.

image001 1

Overview of solution

The solution is a combination of AWS services, primarily Lookout for Metrics, QuickSight, AWS Lambda, Amazon Athena, AWS Glue, and Amazon S3.

The following diagram illustrates the solution architecture. Lookout for Metrics detects and sends the anomalies to Lambda via an alert. The Lambda function generates the anomaly results as CSV files and saves them in Amazon S3. An AWS Glue crawler analyzes the metadata, and creates tables in Athena. QuickSight uses Athena to query the Amazon S3 data, allowing dashboards to be built to visualize both the anomaly results and the live data.

Solution Architecture

This solution expands on the resources created in the Getting Started section of the GitHub repo. For each step, we include options to create the resources either using the AWS Management Console or launching the provided AWS CloudFormation stack. If you have a customized Lookout for Metrics detector, you can use it and adapt it the following notebook to achieve the same results.

The implementation steps are as follows:

  1. Create the Amazon SageMaker notebook instance (ALFMTestNotebook) and notebooks using the stack provided in the Initial Setup section from the GitHub repo.
  2. Open the notebook instance on the SageMaker console and navigate to the amazon-lookout-for-metrics-samples/getting_started folder.
  3. Create the S3 bucket and complete the data preparation using the first notebook (1.PrereqSetupData.ipynb). Open the notebook with the conda_python3 kernel, if prompted.

We skip the second notebook because it’s focused on backtesting data.

  1. If you’re walking through the example using the console, create the Lookout for Metrics live detector and its alert using the third notebook (3.GettingStartedWithLiveData.ipynb).

If you’re using the provided CloudFormation stacks, the third notebook isn’t required. The detector and its alert are created as part of the stack.

  1. After you create the Lookout for Metrics live detector, you need to activate it from the console.

This can take up to 2 hours to initialize the model and detect anomalies.

  1. Deploy a Lambda function, using Python with a Pandas library layer, and create an alert attached to the live detector to launch it.
  2. Use the combination of Athena and AWS Glue to discover and prepare the data for QuickSight.
  3. Create the QuickSight data source and datasets.
  4. Finally, create a QuickSight analysis for visualization, using the datasets.

The CloudFormation scripts are typically run as a set of nested stacks in a production environment. They’re provided individually in this post to facilitate a step-by-step walkthrough.

Prerequisites

To go through this walkthrough, you need an AWS account where the solution will be deployed. Make sure that all the resources you deploy are in the same Region. You need a running Lookout for Metrics detector built from notebooks 1 and 3 from the GitHub repo. If you don’t have a running Lookout for Metrics detector, you have two options:

  • Run notebooks 1 and 3, and continue from the step 1 of this post (creating the Lambda function and alert)
  • Run notebook 1 and then use the CloudFormation template to generate the Lookout for Metrics detector

Create the live detector using AWS CloudFormation

The L4MLiveDetector.yaml CloudFormation script creates the Lookout for Metrics anomaly detector with its source pointing to the live data in the specified S3 bucket. To create the detector, complete the following steps:

  1. Launch the stack from the following link:

image004

  1. On the Create stack page, choose Next.
  2. On the Specify stack details page, provide the following information:
    1. A stack name. For example, L4MLiveDetector.
    2. The S3 bucket, <Account Number>-lookoutmetrics-lab.
    3. The Role ARN, arn:aws:iam::<Account Number>:role/L4MTestRole.
    4. An anomaly detection frequency. Choose PT1H (hourly).
  3. Choose Next.
  4. On the Configure stack options page, leave everything as is and choose Next.
  5. On the Review page, leave everything as is and choose Create stack.

Create the live detector SMS alert using AWS CloudFormation (Optional)

This step is optional. The alert is presented as an example, with no impact on the dataset creation. The L4MLiveDetectorAlert.yaml CloudFormation script creates the Lookout for Metrics anomaly detector alert with an SMS target.

  1. Launch the stack from the following link:

image004

  1. On the Create stack page, choose Next.
  2. On the Specify stack details page, update the SMS phone number and enter a name for the stack (for example, L4MLiveDetectorAlert).
  3. Choose Next.
  4. On the Configure stack options page, leave everything as is and choose Next.
  5. On the Review page, select the acknowledgement check box, leave everything else as is, and choose Create stack.

Resource cleanup

Before proceeding with the next step, stop your SageMaker notebook instance to ensure no unnecessary costs are incurred. It is no longer needed.

Create the Lambda function and alert

In this section, we provide instructions on creating your Lambda function and alert via the console or AWS CloudFormation.

Create the function and alert with the console

You need a Lambda AWS Identity and Access Management (IAM) role following the least privilege best practice to access the bucket where you want the results to be saved.

    1. On the Lambda console, create a new function.
    2. Select Author from scratch.
    3. For Function name¸ enter a name.
    4. For Runtime, choose Python 3.8.
    5. For Execution role, select Use an existing role and specify the role you created.
    6. Choose Create function.
  1. image006
    1. Download the ZIP file containing the necessary code for the Lambda function.
    2. On the Lambda console, open the function.
    3. On the Code tab, choose Upload from, choose .zip file, and upload the file you downloaded.
    4. Choose Save.

image008

Your file tree should remain the same after uploading the ZIP file.

image010

  1. In the Layers section, choose Add layer.
  2. Select Specify an ARN.
  3. In the following GitHub repo, choose the CSV corresponding to the Region you’re working in and copy the ARN from the latest Pandas version.
  4. For Specify an ARN, enter the ARN you copied.
  5. Choose Add.

image011

  1. To adapt the function to your environment, at the bottom of the code from the lambda_function.py file, make sure to update the bucket name with your bucket where you want to save the anomaly results, and the DataSet_ARN from your anomaly detector.
  2. Choose Deploy to make the changes active.

image014

You now need to connect the Lookout for Metrics detector to your function.

  1. On the Lookout for Metrics console, navigate to your detector and choose Add alert.
  2. Enter the alert name and your preferred severity threshold.
  3. From the channel list, choose Lambda.
  4. Choose the function you created and make sure you have the right role to trigger it.
  5. Choose Add alert.

image015

Now you wait for your alert to trigger. The time varies depending on when the detector finds an anomaly.

When an anomaly is detected, Lookout for Metrics triggers the Lambda function. It receives the necessary information from Lookout for Metrics and checks if there is already a saved CSV file in Amazon S3 at the corresponding timestamp of the anomaly. If there isn’t a file, Lambda generates the file and adds the anomaly data. If the file already exists, Lambda updates the file with the extra data received. The function generates a separated CSV file for each different timestamp.

Create the function and alert using AWS CloudFormation

Similar to the console instructions, you download the ZIP file containing the necessary code for the Lambda function. However, in this case it needs to be uploaded to the S3 bucket in order for the AWS CloudFormation code to load it during function creation.

In the S3 bucket specified in the Lookout for Metrics detector creation, create a folder called lambda-code, and upload the ZIP file.

image017

The Lambda function loads this as its code during creation.

image019

The L4MLambdaFunction.yaml CloudFormation script creates the Lambda function and alert resources and uses the function code archive stored in the same S3 bucket.

  1. Launch the stack from the following link:

image004

  1. On the Create stack page, choose Next.
  2. On the Specify stack details page, specify a stack name (for example, L4MLambdaFunction).
  3. In the following GitHub repo, open the CSV corresponding to the Region you’re working in and copy the ARN from the latest Pandas version.
  4. Enter the ARN as the Pandas Lambda layer ARN parameter.
  5. Choose Next.
  6. On the Configure stack options page, leave everything as is and choose Next.
  7. On the Review page, select the acknowledgement check box, leave everything else as is, and choose Create stack.

Activate the detector

Before proceeding to the next step, you need to activate the detector from the console.

  1. On the Lookout for Metrics console, choose Detectors in the navigation pane.
  2. Choose your newly created detector.
  3. Choose Activate, then choose Activate again to confirm.

Activation initializes the detector; it’s finished when the model has completed its learning cycle. This can take up to 2 hours.

Prepare the data for QuickSight

Before you complete this step, give the detector time to find anomalies. The Lambda function you created saves the anomaly results in the Lookout for Metrics bucket in the anomalyResults directory. We can now process this data to prepare it for QuickSight.

Create the AWS Glue crawler on the console

After some anomaly CSV files have been generated, we use an AWS Glue crawler to generate the metadata tables.

  1. On the AWS Glue console, choose Crawlers in the navigation pane.
  2. Choose Add crawler.

image021

  1. Enter a name for the crawler (for example, L4MCrawler).
  2. Choose Next.
  3. For Crawler source type, select Data stores.
  4. For Repeat crawls of S3 data stores, select Crawl all folders.
  5. Choose Next.

image023 1

  1. On the data store configuration page, for Crawl data in, select Specified path in my account.
  2. For Include path, enter the path of your dimensionContributions file (s3://YourBucketName/anomalyResults/dimensionContributions).
  3. Choose Next.
  4. Choose Yes to add another data store and repeat the instructions for metricValue_AnomalyScore(s3://YourBucketName/anomalyResults/metricValue_AnomalyScore).
  5. Repeat the instructions again for the live data to be analyzed by the Lookout for Metrics anomaly detector (this is the S3 dataset location from your Lookout for Metrics detector).

You should now have three data stores for the crawler to process.

image025 876x1024 1

Now you need to select the role to allow the crawler to go through the S3 locations of your data.

  1. For this post, select Create an IAM role and enter a name for the role.
  2. Choose Next.

image027 955x1024 1

  1. For Frequency, leave as Run on demand and choose Next.
  2. In the Configure the crawler’s output section, choose Add database.

This creates the Athena database where your metadata tables are located after the crawler is complete.

  1. Enter a name for your database and choose Create.
  2. Choose Next, then choose Finish.

image029 1

  1. On the Crawlers page of the AWS Glue console, select the crawler you created and choose Run crawler.

You may need to wait a few minutes, depending on the size of the data. When it’s complete, the crawler’s status shows as Ready. To see the metadata tables, navigate to your database on the Databases page and choose Tables in the navigation pane.

In this example, the metadata table called live represents the S3 dataset from the Lookout for Metrics live detector. As a best practice, it’s recommended to encrypt your AWS Glue Data Catalog metadata.

image031

Athena automatically recognizes the metadata tables, and QuickSight uses Athena to query the data and visualize the results.

Create the AWS Glue crawler using AWS CloudFormation

The L4MGlueCrawler.yaml CloudFormation script creates the AWS Glue crawler, its associated IAM role, and the output Athena database.

  1. Launch the stack from the following link:

image004

  1. On the Create stack page, choose Next.
  2. On the Specify stack details page, enter a name for your stack (for example, L4MGlueCrawler), and choose Next.
  3. On the Configure stack options page, leave everything as is and choose Next.
  4. On the Review page, select the acknowledgement check box, leave everything else as is, and choose Create stack.

Run the AWS Glue crawler

After you create the crawler, you need to run it before moving to the next step. You can run it from the console or the AWS Command Line Interface (AWS CLI). To use the console, complete the following steps:

  1. On the AWS Glue console, choose Crawlers in the navigation pane.
  2. Select your crawler (L4MCrawler).
  3. Choose Run crawler.

When the crawler is complete, it shows the status Ready.

image033

Create a QuickSight account

Before starting this next step, navigate to the QuickSight console and create an account if you don’t already have one. To make sure you have access to the corresponding services (Athena and S3 bucket), choose your account name on the top right, choose Manage QuickSight, and choose Security and Permissions, where you can add the necessary services. When setting up your Amazon S3 access, make sure to select Write permission for Athena Workgroup.

image035 1024x994 1

Now you’re ready to visualize your data in QuickSight.

Create the QuickSight datasets on the console

If this is your first time using Athena, you have to configure the output location of the queries. For instructions, refer to Steps 1–6 in Create a database. Then complete the following steps:

  1. On the QuickSight console, choose Datasets.
  2. Choose New dataset.
  3. Choose Athena as your source.
  4. Enter a name for your data source.
  5. Choose Create data source.

image037 1024x529 1

  1. For your database, specify the one you created earlier with the AWS Glue crawler.
  2. Specify the table that contains your live data (not the anomalies).
  3. Choose Edit/preview data.

You’re redirected to an interface similar to the following screenshot.

image039

The next step is to add and combine the metricValue_AnomalyScore data with the live data.

  1. Choose Add data.
  2. Choose Add data source.
  3. Specify the database you created and the metricValue_AnomalyScore table.
  4. Choose Select.

image041 1022x1024 1

You need now to configure the join of the two tables.

  1. Choose the link between the two tables.
  2. Leave the join type as Left, add the timestamp and each dimension you have as a join clause, and choose Apply.

In the following example, we use timestamp, platform, and marketplace as join clauses.

image043

On the right pane, you can remove the fields you’re not interested in keeping.

  1. Remove the timestamp from the metricValue_AnomalyScore table to not have a duplicated column.
  2. Change the timestamp data type (of the live data table) from string to date, and specify the correct format. In our case, it should be yyyy-MM-dd HH:mm:ss.

The following screenshot shows your view after you remove some fields and adjust the data type.

image045

  1. Choose Save and visualize.
  2. Choose the pencil icon next to the dataset.
  3. Choose Add dataset and choose dimensioncontributions.

Create the QuickSight datasets using AWS CloudFormation

This step contains three CloudFormation stacks.

The first CloudFormation script, L4MQuickSightDataSource.yaml, creates the QuickSight Athena data source.

  1. Launch the stack from the following link:

image004

  1. On the Create stack page, choose Next.
  2. On the Specify stack details page, enter your QuickSight user name, the QuickSight account Region (specified when creating the QuickSight account), and a stack name (for example, L4MQuickSightDataSource).
  3. Choose Next.
  4. On the Configure stack options page, leave everything as is and choose Next.
  5. On the Review page, leave everything as is and choose Create stack.

The second CloudFormation script, L4MQuickSightDataSet1.yaml, creates a QuickSight dataset that joins the dimensions table with the anomaly table.

  1. Launch the stack from the following link:

image004

  1. On the Create stack page, choose Next.
  2. On the Specify stack details, enter a stack name (for example, L4MQuickSightDataSet1).
  3. Choose Next.
  4. On the Configure stack options page, leave everything as is and choose Next.
  5. On the Review page, leave everything as is and choose Create stack.

The third CloudFormation script, L4MQuickSightDataSet2.yaml, creates the QuickSight dataset that joins the anomaly table with the live data table.

  1. Launch the stack from the following link:

image004

  1. On the Create stack page¸ choose Next.
  2. On the Specify stack details page, enter a stack name (for example, L4MQuickSightDataSet2).
  3. Choose Next.
  4. On the Configure stack options page, leave everything as is and choose Next.
  5. On the Review page, leave everything as is and choose Create stack.

Create the QuickSight analysis for dashboard creation

This step can only be completed on the console. After you’ve created your QuickSight datasets, complete the following steps:

  1. On the QuickSight console, choose Analysis in the navigation pane.
  2. Choose New analysis.
  3. Choose the first dataset, L4MQuickSightDataSetWithLiveData.

image047 1

  1. Choose Create analysis.

image049 1

The QuickSight analysis is initially created with only the first dataset.

  1. To add the second dataset, choose the pencil icon next to Dataset and choose Add dataset.
  2. Choose the second dataset and choose Select.

image051 1

You can then use either dataset for creating charts by choosing it on the Dataset drop-down menu.

image053 1

Dataset metrics

You have successfully created a QuickSight analysis from Lookout for Metrics inference results and the live data. Two datasets are in QuickSight for you to use: L4M_Visualization_dataset_with_liveData and L4M_Visualization_dataset_with_dimensionContribution.

The L4M_Visualization_dataset_with_liveData dataset includes the following metrics:

  • timestamp – The date and time of the live data passed to Lookout for Metrics
  • views – The value of the views metric
  • revenue – The value of the revenue metric
  • platform, marketplace, revenueAnomalyMetricValue, viewsAnomalyMetricValue, revenueGroupScore and viewsGroupScore – These metrics are part of both datasets

The L4M_Visualization_dataset_with_dimensionContribution dataset includes the following metrics:

  • timestamp – The date and time of when the anomaly was detected
  • metricName – The metrics you’re monitoring
  • dimensionName – The dimension within the metric
  • dimensionValue – The value of the dimension
  • valueContribution – The percentage on how much dimensionValue is affecting the anomaly when detected

The following screenshot shows these five metrics on the anomaly dashboard of the Lookout for Metrics detector.

image057

The following metrics are part of both datasets:

  • platform – The platform where the anomaly happened
  • marketplace – The marketplace where the anomaly happened
  • revenueAnomalyMetricValue and viewsAnomalyMetricValue – The corresponding values of the metric when the anomaly was detected (in this situation, the metrics are revenue or views)
  • revenueGroupScore and viewsGroupScore – The severity scores for each metric for the detected anomaly

To better understand these last metrics, you can review the CSV files created by the Lambda function in your S3 bucket where you saved anomalyResults/metricValue_AnomalyScore.

Next steps

The next step is to build the dashboards for the data you want to see. This post doesn’t include an explanation on creating QuickSight charts. If you’re new to QuickSight, refer to Getting started with data analysis in Amazon QuickSight for an introduction. The following screenshots show examples of basic dashboards. For more information, check out the QuickSight workshops.

image058

image059

Conclusion

The anomalies are presented individually on the Lookout for Metrics console, each with their own graph, making it difficult to view the set as a whole. An automated, integrated solution is needed for deeper analysis. In this post, we used a Lookout for Metrics detector to generate anomalies, and connected the data to QuickSight to create visualizations. This solution enables us to conduct deeper analysis into anomalies and have them all in one single place/dashboard.

As a next step, this solution could as well be expanded by adding an extra dataset and combine anomalies from multiple detectors. You could also adapt the Lambda function. The Lambda function contains the code that generates the data sets and variable names that we use for the QuickSight dashboards. You can adapt this code to your particular use case by changing the data sets itself or the variable names that make more sense to you.

If you have any feedback or questions, please leave them in the comments.


About the Authors

Benoit de PatoulBenoît de Patoul is an AI/ML Specialist Solutions Architect at AWS. He helps customers by providing guidance and technical assistance to build solutions related to AI/ML when using AWS.

Paul TroianoPaul Troiano is a Senior Solutions Architect at AWS, based in Atlanta, GA. He helps customers by providing guidance on technology strategies and solutions on AWS. He is passionate about all things AI/ML and solution automation.



News Credit

%d bloggers like this: