In the article Debugging Google Cloud Functions in Node we exposed a first approach of what Google Functions Framework was and how could it be used.
The article was mainly focused on debugging Google Cloud Function with HTTP signature, ie, those functions intended to be triggered by an HTTP request. There are however other ways of triggering the execution of a Google Cloud Function, as for instance Cloud Storage, Firestore or, more commonly, by forwarding a Pub/Sub message.
This article covers how to debug Google Cloud functions intended to be triggered by a Pub/Sub message.
Google Cloud functions with cloud event signature
Let's assume we have a function that must be triggered by a Pub/Sub message. To do that, we need to perform two actions:
- Properly configuring Google Cloud to specify that our function will be triggered by a given kind of Pub/Sub message. This part is described in the official documentation and it's out of the scope of this article.
- Coding a Google Cloud function with cloud event signature, what means that the Google Cloud Function entry-point, ie, the function that will be executed when our Google Cloud Function was triggered, needs to declare specific arguments and handle them properly.
As specified in the documentation, the entry-point function must declare a single parameter. In the case of being triggered by a Pub/Sub event, this parameter will be a JSON object similar to this one:
{
// data contains the Base64 representation
// of the Pub/Sub message payload,
// in this case a single string
// with value "Hello world!"
"data": "SGVsbG8gd29ybGQh"
}
Assuming that the entry-point function defines a single parameter called cloudEvent
, it would have to get the value of the field cloudEvent.data
and then decoding it from base64. Once done, the Pub/Sub message payload would be available to the Google Cloud Function, as can be seen in this NodeJS example:
/**
* Google Cloud Function entry-point
* @param {object} cloudEvent Cloud event data
*/
function helloWorld(cloudEvent) {
// Decode from Base64
const dataBuffer = new Buffer(cloudEvent.data);
const messageData = dataBuffer.toString("base64");
// Do something with the message
console.log(`Pub/Sub message data: '${messageData}'`);
}
Debugging event signature functions with Functions Framework
To run and debug Google Cloud Functions with event signature using Functions Framework, the argument signature-type
must be set to cloudevent
:
functions-framework \
--source=./dist \
--signature-type=cloudevent \
--target=helloWorld
The command above would run locally the Google Cloud Function source code stored in the ./dist
directory. It would start an http server listening on http://locahost:8080
and, for each POST request received, it would invoke the helloWorld
entry point function passing as first argument the request body content. So, if this curl command is executed...
curl -X POST localhost:8080 \
-d '{"data":{"message":{"data":"SGVsbG8gd29ybGQh"}}}' \
-H 'content-type:application/json'
... it would get the JSON object {"data":{"message":{"data":"SGVsbG8gd29ybGQh"}}}
as first argument. Once Base64-decoded, the obtained data would be a string with value "Hello world!"
, representing the Pub/Sub message body.
As explained in the article Debugging Google Cloud Functions in Node, the command above can be completed with different commands to allow debugging (node --inspect
) or using environmental variables by using, among others, the NPM package env-cmd.
Comentarios
Publicar un comentario