What is Azure IoT Hub and how do I use it?
Tags: azure, synpse, iot, cloud
Published: 2021-11-09 08:51:18

> Synpse is an end-to-end platform to manage your device fleet that can grow to hundreds of thousands of devices, perform OTA software updates, collect metrics, logs, deploy your containerized applications and facilitate tunnel-based SSH access to any of your device. You can find a [Quick Start here](https://docs.synpse.net/start-here/quick-start-web-user).

![Azure landing page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3yvcrvwzqi4dce4wc5a.png)

## Intro into Azure IoT Hub

We often get questions how Synpse is compared or competes with [Azure IoT Hub](https://azure.microsoft.com/en-us/overview/iot/) service. The short answer is that they operate in slightly different domains. Azure IoT hub focuses on application connectivity of the devices while Synpse targets deployment of the applications that may or may not be using services such as IoT Core.

The best results are achieved when solutions are used together. For example, when you build an application locally that utilizes AWS IoT Core message broker or device state services and then use Synpse to distribute your application to thousands of devices. 

## Example application


In this tutorial, we will deploy a simple open-source application that collects metrics and send them to AWS IoT Core for further processing. All code for this blog post can be found at:

[https://github.com/synpse-hq/metrics-nats-example-app](https://github.com/synpse-hq/metrics-nats-example-app) - Sample metrics application
[https://github.com/synpse-hq/azure-iot-hub-example](https://github.com/synpse-hq/azure-iot-hub-example) - Azure IoT Hub example

Steps:

1. Create Azure IoT hub
2. Configure rules to forward results into Azure blob storage
3. Create Azure "Thing"/IoT device for Synpse
4. Demo Synpse application from 3 microservices - Metrics demo, NATs messaging, Azure IoT python forwarder containers

## Technologies used

1. [Synpse](https://cloud.synpse.net) - manage devices and deploy applications to them
2. [NATs](https://nats.io/) - a lightweight message broker that can run on-prem
3. [Azure IoT Hub](https://azure.microsoft.com/en-gb/services/iot-hub/) - message broker between all devices and Azure 

> Contrary to [AWS](https://synpse.net/blog/clouds/aws-iot-core/), Azure was easy to configure using CLI. All steps here will be done using Azure CLI.

## Azure IoT Hub 

1. Create Azure IoT hub:
```bash
az iot hub create --resource-group MyResourceGroup --name MyIotHub --location eastus --tags synpse=true
```

2. Create certificate based on [Azure documentation](https://docs.microsoft.com/en-us/azure/iot-hub/tutorial-x509-self-sign).

Upload the certificates for Azure device by creating "device-identity"

```bash
Thumbprint 1: SHA1 Fingerprint=56:0E:78:56:74:F1:1B:60:73:AA:7C:8E:12:73:C4:62:01:D5:A3:10 
Thumbprint 2: SHA1 Fingerprint=81:EB:0B:27:BB:3F:BB:D8:91:AF:38:28:BE:83:E5:46:C3:0F:4D:DE

# remove colons ':' from the fingerprints

az iot hub device-identity create -n MyIotHub -d synpse --am x509_thumbprint --ptp "560E785674F11B6073AA7C8E1273C46201D5A310" --stp "81EB0B27BB3FBBD891AF3828BE83E546C30F4DDE"
```

For this example, we gonna create a message route to the storage account blob.

3. Create storage account:
```bash
az storage account create -n MyStorageAccountName -g MyResourceGroup -l eastus
```


4. Create container/bucket for results:
```bash
az storage container create --account-name MyStorageAccountName -n metrics
```

5. Create IoT hub endpoint for message routing:
```bash
storageConnectionString=$(az storage account show-connection-string --name MyStorageAccountName --query connectionString -o tsv)

az iot hub routing-endpoint create --resource-group MyResourceGroup --hub-name MyIotHub \
        --endpoint-name storage --endpoint-type azurestoragecontainer --endpoint-resource-group MyResourceGroup \
        --endpoint-subscription-id $(az account show | jq -r .id) --connection-string $storageConnectionString \
        --container-name metrics --batch-frequency 60 --chunk-size 10 \
        --ff {iothub}-{partition}-{YYYY}-{MM}-{DD}-{HH}-{mm}
```

6. Use routing in question with our HUB (endpoint name is same as --endpoint-name)
```bash
az iot hub route create -g MyResourceGroup --hub-name MyIotHub --endpoint-name storage --source-type DeviceMessages --route-name Route --condition true --enabled true
```

### Deploy an application

Deploy an application. Modify application YAML with your thing endpoint.

1. Create certificate secrets
```bash
synpse secret create azure-crt -f device1.crt
synpse secret create azure-key -f device1.key
```

Deploy the application. You will need to modify other values inside YAML file.
```bash
synpse deploy -f synpse-azure-example.yaml
```

where `synpse-azure-example.yaml` is

```yaml
name: Azure-IoT-Hub
description: Azure IoT Hub Synpse example
scheduling:
  type: Conditional
  selectors:
    # device selector
    azure: iot
spec:
  containers:
    - name: nats
      image: nats
      restartPolicy: {}
    - name: metrics
      image: quay.io/synpse/metrics-nats-example-app
      restartPolicy: {}
    - name: azure-iot
      image: quay.io/synpse/azure-iot-hub-example
      command: /server/azure.py
      env:
        - name: NATS_HOSTNAME
          value: nats
        - name: HOSTNAME
          # IoT hub DNS name
          value: "mj-hub.azure-devices.net"
        - name: DEVICE_ID
          # device id used when creating a device
          value: "synpse"
      secrets:
      - name: azure-crt
        filepath: /server/device1.crt
      - name: azure-key
        filepath: /server/device1.key
      restartPolicy: {}
```

You should see messages coming into the Azure IoT Hub

![message flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xejg3wpoelc9w626t62.png)

Once running, you should see the application running and data coming into Azure storage account blob.

![Message flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fcowa6qkjtm9hu796gp.png)


### Things to look for

Certificate configuration path is painful. But this is the common pattern between cloud offering. Azure offers shared private key authentication. This makes multiple device provisioning easier. If you are not interested in cloud portability - this might be easier way to deal with complexities.

Events land into a storage account without visibility how and the default data structure cannot easily adjust. Sometimes it takes a few minutes, sometimes multiple minutes. We assume this is due to the ETL (Extract, Transform, Load) process, which is not visible to users. **But on the other hand, on GCP it was very visible and VERY expensive. Not sure if both here are possible :)**

Overall Azure experience was quite pleasant. API and CLI consistent, documentation is quite well written. 

## go run ./wrap_up.go

This is a simple way to use Azure IoT Hub with Synpse. When it comes to consuming and managing a lot of data, constructing complex applications and integrating seamlessly into your current technological infrastructure - nothing can beat the public cloud. But where the cloud is lacking is - IoT device and application management.

Public cloud providers are built on assumption that they will manage infrastructure for you. When it comes to devices themselves - they are yours and yours only. And this is where Public cloud providers lack influence and where Synpse comes into the picture.

If you have any questions or suggestions, feel free to start a new discussion in our [forum](https://github.com/synpse-hq/synpse/discussions) or drop us a line on [Discord](https://discord.gg/dkgN4vVNdm)

Originally published at https://synpse.net/blog