How to trigger an Azure Function from an IoT Central Rule? - azure-iot-central

I am using Microsoft IoT Central and use its rules to trigger actions.
One can choose a number of actions when the rule is triggered like email, Webhook, Logic Apps as well as Power Automate. I have tried using all of them without any issues. Now I trying to define a simple NodeJS Azure function that shall be run whenever the IoT Central Rule is triggered.
What I don't understand is how to set it up end-to-end using an Azure Function. One cannot choose any existing function from the IoT Central action on rule menu, but are instead pointed to creating a new function.
When I try that from within Azure there is not any "IoT Central" function template as there is for for example IoT Hub or HTTP Trigger.
So - how do I "connect" a new Azure function to be triggered from the IoT Central rule?

Triggering an Azure Function from IoT Central is done by creating a Function with an HTTP trigger and using the Webhook action to trigger it.
The process is documented here
Update:
I see in your screenshot that you are using an older version of IoT Central. In that version, Azure Functions is mentioned as a separate action, but in reality it was just using a webhook as well.
Azure Functions as a separate action was removed in a later version when you select Webhook, it's mentioned as an option (as in the above screenshot).

Related

Best way to design Azure Functions to read events from more than one event hub

I want to design azure functions in Python language to read events from more than one event hub . The number of event hubs are not constant , it can increase or decrease (for example one event hub publishes logs from one azure AD tenant and in future AD tenants can be added). Each function will have same business logic ,only the even hub connection string will differ .
Event hub triggered function can use only one event hub , not more than more .
So, What is the best approach for this problem statement :
Creating 1 Event hub triggered function for each event hub and keep all functions under one azure function APP and to have shared code which can be used by all functions .
Cons : If a new event hub added , need to create a new function and deploy function standalone with Zero downtime of function APP ,which is not possible .As per azure doc ,the deployment unit should be Function APP, not individual functions .
Creating 1 function APP with single Event hub triggered function
subscribing to one event hub .
Pros As new function app is different for each event hub, if a
new event hub added other function apps won't be impacted and zero
downtime for them
Challenge Assuming we have one Github repo with function
logic, function.json , Is it possible to create a function app each
time a new event hub added and create a deployment integrating with
git repo. In future I want to use azure pipelines or Github actions
for CI/CD , will this approach create any issue.
If the business logic is exactly the same, then you can extract the core logic and put it in an http trigger function. You only need to create multiple Azure logic apps to trigger and pass the content of the event hub to the function for processing.
This way you don't have to worry about the downtime caused by multiple deployments, and I think it is easier to create multiple Azure logic apps than to create multiple Azure functions.
You can refer to my logic app design:

GCP: Is it possible for a Cloud function to trigger when an Alert fires?

I am using Google Cloud Monitoring of Google Cloud Platform.
I have created some alert policies for objects that I monitor. However , When there is an alert that fires a, there are some pieces of information that are not included that I want included into the email. So I am thinking to use a cloud function that will trigger upon one of the policies I have created if that is possible to do in this case .
If it is possible please provide advice in this issue.
Cloud Monitoring supports using Pub/Sub as a notification channel: https://cloud.google.com/monitoring/support/notification-options#pubsub
You should be able to write a Cloud Function that acts as a Pub/Sub trigger to respond to these events: https://cloud.google.com/functions/docs/calling/pubsub

How to route Event Hub messages to different Azure functions based on their message type

I have an Azure Event Hub over which I would like to send various types of messages. Each message should be handled by a separate Azure Function, based on their message type. What is the best way to accomplish this?
Actually, I could create some JSON container with a type and payload property and let one parent Azure Function dispatch all the messages payloads - based on their type - to other functions, but that feels a bit hacky.
This question basically asks the same - however it is answered how it can be done using the IoT Hub and message routing. In the Event Hub configuration I cannot find any setting to configure message routing though.
Or should I switch to an Azure Message Queue to get this functionality?
I would use Azure Streaming Analytics to route it to the different Azure Functions. ASAs allow you to specify Event Hubs as a source and several sinks (one of which can be multiple Azure Functions). You can read more about setting up Azure Streaming Analytics services through the Azure Portal here. You'll need to set up the Event Hub as your source (docs). You'll also need to set up your sink (docs). You write some MS SQL-like code to route the messages to the various sinks. However, ASAs are costly relative to other services since you're paying for a fixed amount of compute.
I put some pseudo code below. You'll have to swap it out based on how you configure you're ASA using the information from the attached MS Documentation.
SELECT
*
INTO
[YourOutputAlias]
FROM
[YourInputAlias]
HAVING
[CONDITION]
SELECT
*
INTO
[YourAlternateOutputAlias]
FROM
[YourInputAlias]
HAVING
[CONDITION]
Based on your additional info about the business requirements and assuming that the event size < 64KB (1MB in preview), the following screen snippet shows an example of your solution:
The concept of the above solution is based on the pushing a batch of the events to the Event Domain Endpoint of the AEG. The EventHub Trigger function has a responsibility for mapping each event message type in the batch to the domain topic before its publishing to the AEG.
Note, that using the Azure IoT Hub for ingestion of the events, the AEG can be directly integrated to the IoT Hub and each event message can be distributed in the loosely decoupled Pub/Sub manner. Besides that, for this business requirements can be used the B1 scale tier for IoT Hub ($10/month) comparing to the Basic Event Hubs ($11.16).
The IoT Hub has built-in a message routing mechanism (with some limitations), but a recently new feature of the IoT/AEG integration such as publishing a device telemetry message is giving a good support in the serverless architecture.
I ended up using Azure Durable Functions using the Fan Out/Fan In pattern.
In this approach, all events are handled by a single Orchestrator Function which in fact is a Durable Azure Function (F1). This deserializes incoming JSON to the correct DTO. Based on the content of the DTO, a corresponding activity function (F2) is invoked which processes it.

v2 Azure Function with Service Bus trigger not firing

I am using Azure Functions V2 with a Service Bus trigger using 1.0.23 of the C# Functions SDK. I'm using the following approach to get secrets from KeyVault and use them within the settings of the triggers: How to map Azure Functions secrets from Key Vault automatically
The function, especially when it has done nothing for a while, doesn't fire when there are messages on the subscription. If I then go to the portal and execute manually (yes, that particular execution is fired with a null message) it kicks it into life and picks up the other messages on the queue and processes them correctly.
This obviously isn't ideally for our automated tests. Has anybody seen this, or know of anything that will help?
Also, the Function App is running on a consumption plan.
App Service Plan
If you're using App Service plan then it's simple, just make use of Always on
Consumption Plan
If you're using Consumption plan, the issue could be that your triggers did not sync properly with the Azure Infrastructure (Central Listener). It could have happened due to the way you deployed/edited your trigger related settings as explained in issue #210 below.
When you access the function directly from Portal, it might be forcing your function app to come alive, but as you can see that's only a workaround. Something similar is mentioned here
Take a look at these issues:
Service Bus Topic Trigger goes to sleep - Consumption Plan
They also mention that it wakes up only on accessing it via the portal or calling a HTTP triggered function in the same app, which is similar to the behavior you are seeing.
Issue #210
Issue #681
There are 3 suggested ways to resolve it, mentioned as part of Issue #210 above
In order to synchronize triggers when these deployment options are
used, open the Azure Portal and click the Refresh button, or make a
API call to the sync triggers endpoint:
https://github.com/davidebbo/AzureWebsitesSamples/blob/master/ARMTemplates/FunctionsWebDeploy.json#L90
Powershell sample:
https://github.com/davidebbo/AzureWebsitesSamples/blob/master/PowerShell/HelperFunctions.ps1#L360-L365
I've had a similar issue. ServiceBus connection was injected using ServiceBus value in ConnectionStrings section of Function configuration. This is enough when Function is in hot state but after transitioning to cold state AzureWebJobsServiceBus value is used to connect to service bus. So in my case setting AzureWebJobsServiceBus to ServiceBus connection string in Function configuration fixed this.

Is it possible to receive an event when a new resource is created

I would like to run some automation to take some actions whenever a new resource is created in One of my subscriptions in Azure. Is this possible?
You can use Azure Event grid for this, a solution that is built for exactly this purpose. Taken from https://learn.microsoft.com/en-us/azure/event-grid/overview
Azure Event Grid allows you to easily build applications with event-based architectures. You select the Azure resource you would like to subscribe to, and give the event handler or WebHook endpoint to send the event to. Event Grid has built-in support for events coming from Azure services, like storage blobs and resource groups.
You basically create a subscription to a certain event (such as specific resources being created) that can then be picked up by a webhook, or processed by Azure Automation or Azure Functions

Resources