DISCLAIMER: This article focuses on Google Cloud Functions with HTTP signature, ie, those functions intended to be triggered by an HTTP request. In case of being interested in Cloud Functions being triggered by Cloud Storage, File Storage or Pub/Sub events, read Debugging Google Cloud Functions with event signature in Node.
Cloud Functions, the Function as a Service (FaaS) product of Google Cloud, is an attractive resource for those developing solutions in cloud environments. Similar in conception to AWS Lambdas or Azure Functions, they offer serverless processing at an affordable price.
From the developer point of view, they don't differ too much of a standard function contained in a program developed in Node, Python, Go or Java, except for debugging: Due to their ubiquitous nature debugging is not easy and most of the times is done following one of these two approaches:
- Uploading a debug version, running it and then checking its output (logging and/or function output). This is the easiest way, though it comes with a bunch of associated problems:
- Just the last uploaded version of a function can be run, so there's not way of isolating the debugging version for testing purposes.
- If the developer is working in a strict CI/CD environment, uploading a debugging version can imply creating a debug PR, merging and then deploying it... too tedious for just debugging.
- Patching the code to allow local debugging. This option solves the above problems, but it implies debugging a code that is not that being uploaded, but a patched one. If patch is not properly removed chances are that the function that ran flawlessly in local got into problems when running in the Cloud.
Functions Framework
- install the module @google-cloud/functions-framework and then
- open a terminal and run the command functions-framework --source=<your javascript source base directory> --target=<your function name>
functions-framework --source=./dist --target=helloWorld
./node_modules/@google-cloud/functions-framework \
--source=./dist --target=helloWorld
npx @google-cloud/functions-framework \
--source=./dist --target=helloWorld
curl -X GET localhost:8080 \
-d '{"greet":"hello"}' -H 'content-type:application/json'
node --inspect \
functions-framework --source=./dist --target=helloWorld
node --inspect \
./node_modules/@google-cloud/functions-framework \
--source=./dist --target=helloWorld
Functions Framework and environmental variables
env-cmd node --inspect \
functions-framework --source=./dist --target=helloWorld
env-cmd node --inspect \
./node_modules/@google-cloud/functions-framework \
--source=./dist --target=helloWorld
Functions Framework and Visual Studio Code
--sourcemap
option or set the sourceMap
porperty in the tsconfig.json file to true
:
{
// tsconfig options
"compilerOptions": {
// tsconfig compiler options
"sourceMap": true
}
}
Comentarios
Publicar un comentario