Quickstart: Deploy your first IoT Edge module from the Azure portal to a Windows device

In this quickstart, use the Azure IoT Edge cloud interface to deploy prebuilt code remotely to an IoT Edge device. To accomplish this task, first create and configure a Windows virtual machine to work as an IoT Edge device, then you can deploy a module to it.

In this quickstart you learn how to:

  1. Create an IoT Hub.
  2. Register an IoT Edge device to your IoT hub.
  3. Install and start the IoT Edge runtime on your device.
  4. Remotely deploy a module to an IoT Edge device and send telemetry to IoT Hub.

Diagram - Quickstart architecture for device and cloud

The module that you deploy in this quickstart is a simulated sensor that generates temperature, humidity, and pressure data. The other Azure IoT Edge tutorials build upon the work you do here by deploying modules that analyze the simulated data for business insights.

If you don't have an active Azure subscription, create a free account before you begin.

Open Azure Cloud Shell

Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browser. Azure Cloud Shell allows you to use either bash or PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell comes pre-installed with the commands to allow you to run the content of this article without having to install anything on your local environment.

To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. Pasted text is not automatically executed, so press Enter to run code.

You can launch Azure Cloud Shell with:

Option Example/Link
Select Try It in the upper-right corner of a code block. This doesn't automatically copy text to Cloud Shell. Example of Try It for Azure Cloud Shell
Open Azure Cloud Shell in your browser.
Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Cloud Shell button in the Azure portal

You use the Azure CLI to complete many of the steps in this quickstart, and Azure IoT has an extension to enable additional functionality.

Add the Azure IoT extension to the cloud shell instance.

az extension add --name azure-cli-iot-ext

Prerequisites

Cloud resources:

  • A resource group to manage all the resources you use in this quickstart.

    az group create --name IoTEdgeResources --location westus2
    

IoT Edge device:

  • A Windows virtual machine to act as your IoT Edge device. You can create this virtual machine using the following command, replacing {password} with a secure password:

    az vm create --resource-group IoTEdgeResources --name EdgeVM --image MicrosoftWindowsDesktop:Windows-10:rs5-pro:latest --admin-username azureuser --admin-password {password} --size Standard_DS1_v2
    

    It may take a few minutes to create and start the new virtual machine. You can then download an RDP file for use when connecting to your virtual machine:

    1. Navigate to your new Windows virtual machine in the Azure portal.
    2. Select Connect.
    3. On the RDP tab, select Download RDP File.

    Open this file with Remote Desktop Connection to connect to your Windows virtual machine using the administrator name and password you specified with the az vm create command.

Note

This quickstart uses a Windows desktop virtual machine for simplicity. For information about which Windows operating systems are generally available for production scenarios, see Azure IoT Edge supported systems.

Create an IoT hub

Start the quickstart by creating an IoT hub with Azure CLI.

Diagram - Create an IoT hub in the cloud

The free level of IoT Hub works for this quickstart. If you've used IoT Hub in the past and already have a free hub created, you can use that IoT hub. Each subscription can only have one free IoT hub.

The following code creates a free F1 hub in the resource group IoTEdgeResources. Replace {hub_name} with a unique name for your IoT hub.

az iot hub create --resource-group IoTEdgeResources --name {hub_name} --sku F1

If you get an error because there's already one free hub in your subscription, change the SKU to S1. If you get an error that the IoT Hub name isn't available, it means that someone else already has a hub with that name. Try a new name.

Register an IoT Edge device

Register an IoT Edge device with your newly created IoT Hub. Diagram - Register a device with an IoT Hub identity

Create a device identity for your simulated device so that it can communicate with your IoT hub. The device identity lives in the cloud, and you use a unique device connection string to associate a physical device to a device identity.

Since IoT Edge devices behave and can be managed differently than typical IoT devices, declare this identity to be for an IoT Edge device with the --edge-enabled flag.

  1. In the Azure cloud shell, enter the following command to create a device named myEdgeDevice in your hub.

    az iot hub device-identity create --device-id myEdgeDevice --hub-name {hub_name} --edge-enabled
    

    If you get an error about iothubowner policy keys, make sure that your cloud shell is running the latest version of the azure-cli-iot-ext extension.

  2. Retrieve the connection string for your device, which links your physical device with its identity in IoT Hub.

    az iot hub device-identity show-connection-string --device-id myEdgeDevice --hub-name {hub_name}
    
  3. Copy the value of the connectionString key from the JSON output and save it. This value is the device connection string. You'll use this connection string to configure the IoT Edge runtime in the next section.

    Retrieve connection string from CLI output

Install and start the IoT Edge runtime

Install the Azure IoT Edge runtime on your IoT Edge device and configure it with a device connection string. Diagram - Start the runtime on device

The IoT Edge runtime is deployed on all IoT Edge devices. It has three components. The IoT Edge security daemon starts each time an IoT Edge device boots and bootstraps the device by starting the IoT Edge agent. The IoT Edge agent manages deployment and monitoring of modules on the IoT Edge device, including the IoT Edge hub. The IoT Edge hub handles communications between modules on the IoT Edge device, and between the device and IoT Hub.

The installation script also includes a container engine called Moby that manages the container images on your IoT Edge device.

During the runtime installation, you're asked for a device connection string. Use the string that you retrieved from the Azure CLI. This string associates your physical device with the IoT Edge device identity in Azure.

Connect to your IoT Edge device

The steps in this section all take place on your IoT Edge device, so you want to connect to that virtual machine now via remote desktop.

Install and configure the IoT Edge service

Use PowerShell to download and install the IoT Edge runtime. Use the device connection string that you retrieved from IoT Hub to configure your device.

  1. If you haven't already, follow the steps in Register a new Azure IoT Edge device to register your device and retrieve the device connection string.

  2. Run PowerShell as an administrator.

    Note

    Use an AMD64 session of PowerShell to install IoT Edge, not PowerShell (x86). If you're not sure which session type you're using, run the following command:

    (Get-Process -Id $PID).StartInfo.EnvironmentVariables["PROCESSOR_ARCHITECTURE"]
    
  3. The Deploy-IoTEdge command checks that your Windows machine is on a supported version, turns on the containers feature, downloads the moby runtime, and then downloads the IoT Edge runtime.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; `
    Deploy-IoTEdge -ContainerOs Windows
    
  4. Your machine may restart automatically. If you are prompted by the Deploy-IoTEdge command to reboot, do so now.

  5. Run PowerShell as an administrator again.

  6. The Initialize-IoTEdge command configures the IoT Edge runtime on your machine. The command defaults to manual provisioning with Windows containers.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; `
    Initialize-IoTEdge -ContainerOs Windows
    
  7. When prompted for a DeviceConnectionString, provide the string that you copied in the previous section. Don't include quotes around the connection string.

View the IoT Edge runtime status

Verify that the runtime was successfully installed and configured.

  1. Check the status of the IoT Edge service.

    Get-Service iotedge
    
  2. If you need to troubleshoot the service, retrieve the service logs.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; Get-IoTEdgeLog
    
  3. View all the modules running on your IoT Edge device. Since the service just started for the first time, you should only see the edgeAgent module running. The edgeAgent module runs by default, and helps to install and start any additional modules that you deploy to your device.

    iotedge list
    

    View one module on your device

It may take a few minutes for the installation to complete and the IoT Edge agent module to start.

Your IoT Edge device is now configured. It's ready to run cloud-deployed modules.

Deploy a module

Manage your Azure IoT Edge device from the cloud to deploy a module that sends telemetry data to IoT Hub. Diagram - deploy module from cloud to device

One of the key capabilities of Azure IoT Edge is being able to deploy code to your IoT Edge devices from the cloud. IoT Edge modules are executable packages implemented as containers. In this section, you deploy a pre-built module from the IoT Edge Modules section of the Azure Marketplace.

The module that you deploy in this section simulates a sensor and sends generated data. This module is a useful piece of code when you're getting started with IoT Edge because you can use the simulated data for development and testing. If you want to see exactly what this module does, you can view the simulated temperature sensor source code.

To deploy your first module from the Azure Marketplace, use the following steps:

  1. In the Azure portal, enter Simulated Temperature Sensor into the search and open the Marketplace result.

    Simulated Temperature Sensor in Azure portal search

  2. Choose an IoT Edge device to receive this module. On the Target Devices for IoT Edge Module page, provide the following information:

    1. Subscription: select the subscription that contains the IoT hub you're using.

    2. IoT Hub: select the name of the IoT hub you're using.

    3. IoT Edge Device Name: if you used the suggested device name earlier in this quickstart, enter myEdgeDevice. Or, select Find Device to choose from a list of IoT Edge devices in your IoT hub.

    4. Select Create.

  3. Now that you've chosen an IoT Edge module from the Azure Marketplace, and chosen an IoT Edge device to receive the module, you're taken to a three-step wizard that helps you define exactly how to deploy the module. In the Add Modules step of the wizard, notice that the SimulatedTemperatureSensor module is autopopulated. In the tutorials, you use this page to add additional modules to your deployment. For this quickstart, only deploy this one module. Select Next to continue to the next step of the wizard.

  4. In the Specify Routes step of the wizard, you define how messages are passed between modules and to IoT Hub. For the quickstart, you want all messages from all modules to go to IoT Hub ($upstream). If it's not autopopulated, add the following code then select Next:

     {
     "routes": {
         "route": "FROM /messages/* INTO $upstream"
         }
     }
    
  5. In the Review Deployment step of the wizard, you can preview the JSON file that defines all the modules that get deployed to your IoT Edge device. Notice that the SimulatedTemperatureSensor module is included, and two additional system modules called edgeAgent and edgeHub. Select Submit when you're done reviewing.

    When you submit a new deployment to an IoT Edge device, nothing is pushed to your device. Instead, the device queries IoT Hub regularly for any new instructions. If the device finds an updated deployment manifest, it uses the information about the new deployment to pull the module images from the cloud then starts running the modules locally. This process may take a few minutes.

  6. After you submit the module deployment details, the wizard returns you to the IoT Edge page of your IoT hub. Select your device from the list of IoT Edge devices to see its details.

  7. On the device details page, scroll down to the Modules section. Three modules should be listed: $edgeAgent, $edgeHub, and SimulatedTemperatureSensor. If one or more of the modules are listed as specified in deployment but not reported by device, your IoT Edge device is still starting them. Wait a few moments and select Refresh at the top of the page.

    View SimulatedTemperatureSensor in list of deployed modules

View generated data

In this quickstart, you registered an IoT Edge device and installed the IoT Edge runtime on it. Then, you used the Azure portal to deploy an IoT Edge module to run on the device without having to make changes to the device itself.

In this case, the module that you pushed creates sample data that you can use for testing. The simulated temperature sensor module generates environment data that you can use for testing later. The simulated sensor is monitoring both a machine and the environment around the machine. For example, this sensor might be in a server room, on a factory floor, or on a wind turbine. The message includes ambient temperature and humidity, machine temperature and pressure, and a timestamp. The IoT Edge tutorials use the data created by this module as test data for analytics.

Confirm that the module deployed from the cloud is running on your IoT Edge device.

iotedge list

View three modules on your device

View the messages being sent from the temperature sensor module to the cloud.

iotedge logs SimulatedTemperatureSensor -f

Tip

IoT Edge commands are case-sensitive when referring to module names.

View the data from your module

You can also watch the messages arrive at your IoT hub by using the Azure IoT Hub Toolkit extension for Visual Studio Code (formerly Azure IoT Toolkit extension).

Clean up resources

If you want to continue on to the IoT Edge tutorials, you can use the device that you registered and set up in this quickstart. Otherwise, you can delete the Azure resources that you created to avoid charges.

If you created your virtual machine and IoT hub in a new resource group, you can delete that group and all the associated resources. Double check the contents of the resource group to make sure that there's nothing you want to keep. If you don't want to delete the whole group, you can delete individual resources instead.

Remove the IoTEdgeResources group.

az group delete --name IoTEdgeResources

Next steps

In this quickstart, you created an IoT Edge device and used the Azure IoT Edge cloud interface to deploy code onto the device. Now, you have a test device generating raw data about its environment.

The next step is to set up your local development environment so that you can start creating IoT Edge modules that run your business logic.