0
votes

Requirement Create a Azure Function which monitors all the incoming messages to IoT Hub and send it Cosmos database after doing some modifications to messages

Architecture

enter image description here

Issue: Unable to create a Azure function which reads the incoming messages to IoT Hub and sends to Cosmos DB

Steps Followed

I have followed below steps to create and deploy the Azure Function App using Visual Studio Code

  • Open Visual Studio Code
  • Go to Azure Extension Panel

    enter image description here

  • Selected the C# Language

    enter image description here

  • It created following files

    • .vscode
    • .gitignore
    • FunctionApps.csproj
    • host.json
    • localsettings.json
  • Now I have clicked "Create Function" button in Visual Studio Code and selected CosmosDBTrigger and mapped the CosmoDB which is already created in Azure portal. which generated below c# class

    namespace Company.Function
    {
     public static class CosmosDBTriggerCSharp
     {
     [FunctionName("CosmosDBTriggerCSharp")]
     public static void Run([CosmosDBTrigger(
        databaseName: "databaseName",
        collectionName: "collectionName",
        ConnectionStringSetting = "dev-test-cosmo-db_DOCUMENTDB",
        LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
        {
        if (input != null && input.Count > 0)
        {
            log.LogInformation("Documents modified " + input.Count);
            log.LogInformation("First document Id " + input[0].Id);
        }
       }
      }
    }
    

Now, I have deploy the function by doing (Ctrl+Shift+P) by typing 'deploy to function app' and running the Azure Functions: Deploy to Function App command.

And I see Success message in Visual Studio Code Terminal

  3:09:06 PM liveConnectMessageEnhancefunapp: Creating zip package...
  3:09:07 PM liveConnectMessageEnhancefunapp: Starting deployment...
  3:09:18 PM liveConnectMessageEnhancefunapp: Updating submodules.
  3:09:19 PM liveConnectMessageEnhancefunapp: Preparing deployment for commit id 'f58cf57151'.
  3:09:21 PM liveConnectMessageEnhancefunapp: Skipping build. Project type: Run-From-Zip
  3:09:21 PM liveConnectMessageEnhancefunapp: Skipping post build. Project type: Run-From-Zip
  3:09:24 PM messageEnhancefunapp: Syncing 0 function triggers with payload size 2 bytes successful.
  3:09:25 PM messageEnhancefunapp: Updating D:\home\data\SitePackages\packagename.txt with deployment 20190110093910.zip
  3:09:25 PM messageEnhancefunapp: Deployment successful.
  Deployment to "messageEnhancefunapp" completed.

Warnings

C:\Users\deeku.nuget\packages\microsoft.net.sdk.functions\1.0.24\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): warning : Function [CosmosDBTriggerCSharp]: Missing value for AzureWebJobsStorage in local.settings.json. This is required for all triggers other than HTTP. [C:\FunctionApps\FunctionApps.csproj] C:\Users\deeku.nuget\packages\microsoft.net.sdk.functions\1.0.24\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): warning : [C:\FunctionApps\FunctionApps.csproj]

But when I navigate to Azure Portal, I see below message

Your app is currently in read-only mode because you are running from a package file. When running from a package, the file system is read-only and no changes can be made to the files. To make any changes update the content in your zip file and WEBSITE_RUN_FROM_PACKAGE app setting.

1
1st warning is to remind you AzureWebJobsStorage is necessary in local debug. 2nd message is to tell you can't modify the pre-compiled project on portal. Those are both harmless if you focus on the running on Azure site, what error have you seen e.g. cosmosdb trigger not work as expected? Unable to create a Azure function which reads the incoming messages to IoT Hub and sends to Cosmos DB, to achieve this you need to create a IoT Hub (Event Hub) trigger instead of cosmosdb. - Jerry Liu
I have IoT Hub and messages are receiving on it, But I have requirement to update message before storing it to CosmosDB. so I am planning to use Azure functions between IotHub and CosmosDb. I do not want to directly write azure function in portal because I want to track changes of this function in TFS. so I am using visual studio code to write functions - kudlatiger
Ah, it seems my poor English makes you confused. By Those are both harmless if you focus on the running on Azure site, I don't mean writing code in portal. The warnings and messages don't represent there's sth wrong with the pre-compiled project on Azure. That is why I ask whether you have seen any explicit error. To verify whether the code works before deployment, add AzureWebJobsStorage in local.settings.json and debug. And for the trigger type, CosmosDB trigger only works when we send data to some CosmosDB, perhaps you need to reconsider the pattern? - Jerry Liu
I have selected wrong template 'CosmosDBTrigger'. I should have searched for 'IoT Hub trigger' but it's not available as mentioned by @Tamás Huj . - kudlatiger
IoT Hub trigger is actually an Event Hub trigger. Since you could use Visual Studio, just follow the solution for convenience. - Jerry Liu

1 Answers

1
votes

In your example, you are creating an CosmosDBTrigger it can be used if you want to capture the changes in your cosmos DB. But, based on your description you want to capture the changes in IoT Hub.

I did not found the IoT Hub trigger in Visual Studio Code, but if you are using the full version of Visual Studio you can find the IoT Hub trigger there:

Visual studio IoT Hub Trigger

You just need to set up your connection on this screen, and in the method you will have the message from IoT Hub, and you can modify it as you wish, and you can save it to Cosmos DB.

Here is a similar blog post about the same topic: https://medium.com/@avirup171/azure-iot-hub-azure-function-azure-cosmos-db-walkthrough-cc30d12d1055