A couple of months ago, I wrote an article “Deploy IoT Edge modules at scale with layered deployments“. In that article, I created a layered deployment manually. But what if you have multiple IoT hubs, should we configure each IoT hub by hand? I hope not, so this was the starting point in my search for integrating the (layered) deployments into Infrastructure as code (IaC).

Bicep

The first thing I looked into was Bicep, because this connected nicely with my article “Setting up a IoT hub with Bicep” and in my mind this was the most obvious solution. Microsoft has enabled almost all Azure resources though Bicep.

Indeed, almost everything, except (layered) deployments.

Azure CLI

After an online search, I found an article from Microsoft to deploy (layered) deployments through Azure CLI. Best part of this, is that it also can be integrated to an Azure DevOps pipeline.

So, let’s try to add a layered deployment to the solution of article “Deploy IoT Edge modules at scale with layered deployments“.

Deployment manifest

A deployment manifest is mandatory for automatically creating a (layered) deployment. This JSON file describes all the settings like modules, routes and desired properties. Because we have already created a layered deployment in the previous article, we can easily use the deployment manifest of one of those (layered) deployments as base.

Open the Azure portal and navigate to the IoT hub. Select “Configurations + Deployment” (no. 1 in the screenshot below) and then select a layered deployment, in our case I want to create a copy of “motionsensor_deployment” (no. 2 in the screenshot below).

I stored the file in a new VS Code project and renamed the file to motionsensor_deployment_v2.json because I want to deploy a new layered deployment. I added my deployment manifest in the screenshot below.

Normally, you will change the downloaded manifest to your needs. But in this example, I don’t want to change anything. I just want to add a layered deployment to the IoT hub, to see that it is working.

Deploy to Azure

To create the layered deployment in Azure, we need to execute an Azure CLI command. So, I open a terminal in VS Code via the menu (see the screenshot below) or by using the shortcut Ctrl+Shift+`.

Now, we can create the layered deployment by executing the command:

az iot edge deployment create --layered --deployment-id <ID of the layered deployment> --hub-name <Name of the IoT hub> --content <Filepath of the deployment manifest> --target-condition "<Target condiftion>" --priority <Priority>

In our example, the command will look like this:

az iot edge deployment create --layered --deployment-id motionsensor_deployment_v2 --hub-name ih-demodeployment --content motionsensor_deployment_v2.json --target-condition "tags.motion = true" --priority 101

Notice that I use --layered, this is only necessary for layered deployments.

Check

To check if the deployment was successful, I go back to the IoT hub in the Azure portal. In the “Configurations + Deployments” overview, I see the new deployment:

Conclusion

Here, my journey for integrating the (layered) deployments into Infrastructure as code (IaC) ended. It is possible to integrate the (layered) deployments into your Azure DevOps pipeline by using Azure CLI. By automating this step, it makes your IoT hub deployments repeatable and more consistent.