- A Cloud Guru
Create an Azure IoT Edge Environment
Running cloud services on Edge devices can address a number of different challenges and scenarios, including intermittent internet connectivity, lower latency event architectures, and preprocessing data to reduce cloud storage and processing costs. In this lab, you will create an Azure IoT Edge environment on a Linux virtual machine to demonstrate how to deploy base Edge components. Then, you will configure and deploy 2 modules to your Edge device: a simulated temperature sensor and an Azure Stream Analytics job that will filter data generated by the sensor. You will get the most from this lab if you are already familiar with Azure IoT Hub infrastructure and workflows.
Table of Contents
Register Edge Device in Azure IoT Hub
From within the Azure portal:
- Navigate to the Azure IoT Hub already deployed into your lab subscription.
- Add an Edge device to the hub registry using symmetric key authentication; name it anything you like.
- Copy the Primary Connection String from the device registration and save it off for use in the next objective.
Deploy Azure IoT Edge Runtime to Linux VM and Connect to Hub
The VM you will use for your Edge device is already deployed into the lab subscription, and it also has required prerequisites for the Azure IoT Edge Runtime. The most notable prerequisite is a supported Ubuntu 18.04 OS and the Moby container runtime, which is required to run the Edge modules, all of which are deployed as Docker containers.
From within the Azure portal:
- Launch Cloud Shell choosing the Bash environment when prompted.
- When prompted to initialize storage for Cloud Shell, select Advanced settings, and add the storage account and file share. Note that the storage account name must be universally unique; you might want to use the random characters from the beginning of the resource group name as a part of your storage account name.
At the terminal command prompt:
- Use SSH to log in to your VM using the username, public IP, and password provided in the lab metadata. Answer in the affirmative to any questions; your response is case-sensitive. Your login will be in this form:
ssh cloud_user@[labPublicIP](ssh is lower-case).
- When prompted for the password, type or copy the password from the lab metadata. Note that the cursor will not move to show the number of characters or the password.
- Install the Azure IoT Edge runtime using this command:
sudo apt-get install aziot-edge.
- If prompted, enter "Y" to continue.
- Create and populate a configuration file that will be used, in part, to connect to the Azure IoT Hub. In the command below, replace the placeholder value for the connection-string argument with the connection string you copied from the device registration:
sudo iotedge config mp --connection-string 'PASTE_CNX_STRING'. Tip: Be sure your connection string is wrapped in single quotes.
- Verify that the config file was created and that it contains the connection string by viewing the file:
sudo nano /etc/aziot/config.toml.
CTRL-Xto close the file and return to the command prompt.
- Apply the configuration to your runtime:
sudo iotedge config apply -c '/etc/aziot/config.toml'.
- Check the status of the IoT Edge Runtime to ensure all is running or ready:
sudo iotedge system status.
- Confirm that 1 of the 2 runtime modules, Edge Agent, is installed (you will install the other runtime module, Edge Hub, as a part of the other module deployments):
sudo iotedge list.
- Minimize the terminal window. If it times out before you get back to use it, simply log back in using SSH.
- In the Azure portal, return to the device registration page, and scroll to find the modules section. You should see the Edge Agent running. Refresh as necessary. Don't worry about any error-like messages you see in the IoT Edge Runtime Response property, slightly up the page from the modules section. That will clear up after the Edge Hub is deployed.
Configure Azure Stream Analytics Edge Job
In the Azure portal:
- Navigate to the skeletal Azure Stream Analytics job already set up for you. It is called ASAEdgeJob. You can see on the Overview page that this job does not yet have any inputs or outputs, and the query is just a placeholder.
- In the ASA job, create a stream input on Edge Hub with an alias of simTemperature.
- Create an output on Edge Hub (both the input and the output are on the Edge Hub runtime module, which acts as the broker). The alias for this output should be filteredTelemetry. Tip: Take note of the names of the ASA job and the input and the output aliases. You will need them in the next objective.
- The query acts as a transformation or filter over telemetry data that will be coming from the Simulated Temperature Module you will be deploying in the next objective. Alter the placeholder query so that it references the input and output aliases you just created.
- Set a filter on the temperature returned by the machine whenever the temperature is greater than 70. Hint: See the Additional Resources section of this lab for tips around how to query JSON messages and a sample message to help you build your query.
- Save the query.
- Associate the ASA job with the storage account already deployed with the lab. Tip: Make sure you select the account that was deployed with the lab, not the storage account you created to back your Cloud Shell. The save button will not be enabled if you select the Cloud Shell storage.
- Publish the job in order to stage it into a blob container for deployment in the next objective. This may take a few minutes. The system will set up a couple of containers in the storage account as a part of the staging process.
Configure Module and Routing Deployment Manifest
- Back in the IoT Edge device explorer, select your Edge device, and run the Set Modules wizard. This wizard builds a deployment manifest, which you will review before deploying the modules.
- Add the SimulatedTemperatureModule from the Marketplace.
- Add the Azure Stream Analytics module you staged in the the last objective. Note that the name of the module defaults to the same name as the job: ASAEdgeJob. UI Quirk: If the Save button is disabled, go back to the dropdown and explicitly select the job; the Save button should then be enabled.
- Change runtime settings, updating (or confirming) that the Image URI version for both Edge Agent and Edge Hub is 1.2. Do not alter any other values. This is required because the runtime you just installed is version 1.2.
- Continue to Routes and remove the default route named route (if it exists). Tip: To complete the next 2 steps, refer to the Additional Resources section of the lab to understand routing expression syntax.
- The SimulatedTemperatureSensorToIoTHub route was created by default when you added the module. This route sends all messages from the Simulated Temperature Module to the IoT Hub in the cloud, which is not what we want. Replace this route with one named SimulatedTempToASA and send all data from the simulated sensor to the Azure Stream Analytics module using the simTemperature input on the ASA module. Tip: You can rename and edit the route that was created or delete it and create a new route.
- Create a route that sends data from the ASA module, through the filteredTelemetry output, to the Azure IoT Hub in the cloud. Name the route FilteredDataToHub.
- Review the deployment manifest and go back to fix anything that doesn't look right. **Note: ** There may be a few places, particularly in route expressions, where characters, such as quotes, are escaped. That's okay and expected.
- Deploy (Create) the modules.
Verify Running Edge Environment
- Back on the device details page, verify modules and their status in the same location as you did after installing the runtime on the VM. **Note: ** Module deployment requires polling by the device, so this can take a few minutes. Refresh, as needed. Watch for the IoT Edge Runtime Response property to show a response code of "200 — OK" and refresh one more time if the modules still show errors.
- In the terminal window, log back in to the VM, if necessary, and run the same command you ran, previously, to see the list of modules. This time, instead of just the Edge Agent, you should see the Edge Hub, the SimulatedTemperatureSensor, and the ASAEdgeJob modules.
- Back on the IoT Hub overview page, you should also eventually see that messages are flowing to the hub.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.