Introduction and Use Case:
This follows up on a previous post where we built a Raspberry Pi based soil sensor and onboarded it to Azure IoT Hub. What next? How do we read that data and get it into a Log Analytics Workspace? π€
Some advanced readers may have thought to make a diagnostics setting in the IoT Hub to forward telemetry data to a workspace, only to find just the βtelemetryβ data (send success/fail and other telemetry metrics π) but not the actual message data make it into the AzureDiagnostics table in a workspace (good guess though, that was my first move too π).
The best way to get our soil Sensor messages from IoT Hub into a Log Analytics workspace is from IoT Hub through a Service Bus via Service Bus Queue, and into a Logic App which can parse the βmessageβ data, then finally send it to a Log Analytics Workspace, like this:
π¨ IOT Hub β‘ Service Bus β‘ Logic App β‘ Log Analytics Workspace π
In this Post We Will:
- π Create an Azure Service Bus & Service Bus Queue π©
- π Setup an Azure Log Analytics Workspace π
- π Build a Logic App to Parse and send the Message Data π¨
- π Accomplish something AWESOME today! π
Create Azure Service Bus & Service Bus Queue
-
Login to the Azure portal and click +Create a Resource button, then search for Service Bus in the Marketplace.
-
Select Service Bus then Create.
- Select the Subscription, Resource Group, Logic App Name, Region, Enable Log Analytics, Plan, and Zone Redundancy for our Logic App.
- For Enable Log Analytics section, select No because this doesnβt pass on the message date; only the diagnostic data and other metrics.
- For Pricing Tier choose the Consumption plan.
π A servicebus-1 API Connection will be auto-generated when you succesfully create your service bus.
- Navigate to the Queues blade under Entities and select + Queue
- Name your queue after the pepper or plant youβre monitoring. You can leave the other settings alone.
Create a Log Analytics Workspace
- If you donβt already have one ready, navigate to Log Analytics Workspace in Azure Portal and follow the below steps to get one going (these steps are taken from a previous post so you might notice different resource groups etc. in the following screenshots. For this exercise I kept everything in a resource group called βIoTβ and called my workspace βPeppersβ):
- Select +Create
- Select Subscription and Resource Group:
- Select Instance Name and Region:
Commitment / Pricing Tiers
Choose the appropriate commitment tier given your expected daily ingest volume.
It makes sense to bump up to the 100GB/day commitment tier even when you hit as little as 50GB/day because of the 50% discount afforded at 100GB/day, for example.
π‘ Check out my prior Sentinel Cost Optimization articles and exercises Parts 1 and 2 at hanley.cloud. While youβre at it, donβt forget to peruse my GitHub repository for KQL breakdowns and ready-made queries for all kinds of complicated situations that you can simply copy and paste.
- Click Review & Create β¦to Finish Setting up a New Log Analytics Workspace
Build Logic App
-
Search for Logic Apps in the top search bar and click on the icon, then + Add
-
On the next page, fill out the required details as shown below. Make sure to select No for Enable log analytics unless you want Diagnostics for the Logic App forwarded as well.
-
Open the new app in Logic App Designer and letβs build this out in 4 steps!
Step 1: Trigger - When one or more messages arrive in a queue (auto-complete)
- Name your queue after the sensor sending the data.
π You can send multiple sensors across multiple queues on a single service bus to a log analytics workspace. I named my Service Bus Peppers and have 2 queues at the time of writing this article: Goat Horn and Szechuan πΆπΆπΆ
- Setup your connection:
- Set your connection to the Service Bus
Step 2: Compose - Write Service Bus Message - Base64ToString
- Use the following expression to convert the message to string:
base64ToString(triggerBody()?['ContentData'])
Step 3: Parse JSON - Parse Temperature and Humidity to Update Custom Log
- Use the following schema to tell the parser how to read the message data:
{
"properties": {
"Humidity": {
"type": "integer"
},
"Temperature": {
"type": "number"
}
},
"type": "object"
}
Step 4: Send Data - Send Data to Log Analytics
- Fill out the request body as illustrated:
π An azureloganalyticsdatacollector-1 API Connection will show up auto-magically when you succesfully create your Logic App (just like the servicebus-1 API connection earlier).
Confirm Results in Log Analytics Workspace:
Navigate to your Log Analytics Workspace and query your new custom list:
Here we can see the Temperate and Humidity by Pepperβ
π± πΏ What will you grow? πΌπ»
In this Post We:
- π Created an Azure Service Bus & Service Bus Queue π©
- π Setup an Azure Log Analytics Workspace π
- π Built a Logic App to Parse and send the Message Data π¨
- π Accomplished something AWESOME today! π
Next Time:
TODAY weβve managed to get just one IoT device registered and transmitting itβs sensor data across a service bus and custom endpoint to a Log Analytics Workspace.
NEXT TIME weβll look at the free tiered Azure IoT Hubβs most significant limitation - the custom endpoint bottleneck - and how to solve it with a well constructed logic app so that we can scale this up and get readings for more peppers.
π This circumvents the restrictions on the free tiered Azure IoTHub, allowing you to transmitt data from multiple IoT devices simultaneously without upgrading your subscription π