Learn how to monitor IoT Edge devices' built-in metrics with Azure Monitor.

You'll need Azure Cloud Shell for this lab with an active Azure subscription.

Perform the following steps in the Azure Cloud Shell environment.

  1. Select Bash from the top left as the environment for your cloud shell.
  2. Add the Azure IoT extension to the cloud shell instance.
    az extension add --name azure-iot
    
    If you get ModuleNotFoundError, please retry after removing the old extension using az extension remove –name azure-cli-iot-ext
  3. Create a resource group to hold the cloud infrastructure you'll use for the lab.

Perform these steps in the cloud shell environment.

  1. Create an IoT Hub
    az iot hub create \
    --resource-group iotlab-resources-$UNIQUESTRING \
    --name iotlab-hub-$UNIQUESTRING \
    --sku S1 \
    --partition-count 2   
    
  2. Register an IoT Edge device
    az iot hub device-identity create \
    --hub-name iotlab-hub-$UNIQUESTRING \
    --device-id edge-device-$UNIQUESTRING \
    --edge-enabled   
    

We'll use an Azure VM as a virtual edge "device" and associate it with the device identity created in the previous step.

  1. Setup an SSH key to login to the VM
    ssh-keygen -m PEM -t rsa -b 4096 -q -f ~/.ssh/id_lab -N ""   
    
  2. Provision a VM and associate with edge device identity
    # Set environment variable
    export CONNSTR=$(az iot hub device-identity show-connection-string \
      --device-id edge-device-$UNIQUESTRING \
      --hub-name iotlab-hub-$UNIQUESTRING \
      -o tsv)   
    
    # Deploy VM
    az group deployment create \
      --name edgeVm \
      --resource-group iotlab-resources-$UNIQUESTRING \
      --template-uri "https://aka.ms/iotedge-vm-deploy-lab" \
      --parameters location=$RGLOC \
      --parameters dnsLabelPrefix=iotedge-vm-$UNIQUESTRING \
      --parameters adminUsername='azureuser' \
      --parameters deviceConnectionString=$CONNSTR \
      --parameters authenticationType='sshPublicKey' \
      --parameters adminPasswordOrKey="$(< ~/.ssh/id_lab.pub)" | \
        jq .properties.outputs   
    
  3. Note the ssh command from the value key in previous command's output and use it to login to the VM. For example ssh -i ~/.ssh/id_lab azureuser@iotedge-vm-xyz.westus2.cloudapp.azure.com.
  4. It can take about 2 minutes for the VM to complete provisioning, enter watch sudo iotedge list in the VM's bash shell and wait for the edgeAgent module to be reported as running. It is expected to see "command not found" initially. Ctrl+c returns you to the VM's shell.
  5. Type exit in the VM's SSH shell to return the cloud shell environment
  1. Navigate to the Azure Portal using the following url: https://aka.ms/edgemon-portal
  2. Go to the portal page for IoT Hub created in the prior step. If you are having trouble finding it, enter the unique string you chose in the search bar after ensuring the subscription you are using is selected.
  3. Select the IoT Edge ToC item and click on the device you created earlier.
  4. On the device details page, click the Set modules button on the top left
  5. Click on + Add and select + IoT Edge Module
  6. In the Add IoT Edge Module screen, enter tempSensor for Module name and mcr.microsoft.com/azureiotedge-simulated-temperature-sensor:1.0.10 for Image URI. Then click the Add button at the bottom.
  7. Back on the Set Modules screen, click Review + create followed by Create to set the workload.
  8. In a few seconds, you should see the tempSensor module listed in output of sudo iotedge list on the VM created in the previous step.

Now, to monitor the edge device we'll add the Azure Monitor IoT Edge module.

  1. Note the Resource ID of the IoT Hub from the Properties ToC item:
  2. Select the IoT Edge ToC item and click on the device you created earlier.
  3. On the device details page, click the Set modules button on the top left
  4. Click on + Add and select + IoT Edge Module
  5. In the Add IoT Edge Module pane, enter azmonitor for Module name and mcr.microsoft.com/azuremonitor/containerinsights/ciprod:iot-0.1.3.2-amd64 for Image URI.
  6. On the same pane, click on the Environment Variables tab and add two variables with names and values noted below:

    HubResourceID

    Value noted in Step 1

    LogAnalyticsWorkspaceId

    80868412-0fe2-4c0f-af0d-d4a9dfbb5f24

    LogAnalyticsSharedKey

    B22uotMZQfOaIiWe9LIe72NPlOwEP3senQc9u1QhH4aHsJsJ3+rbjrY4qv1cL17V1UeGSl/fOXuFNi4NJDhBNA==

    (The last two characters of the shared key are ==)
  7. Click the Add button at the bottom to confirm module settings, followed by Review + create and Create on the Set Modules screen.
  8. In a few seconds, you should see the azmonitor module listed in output of sudo iotedge list on the VM created earlier.

In a few minutes, you can visualize detailed metrics from the device right in the IoT Hub portal page with Azure Monitor Workbooks!

Check out this 5-min video demo that explores the visualizations enabled using Azure Monitor Workbooks.

Congratulations on completing the lab! If you are interested in exploring more about this feature including how to monitor metrics from your custom module using this solution and at-scale alerting, please sign up for the preview at https://aka.ms/edgemon-intake