I am trying to automate setting up of IoT Hub. But I am not able to find any code related to creating a device in the IoT Hub. I found the below command:
resource “azurerm_iothub_device” “example” {
name = “device_name” iothub_name = azurerm_iothub.iothub12.name
}
But it does not seem to be valid.
I have installed the extensions azure IoT Hub and IoT Device Workbench.
How can I do it?
A workaround I use is to use a local provisioner. I use it in my iothub resource for creation. The downside is that it only works on resource creation (or deletion) - so if devices need to be updated at a later stage without recreating the IoT hub you would need to create a new resource with said provisioner.
My provisioner:
provisioner "local-exec" {
command = "${path.module}/create-iot-device.sh ${azurerm_iothub.<your-resource-name>.name}"
}
And the create-iot-device.sh
#!/bin/bash
az iot hub device-identity create --device-id dev-1 --hub-name $1
According to EliiseS, as of now, support for IoT Hub device creation using Terraform is not available.
Related
Assuming I have an Azure resource ID
Such as:
"/subscriptions/XXXXXXXX/resourceGroups/YYYYYY/providers/Microsoft.ZZZZ/WWWWW/my-resource-name"
How can I find out to which Vnets its connected/sits in using PowerShell commands?
It will be possible ,but you have to write lengthy code for it.(e.g If you have Network interface you need to use if block for it, so that it gets you the network interface subnetid and then again elseiffor any other resource ).
Azure portal provides a feature called connected device from their we can simply check the devices are connected to the V-net.
As shown here:
Using Powershell CMD if you get the virtual network you can determine the associated resources in the subnet :-
Background
I'm using Azure-cli (version >2.3.1) to create IoT Edge deployments:
echo az iot edge deployment create --content $(iotDeploymentPath) --deployment-id $(deploymentId) --hub-name ${hub} --priority $(priority) --target-condition "tags.location.place='$(env)'"
and for getting the metrics:
targetedCount="$(az iot edge deployment show-metric --deployment-id $(deploymentId) --metric-id targetedCount --hub-name ${hub})"
appliedCount="$(az iot edge deployment show-metric --deployment-id $(deploymentId) --metric-id appliedCount --hub-name ${hub})"
reportedSuccessfulCount="$(az iot edge deployment show-metric --deployment-id $(deploymentId) --metric-id reportedSuccessfulCount --hub-name ${hub})"
A few days ago, we got the following error message while trying to receive the deployment's metrics:
the metric 'targetedCount' is not defined in the device configuration
$(deploymentId)
the metric 'appliedCount' is not defined in the device configuration
$(deploymentId)
the metric 'reportedSuccessfulCount' is not defined in the device
configuration $(deploymentId)
with the following depracation message:
This extension 'azure-cli-iot-ext' is deprecated and scheduled for
removal. Please remove and add 'azure-iot' instead.
So I've installed 'azure-iot' instead of 'azure-cli-iot-ext', but now it seems like
az iot edge deployment show-metric stopped working and keeps sending the error message mentioned above.
the metric 'targetedCount' is not defined in the device configuration
$(deploymentId)
the metric 'appliedCount' is not defined in the device configuration
$(deploymentId)
the metric 'reportedSuccessfulCount' is not defined in the device
configuration $(deploymentId)
Question:
How can I get those metrics back without having the CLI deprecated extanstion version?
Quoting Azure support team answer:
I just received an update from our Product Group. They have informed
me that before azure-cli-iot-ext 0.8.7, iot edge deployment
show-metric would only work against system metrics. After, an argument
was introduced for the user to be able to switch between system and
user metrics but to match iot hub configuration show-metric, the user
metrics became the default (which carried over to azure-iot).
In addition, the PG member pointed out that all the metrics causing
issues with appear to be system metrics. In that case, please try
adding the --metric-type or –mt argument with value ‘system’.
For example:
az iot edge deployment show-metric -n myiothub -d mydeployment -m targetedCount --mt system
I have a device that is an mqtt client to Azure IoT Hub and want the devcie to be notified about changes/updates in the device-twin for that device.
I've successfully connected and updated the reported value in the device-twin from the device but can't seem to get an event when I change some of the desired properties in the device-twin from the Azure shell (https://shell.azure.com/) using a command like this:
az iot hub device-twin update -n <myIotHubBame> -d <myDeviceId> --set properties.desired='{"foo":"bar"}'
On the device I've tried subscribing to $iothub/twin/res/# and devices/<myDeviceId>/messages/devicebound/# but am not receiving any message after issuing the az command from the Azure shell.
I tried also setting a message route with Data Source = Twin Change Events and Endpoint = event but still no message being received in the device.
Anyone has a suggestion on how to get notification to the device when properties in the device-twin changes ?
The MQTT device needs to subscribe on the following topic
$iothub/twin/PATCH/properties/desired/#
for receiving a notification changes on the device twin desired properties.
When I follow these steps:
create event hub in a namespace with a resource group named tést (or other name with é in it)
Try to open up the event hub in the portal
Then the portal never finishes loading the event hub and I get this:
rainy cloud
I can reproduce this bug on different Azure subscriptions...
#SQLWaldorf,
I checked on this issue, when using the accent in Portal, it doesn't allow me to create the namespace since the character doesn't seem to be supported, I attached both screenshots from my experiment:
Then without accent:
I am trying to read the events that my device is sending. I am using azure npm lib to read the what i think is right.
Ok so first, under my Azure ioT Hub account for that service there is a tab call Messaging. There is something called "Event Hub-compatible name" and "Event Hub-compativle endpoint". Do i have to create a new Event hub with the name of "Event Hub-compatible name" or what? I am a bit confused :D
If not what is the connection string and topic and so on?
Here is how the code is now...
var azure = require('azure');
var serviceBusService = azure.createServiceBusService("Endpoint=XXXXXXXXXX.servicebus.windows.net/");
var isWaiting = false;
function waitForMessages(){
console.log("Checking Queue...");
isWaiting = true;
serviceBusService.receiveQueueMessage("messages","events",function (error, receivedMessage){
console.log(error);
console.log(receivedMessage);
isWaiting = false;
});
}
// Start messages listener
setInterval(function () {
if(!isWaiting){
waitForMessages();
}
}, 200);
The Event Hub-compatible name does not mean that you have to create a Event Hub with the same name.
IOT Hub provides an endpoint that is backward compatible with Event Hub API. I think the actual implementation is a little more complex, but you can think of IOT Hub as inheriting from or at least an implementation of Event Hubs. Use this Event Hub compatible name with any Event Hub SDKs or code examples as part of the connection string.
For explaining the concepts of Event Hub-compatible name & Event Hub-compatible endpoint, you can refer to the section How to read from Event Hubs-compatible endpoints of the offical doc Azure IoT Hub developer guide. You can use the Azure Service Bus SDK for .NET or the Event Hubs - Event Processor Host to read events from IoT Hub in C#.
Otherwise, there are two Azure IoT SDKs for NodeJS using connection string: Azure IoT Service SDK (API Reference) & Azure IoT Device SDK (API Reference).
The connection string that you can find it at the one policy of the tab Shared Access Policies in the All settings, please see the pic below from the doc Tutorial: Get started with Azure IoT Hub.
According to your needs for reading events from IoT Hub, you can follow these samples to code using Azure IoT SDKs for NodeJS.
Using Azure IoT Service SDK to list the deviceIds registed in your IoT Hub, please see the sample https://github.com/Azure/azure-iot-sdks/blob/master/node/service/samples/registry_sample.js.
Using Azure IoT Device SDK to monitor events from IoT Hub, please see the sample https://github.com/Azure/azure-iot-sdks/blob/master/node/device/samples/remote_monitoring.js.
Hope it helps. Any concern, please feel free to let me know.
You can use the Event Hubs SDK for Node.js to see events/messages sent by your device to your IoT Hub:
https://www.npmjs.com/package/azure-event-hubs
The client object of the event hubs SDK can accept an IoT Hub connection string, so you don't need to use the Event Hubs connection string.
If you're just trying to debug your device and want to verify that it's actually sending messages, you can use the tool called iothub-explorer provided with the Azure IoT Hub SDK:
https://github.com/Azure/azure-iot-sdks/tree/master/tools/iothub-explorer
Also a couple of clarifications regarding the previous answer: the Service SDK allows to send messages to devices, and to read the "feedback" message that the device sends, which is used to know if the device accepted or rejected the command message but contains no data. It doesn't help to read data events sent by the device.
Connect to the IoT Hub to receiving the data:
var protocol = 'amqps';
var eventHubHost = '{your event hub-compatible namespace}';
var sasName = 'iothubowner';
var sasKey = '{your iot hub key}';
var eventHubName = '{your event hub-compatible name}';
var numPartitions = 2;
Protocol
var protocol = 'amqps';
It is the Event Hub-compatible endpoint: sb://abcdefnamespace.servicebus.windows.net/
but without the sb:// and .service windows.net/
Like that: abcdefnamespace
var eventHubHost = '{your event hub-compatible namespace}';
Its so OK
var sasName = 'iothubowner';
Primary key, like this: 83wSUdsSdl6iFM4huqiLGFPVI27J2AlAkdCCNvQ==
var sasKey = '{your iot hub key}';
Name like this: iothub-ehub-testsss-12922-ds333s
var eventHubName = '{your event hub-compatible name}';
Its so OK
var numPartitions = 2;
Every Iot Hub instance comes with a built in endpoint that is compatible with Event Hubs. The Event Hub compatible endpoint you see in the Azure portal would be the connection string pointing to the Event Hub instance from where you can read all the messages sent to your Iot Hub instance.
You can use this connection string to the instantiate the EventHubConsumerClient class from the #azure/event-hubs library and read your messages.
In the event of a failover, it is said that this backing built-in endpoint would change. So, you would have to fetch the new connection string and restart the process. Another option is to use the sample code in the azure-iot-samples-node repo to get the Event Hub compatible connection string by passing in the Iot Hub connection string and then use the #azure/event-hubs library to read your messages. See more in Read from the built-in endpoint