Running Eclipse HONO and Ditto on Google Cloud (4)

MichaelChi
Google Cloud - Community
3 min readAug 20, 2022

This is part 4 of a blog series that demonstrates running Eclipse HONO and Eclipse Ditto on Google Cloud.

In this episode I will connect Eclipse Ditto with Google Cloud Functions as well as create a device simulator that published telemetry via MQTT protocol. When everything is properly configured, the telemetry sending from device simulator will be forwarded to Google Cloud Functions.

Create a Cloud Function

To simplify the test, the Cloud Function is just to verify that the Google Cloud receives telemetry forwarded by Eclipse Ditto. Later we will enhance it to simulate more realistic use case, but for now it is good enough just to echo back anything it receives.

Go to Google Cloud Console, create a new Cloud Function with below nodejs codes. This Cloud function does nothing but just log the request coming from Eclipse Ditto.

exports.process = (req, res) => {
console.log(`process invoked`);
console.log(`req.query=${JSON.stringify(req.query)}`);
let message = req.query.message || req.body.message || 'ok!';
console.log(`received message: ${message}`);
res.status(200).send(message);
};

Deploy the Cloud Function and don’t forget to change the entry point to process

Now that we have the code ready, when the device publish telemetry to Eclipse HONO, it will be forwarded to Eclipse Ditto through the connection we configured in previous post. Then the Eclipse Ditto Connectivity service will invoke the HTTP endpoint (the Cloud Function) and POST the telemetry to the Cloud Function.

Since we don’t want the Cloud Function to be invoked by anyone else, typically we should configure Cloud Function permissions to allow specific users as Cloud Function invoker.

Eclipse Ditto does support OAuth 2.0 client_credentials grant, however Google Cloud does not currently support this grant when this document is written. In order to secure our Cloud Function, we must implement our own authentication mechanism. But for now, we just want to verify if the connection between Eclipse Ditto and Google Cloud, we simply grant allUsers as Cloud Functions Invoker to allow anonymous invocation. It’s insecure to expose your Cloud Functions to the internet, but for now we just want to verify the connectivity, so let’s leave it for now, we will get back to this later.

Create the connection for Eclipse and Cloud Functions

We have a Cloud Functions which provides a HTTP endpoint which accepts telemetry data, now we want to create a connection between Eclipse and Cloud Functions, so whenever telemetry flows to Eclipse HONO, it will be picked up by Eclipse Ditto and then forwarded to the Cloud Functions.

We use same REST API to create another connection, where

  • <YOUR FUNCTIONS URL> is the Cloud Functions HTTP URL
  • <YOUR ENTRY POINT> is the Cloud Functions path

For example if your Cloud Functions endpoints is https://myfunctions.cloudfunctions.net/receiveTelemetry, then the <FUNCTION URL> should be https://myfunctions.cloudfunctions.net and the <ENTRY POINT> should be /receiveTelemetry

curl -X POST -i -u devops:devopsPw1! -H 'Content-Type: application/json' -d '{
"targetActorSelection": "/system/sharding/connection",
"headers": {},
"piggybackCommand": {
"type": "connectivity.commands:createConnection",
"connection": {
"id": "gcp-connection-org-eclipse-ditto",
"connectionType": "http-push",
"connectionStatus": "open",
"failoverEnabled": true,
"specificConfig": {
"parallelism": "2"
},
"uri": "https://<YOUR FUNCTIONS URL>:443",
"sources": [],
"targets": [
{
"address": "POST:/<YOUR ENTRY POINT>",
"topics": ["_/_/things/twin/events", "_/_/things/live/messages",
"_/_/things/live/events", "_/_/things/live/commands",
"_/_/policies/announcements", "_/_/connections/announcements"],
"authorizationContext": ["nginx:ditto"],
"headerMapping": {
"content-type": "application/json"
}
}
]
}
}
}' http://${DITTO_API_IP}:${DITTO_API_PORT_http}/devops/piggyback/connectivity?timeout=8s

Verify the connectivity

Do a HTTP post to publish telemetry and verify if Cloud Function is correctly invoked.

curl -X POST -i -u ${DEVICE_ID}-auth@${TENANT_NAME}:my-password -H 'Content-Type: application/json' -d '{"temp": 11114.51, "hum":10}' http://${HTTP_ADAPTER_IP}:${HTTP_ADAPTER_PORT_http}/telemetry

Wait for several seconds, you should see log entries in Cloud Logging.

At this point, we have verified the connectivity from devices to Eclipse HONO and Eclipse Ditto as well as Google Cloud.

Part(5)

--

--