Deploy Azure Stream Analytics as an IoT Edge module - preview

IoT devices can produce large quantities of data. To reduce the amount of uploaded data or to eliminate the round-trip latency of an actionable insight, the data must sometimes be analyzed or processed before it reaches the cloud.

Azure IoT Edge takes advantage of pre-built Azure service IoT Edge modules for quick deployment. Azure Stream Analytics is one such module. You can create an Azure Stream Analytics job from its portal and then go to the Azure IoT Hub portal to deploy it as an IoT Edge module.

Azure Stream Analytics provides a richly structured query syntax for data analysis both in the cloud and on IoT Edge devices. For more information about Azure Stream Analytics on IoT Edge, see Azure Stream Analytics documentation.

This tutorial walks you through creating an Azure Stream Analytics job and deploying it on an IoT Edge device. Doing so lets you process a local telemetry stream directly on the device and generate alerts that drive immediate action on the device.

The tutorial presents two modules:

  • A simulated temperature sensor module (tempSensor) that generates temperature data from 20 degrees to 120 degrees, incremented every 5 seconds.
  • A Stream Analytics module that resets the tempSensor when the 30-second average reaches 70. In a production environment, you might use this functionality to shut off a machine or take preventative measures when the temperature reaches dangerous levels.

In this tutorial, you learn how to:

  • Create an Azure Stream Analytics job to process data on the edge.
  • Connect the new Azure Stream Analytics job with other IoT Edge modules.
  • Deploy the Azure Stream Analytics job to an IoT Edge device.

Prerequisites

  • An IoT hub.
  • The device that you created and configured in the quickstart or in the articles about deploying Azure IoT Edge on a simulated device in Windows or in Linux. You need to know the device connection key and the device ID.
  • Docker running on your IoT Edge device.
  • Python 2.7.x on your IoT Edge device.
    • Install Python 2.7 on Windows.
    • Most Linux distributions, including Ubuntu, already have Python 2.7 installed. To ensure that pip is installed, use the following command: sudo apt-get install python-pip.

Create an Azure Stream Analytics job

In this section, you create an Azure Stream Analytics job to take data from your IoT hub, query the sent telemetry data from your device, and then forward the results to an Azure Blob storage container. For more information, see the "Overview" section of the Stream Analytics documentation.

Create a storage account

An Azure Storage account is required to provide an endpoint to be used as an output in your Azure Stream Analytics job. The example in this section uses the Blob storage type. For more information, see the "Blobs" section of the Azure Storage documentation.

  1. In the Azure portal, go to Create a resource, enter Storage account in the search box, and then select Storage account - blob, file, table, queue.

  2. In the Create storage account pane, enter a name for your storage account, select the same location where your IoT hub is stored, select the same resource group as your IoT hub, and then select Create. Note the name for later use.

    Create a storage account

  3. Go to the storage account that you created, and then select Browse blobs.

  4. Create a new container for the Azure Stream Analytics module to store data, set the access level to Container, and then select OK.

    Storage settings

Create a Stream Analytics job

  1. In the Azure portal, go to Create a resource > Internet of Things, and then select Stream Analytics Job.

  2. In the New Stream Analytics Job pane, perform the following steps:

    1. In the Job name box, type a job name.
    2. Use the same Resource group and Location as your IoT hub.

      Note

      Currently, Azure Stream Analytics jobs on IoT Edge aren't supported in the West US 2 region.

    3. Under Hosting environment, select Edge.

  3. Select Create.

  4. In the created job, under Job Topology, open Inputs.

    Azure Stream Analytics input

  5. Select Add stream input, then select Edge Hub.

  6. In the New input pane, enter temperature as the input alias.

  7. Select Save.

  8. Under Job Topology, open Outputs.

    Azure Stream Analytics output

  9. Select Add, then select Edge Hub.

  10. In the New output pane, enter alert as the output alias.

  11. Select Save.

  12. Under Job Topology, select Query, and then replace the default text with the following query:

    SELECT  
        'reset' AS command 
    INTO 
       alert 
    FROM 
       temperature TIMESTAMP BY timeCreated 
    GROUP BY TumblingWindow(second,30) 
    HAVING Avg(machine.temperature) > 70
    
  13. Select Save.

  14. Under Configure, select IoT Edge settings.
  15. Select your Storage account from the drop-down menu.
  16. Choose to Use existing container, and select the one that you created from the drop-down menu.
  17. Select Save.

Deploy the job

You are now ready to deploy the Azure Stream Analytics job on your IoT Edge device.

  1. In the Azure portal, in your IoT hub, go to IoT Edge (preview), and then open the details page for your IoT Edge device.

  2. Select Set modules.
    If you previously deployed the tempSensor module on this device, it might autopopulate. If it does not, add the module by doing the following:

    a. Click Add and select IoT Edge Module.

    b. For the name, type tempSensor.

    c. For the image URI, enter microsoft/azureiotedge-simulated-temperature-sensor:1.0-preview.

    d. Leave the other settings unchanged.

    e. Select Save.

  3. To add your Azure Stream Analytics Edge job, click Add and select Azure Stream Analytics Module.

  4. Select your subscription and the Azure Stream Analytics Edge job that you created.

  5. Select your subscription and the storage account that you created, and then select Save.

  6. Select Next.

  7. Copy the following code to Routes. Replace {moduleName} with the name of your Azure Stream Analytics module. The module should have the same name as the job that it was created from.

    {
        "routes": {                                                               
          "telemetryToCloud": "FROM /messages/modules/tempSensor/* INTO $upstream", 
          "alertsToCloud": "FROM /messages/modules/{moduleName}/* INTO $upstream", 
          "alertsToReset": "FROM /messages/modules/{moduleName}/* INTO BrokeredEndpoint(\"/modules/tempSensor/inputs/control\")", 
          "telemetryToAsa": "FROM /messages/modules/tempSensor/* INTO BrokeredEndpoint(\"/modules/{moduleName}/inputs/temperature\")" 
        }
    }
    
  8. Select Next.

  9. In the Review Template step, select Submit.

  10. Return to the device details page, and then select Refresh.
    You should see the new Stream Analytics module running, along with the IoT Edge agent module and the IoT Edge hub.

    Module output

View data

Now you can go to your IoT Edge device to check out the interaction between the Azure Stream Analytics module and the tempSensor module.

  1. Check that all the modules are running in Docker:

    docker ps  
    

    Docker output

  2. View all system logs and metrics data. Use the Stream Analytics module name:

    docker logs -f {moduleName}  
    

You should be able to watch the machine's temperature gradually rise until it reaches 70 degrees for 30 seconds. Then the Stream Analytics module triggers a reset, and the machine temperature drops back to 21.

Docker log

Next steps

In this tutorial, you configured an Azure Streaming Analytics job to analyze data from your IoT Edge device. You then loaded this Azure Stream Analytics module on your IoT Edge device to process and react to temperature increase locally, as well as sending the aggregated data stream to the cloud. To see how Azure IoT Edge can create more solutions for your business, continue on to the other tutorials.