Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 57 ยท AI and Machine Learning

Azure Machine Learning and IoT Hub

Understand Azure Machine Learning (workspaces, compute, jobs, designer, online endpoints) and IoT Hub (device identities, routing, provisioning, Digital Twins) with runnable stand-ins.

The previous lesson showed ready-made AI you call over an API. Sometimes you need your own model, trained on your own data, and sometimes your data comes from thousands of physical devices. Two Azure services cover these needs: Azure Machine Learning (training, tracking and serving models) and IoT Hub (connecting and managing devices and receiving their messages). They often work together: devices stream sensor readings, and a model deployed behind an endpoint scores them.

Both services need a subscription and, for IoT, real or simulated devices, so the Azure commands here are read-only. The runnable examples are JavaScript stand-ins that model a traffic split between two model versions and message routing in IoT Hub, using invented data.

Azure Machine Learning

A workspace is the top-level resource of Azure Machine Learning (AML). It holds everything related to a project and links to a storage account, a container registry, a key vault and Application Insights that it uses behind the scenes. Its main building blocks:

  • Data assets: references to data in storage, versioned so a training run can say exactly which data it used.
  • Environments: a Docker image plus a package list that defines the software a job runs with.
  • Compute: a compute instance (a personal cloud workstation for notebooks) or a compute cluster that scales between zero and a maximum number of nodes. Scaling to zero means you pay nothing while no job is queued (see Scaling and Autoscale for the same idea).
  • Jobs: one execution of a script on compute. AML records the code, the data version, the environment, the metrics and the output model artifact, so every result can be reproduced and compared.
  • Models and endpoints: a registered model, and the service that exposes it for predictions.
Terminal
az extension add --name ml

az ml workspace create --name ws-demo --resource-group rg-ml --location westeurope

az ml compute create --name cpu-cluster --type amlcompute \
  --size Standard_DS3_v2 --min-instances 0 --max-instances 4 \
  --workspace-name ws-demo --resource-group rg-ml

A job is described in YAML and submitted with az ml job create. The sketch below is read-only; the train.py script and the environment name are yours to supply:

YAML
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
code: ./src
command: python train.py --data ${{inputs.training_data}} --lr 0.01
inputs:
  training_data:
    type: uri_folder
    path: azureml:customers-data@latest
environment: azureml:my-sklearn-env@latest
compute: azureml:cpu-cluster
experiment_name: churn-model
Terminal
az ml job create --file job.yml --workspace-name ws-demo --resource-group rg-ml

No-code options: designer and automated ML

You do not have to write training code to start. The designer is a drag-and-drop canvas where you connect modules (load data, clean, split, train, evaluate) into a pipeline. Automated ML tries many algorithms and settings for you on a tabular, text or image problem, ranks the results and explains the best one. Both produce an ordinary model you can deploy. They are good for a first prototype or when the team has more analysts than programmers; code-first jobs are better when you need full control and source-controlled, repeatable training.

Endpoints: serving a model

An online endpoint is a stable HTTPS URL with authentication. Behind it sit one or more deployments, each running a specific model version on managed compute. You can put 100% of traffic on one deployment, or split it, for example 90% to the current model and 10% to a new one, then shift more traffic when the new model proves itself. This is the same blue-green and canary idea you saw in Deployment Slots, Blue-Green and Canary. A batch endpoint instead scores a large dataset in the background and writes results to storage, which suits nightly scoring.

Terminal
az ml online-endpoint create --name churn-api --auth-mode key
az ml online-deployment create --name blue --endpoint churn-api --file blue.yml --all-traffic
az ml online-deployment create --name green --endpoint churn-api --file green.yml
az ml online-endpoint update --name churn-api --traffic "blue=90 green=10"

Splitting traffic: a stand-in

The stand-in sends 1,000 requests to an endpoint that has two invented "models" and a configurable split. It then prints how many requests each deployment served and its average error, so you can see why a small canary share is a cheap way to test.

JS Blue-green traffic split for a model

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Even with only about a hundred requests, the 10% canary shows the candidate is better, so you can promote it with confidence.

IoT Hub

Azure IoT Hub is a managed message hub between your cloud and a large fleet of devices. It provides:

  • A device identity registry: every device is registered and authenticates with a symmetric key, an X.509 certificate or a TPM chip. The hub refuses unknown devices.
  • Device-to-cloud telemetry, cloud-to-device messages, direct methods (call a function on a device and get an answer) and device twins (JSON documents holding desired and reported properties, so you can configure a device even while it is offline).
  • Message routing: rules that send telemetry to different destinations such as a storage account, an Event Hubs endpoint or a Service Bus queue, based on a query.
  • A built-in endpoint that is compatible with Event Hubs, so stream processors can read telemetry directly.
Terminal
az extension add --name azure-iot
az iot hub create --name hub-demo-4821 --resource-group rg-iot --sku S1 --location westeurope
az iot hub device-identity create --hub-name hub-demo-4821 --device-id sensor-001
az iot hub device-identity connection-string show --hub-name hub-demo-4821 --device-id sensor-001

Device Provisioning Service (DPS) solves the factory problem: you cannot hand-register a million devices. A device ships with a certificate or key and the DPS address; at first boot it contacts DPS, which assigns it to the right hub (for example the one nearest its region) and registers it automatically. This is called zero-touch provisioning.

Azure Digital Twins sits one level higher. You describe the real world (buildings, floors, rooms, machines) as a graph of twins with the DTDL modeling language, and feed it from IoT Hub telemetry. Queries such as "all rooms on floor 3 hotter than 28 degrees" then work on the model, not on raw device messages.

Message routing: a stand-in

A route has a name, a query over message properties or the JSON body, and an endpoint. A message goes to every route whose query matches; if nothing matches, it falls through to the built-in endpoint. The stand-in implements a few simple conditions and routes invented telemetry.

JS Route device messages like IoT Hub

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that door-008 matches both the hot-alert rule and the door rule, so it is delivered twice: in real IoT Hub a message that matches several routes goes to all of them, and the catch-all route makes sure nothing is lost.

Putting the two together

A typical pipeline looks like this: sensors send telemetry to IoT Hub, a route forwards it to Event Hubs or storage, a stream or batch job turns it into features, an AML model scores it behind an endpoint, and alerts or dashboards show the result (see Azure Data and Analytics Overview for the processing stages). Secure the endpoints with private networking and managed identities, and watch the whole chain with Azure Monitor.

The wider Azure IoT and spatial family

IoT Hub is the messaging core, but Azure has other pieces for connected-device solutions:

  • Azure IoT Central is a managed application platform: you define device templates, dashboards and rules in a web UI instead of building the back end yourself.
  • Azure Sphere is a secured microcontroller platform: a certified chip, a custom Linux-based OS and a security service that delivers updates, for devices that must stay secure for years. (Check the current lifecycle status of the product before you design around it.)
  • IoT Edge runs containers, Functions and AI models on the device itself, so it can filter data, work offline and react locally.
  • Device Provisioning Service enrolls devices into IoT Hub automatically and at scale.
  • Azure Digital Twins models real-world environments (a building, a factory, a power grid) as a graph of twins, with relationships and live state fed from IoT data, so you can query "which rooms on floor 3 are above 28 degrees".
  • Azure Data Explorer stores and queries telemetry history; Stream Analytics filters it on the way in (see Event Grid and Event Hubs).

Microsoft Mesh sits at the edge of this family: a platform for shared mixed-reality experiences. Mesh-enabled apps let people in different places meet as avatars or holograms using HoloLens 2, VR headsets, PCs and phones (multi-device support), with spatial maps of the room, holographic rendering and multi-user state synchronization handled by the service. Most developers will never need it, but it shows how Azure combines sensors, twins and real-time collaboration.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is a compute cluster with a minimum of 0 nodes cost-effective for training?

    Scaling to zero means compute is billed only while jobs run.
  2. 2. What does an online endpoint with two deployments and a 90/10 traffic split let you do?

    A canary share exposes the new deployment to real requests with limited risk.
  3. 3. What problem does the Device Provisioning Service solve?

    DPS provides zero-touch provisioning so devices do not have to be registered by hand.
  4. 4. A device message matches two message routes. What does IoT Hub do?

    Messages are sent to all endpoints whose routing query matches; the built-in endpoint is used when none match.
  5. 5. What is a device twin?

    Twins let the cloud configure a device and read its state even when the device is offline.

Summary

  • An Azure Machine Learning workspace groups data, environments, compute, jobs, models and endpoints; clusters can scale to zero.
  • The designer and automated ML offer no-code starting points; code-first jobs give repeatability and control.
  • Online endpoints serve models over HTTPS and support traffic splits between deployments; batch endpoints score large datasets.
  • IoT Hub manages device identities, telemetry, twins, direct methods and message routing; DPS provisions devices automatically.
  • Digital Twins models the physical world as a graph fed by telemetry.

Next: Bicep and ARM Templates.