Function Apps. Serverless Compute. Simulated Devices. NOW with added Application Insights! How simple is this…


Just another post about my love of the serverless compute that Microsoft now give us.

 

So I wanted to spin up a simulated device to pump some dummy data into IoT Hub.

Reach for the Console App? Not my first choice these days.

Reach for a NodeJS script, or NodeRed flow on a Linux VM? Too old skool. Overkill even!

 

functions.azure.com

New C# Timer-triggered function (default option), with just this code:

#r "Newtonsoft.Json"
using System;
using Newtonsoft.Json;
using System.Text;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    var msg = new { Value = DateTime.Now.Second.ToString() };
    SendMessage(msg, "MyIoTHub", 
        "Device1", 
        "2016-02-03", 
        "<YOUR DEVICE SAS SIG>"
    ); 
}

static void SendMessage(object body, string iotHub, string deviceId, string apiVer, string sas)
{
    string restUri = $"https://{iotHub}.azure-devices.net/devices/{deviceId}/messages/events?api-version={apiVer}";
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("Authorization", sas);

    var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
    var result = client.PostAsync(restUri, content).Result;
}

 

And that's it!

 

No dlls to upload (it's just HTTP REST)

No config or deploy or setup

It defaults to once per five minutes, but you can change that in the "Integrate" section of your App editor.

And of course I can ARM deploy this, code sourced from GitHub.

 

And if that weren't good enough, I now hear that Application Insights is available in beta for you to try with your [non-production] Logic Apps.

The Function App team listening to user requests once again, have delivered as promised.

https://github.com/Azure/Azure-Functions/wiki/App-Insights-Early-Preview

This gives us a feature rich array of tools to monitor and analyse performance, throughput and so much more!

 

Thanks for reading!

Pete Laker (Azure MVP)
MVP logo8507_PeteWikiNinjaStick_png-199x0


Comments (1)

  1. Sjoukje Zaal says:

    Nice one Pete! I must admit that I’m still firing up a console app. Maybe it is time for a change!

Skip to main content