Using Funk to process data using Azure Functions

Soracom Funk provides an easy way for devices to send data to cloud functions and then receive the processed results. With Funk, this communication can be accomplished without setting up any custom servers or network environments. In this guide, we'll create a simple HTTP-triggered function in Azure, and then use our device to trigger it using Funk and receive the results. Let's get started!

Prerequisites


Configure Azure Functions

1. Create a Function

Azure provides several methods for creating a function, such as on the Functions App service dashboard in the Azure portal, using Azure CLI directly on your local machine, or even directly in VS Code.

Follow one of Azure's quickstart guides for creating a sample function:

If you have not created an Azure Function before, we recommend using the Develop Azure Functions by using Visual Studio Code guide, as it will allow you to quickly deploy a function with minimal setup. For this guide, at a minimum you should complete the following sections:

  1. Install the Azure Functions extension
  2. Create an Azure Functions project
  3. Publish to Azure
  4. Get the URL of the deployed function

During the guide, Azure will automatically provide the following sample function, which simply checks for a name query parameter or body parameter, and returns a message:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

Once you have completed the quickstart guide, you should have the following information:

You can find this information by getting the URL of the deployed function, which will return a URL in the following format: https://<function_app_name>.azurewebsites.net/api/<function_name>?code=<function_key>

In some cases, the Function key may include symbols that will be URL-encoded when included in the Function URL. If your Function key includes % characters (indicating that your Function key has been URL-encoded), ensure that you URL decode the Function key before proceeding.


Configure Soracom Funk

Head over to the Soracom User Console and configure Funk with our Azure function.

2. Add a Funk Configuration

Let's open the group which contains our Soracom Air SIM in order to set up Funk. You can either click the name of the group from the SIM Management page, or from the Groups page.


Test the connection

Now that Funk is configured, any HTTP POST request that is sent from a Soracom Air SIM device within this group to a Funk endpoint will be sent to our Azure Function, and the response from the function will be returned to our device.

As we configured Funk for JSON data, Funk will expect a JSON payload in the body of an HTTP POST request.

3. Send test data

In this example, our device with Soracom Air SIM is running Linux. In order to test our Azure function, we can use the curl command to send an HTTP POST request:

curl -X POST \
>  -H 'Content-Type: application/json' \
>  -d '{"name": "Soracom"}' \
>  funk.soracom.io

As our sample Azure function will return a simple "Hello" message using the name parameter in our body, we should see the response directly on our device:

>Hello Soracom
>

Conclusion

Congratulations. You just triggered an Azure Function from your cellular device by using Soracom Funk. Now you can modify the data being sent and the Azure Function code to fit your use case. Another important next step would be to implement signature validation in order to ensure that the requests to your cloud function are indeed coming from Soracom.

Since the connection between the device and Soracom's cloud is encrypted over cellular, we just had to configure the hop between Soracom and Azure. This means that devices can be deployed at scale without having to worry about managing credentials, endpoints, or compute resources needed to manage cryptographic handshakes.