Azure IoT Edge Modules – Getting Started

Overview

Azure IoT Edge lets you deploy containerized workloads—called modules—to edge devices, enabling cloud intelligence locally. This guide walks you through creating, building, and deploying your first IoT Edge module.

Prerequisites

Step 1 – Create the Module Project

Open a terminal and run:

dotnet new iotedge.module -n TemperatureSensor

Step 2 – Implement Business Logic

Edit Program.cs to simulate temperature readings:

using System;
using System.Text;
using System.Threading;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Client.Transport.Mqtt;

class Program
{
    static ModuleClient ioTHubModuleClient;
    static Timer timer;

    static void Main(string[] args)
    {
        Init().GetAwaiter().GetResult();
        Thread.Sleep(Timeout.Infinite);
    }

    static async Task Init()
    {
        ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(TransportType.Mqtt_Tcp_Only);
        await ioTHubModuleClient.OpenAsync();
        timer = new Timer(SendMessage, null, 0, 5000);
    }

    static async void SendMessage(object state)
    {
        var temperature = new Random().Next(20, 30);
        var messageString = $@"{{""temperature"":{temperature}}}";
        var message = new Message(Encoding.UTF8.GetBytes(messageString));
        await ioTHubModuleClient.SendEventAsync("output1", message);
    }
}

Step 3 – Build & Push the Docker Image

Replace <your‑acr> with your Azure Container Registry name.

az acr login --name <your-acr>
docker build -t <your-acr>.azurecr.io/temperature-sensor:1.0 .
docker push <your-acr>.azurecr.io/temperature-sensor:1.0

Step 4 – Deploy via IoT Hub

In the Azure portal, navigate to your IoT Hub → IoT Edge → your device → Set Modules. Add a new module with the image you just pushed and save the deployment.

Alternatively, use the CLI:

az iot edge set-modules --device-id <device-name> --hub-name <hub-name> --content deployment.json

Step 5 – Verify the Module

Open the Azure portal → IoT Edge → your device → Modules. You should see temperature-sensor running. Check the logs for temperature data.

IoT Edge dashboard

Next Steps

Explore related topics:

Comments