Test locally with the Pub/Sub emulator
You can test functions locally before you deploy them, by using Functions Framework in conjunction with the Pub/Sub emulator. The examples on this page are based on Cloud Run functions.
Use Functions Framework with Pub/Sub emulator
You can trigger a function locally using a push message from the Pub/Sub emulator.
Test this feature as described here. Note that it requires you to use three separate terminal instances:
In the first terminal, start the Pub/Sub emulator on port 8043 in a local project:
gcloud beta emulators pubsub start \ --project=abc \ --host-port='localhost:8043'
In the second terminal, create a Pub/Sub topic and subscription:
curl -s -X PUT 'http://localhost:8043/v1/projects/abc/topics/mytopic'
Use
http://localhost:8080
as the push subscription's endpoint.curl -s -X PUT 'http://localhost:8043/v1/projects/abc/subscriptions/mysub' \ -H 'Content-Type: application/json' \ --data '{"topic":"projects/abc/topics/mytopic","pushConfig":{"pushEndpoint":"http://localhost:8080/projects/abc/topics/mytopic"}}'
In the third terminal, clone the sample repository to your local machine:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Run functions sample code:
Node.js
cd nodejs-docs-samples/functions/v2/helloPubSub/
Python
cd python-docs-samples/functions/v2/pubsub/
Go
cd golang-samples/functions/functionsv2/hellopubsub/
Take a look at the sample code:
Node.js
Python
Go
Create the buildpack (this may take a few minutes):
Node.js
pack build \ --builder gcr.io/buildpacks/builder:v1 \ --env GOOGLE_FUNCTION_SIGNATURE_TYPE=event \ --env GOOGLE_FUNCTION_TARGET=helloPubSub \ my-function
Python
pack build \ --builder gcr.io/buildpacks/builder:v1 \ --env GOOGLE_FUNCTION_SIGNATURE_TYPE=event \ --env GOOGLE_FUNCTION_TARGET=subscribe \ my-function
Go
pack build \ --builder gcr.io/buildpacks/builder:v1 \ --env GOOGLE_FUNCTION_SIGNATURE_TYPE=event \ --env GOOGLE_FUNCTION_TARGET=HelloPubSub \ my-function
Start the Pub/Sub function on port 8080. This is where the emulator will send push messages:
docker run --rm -p 8080:8080 my-function
In the second terminal, invoke the function by publishing a message. The message data needs to be encoded in base64. This example uses the base64 encoded string
{"foo":"bar"}
.curl -s -X POST 'http://localhost:8043/v1/projects/abc/topics/mytopic:publish' \ -H 'Content-Type: application/json' \ --data '{"messages":[{"data":"eyJmb28iOiJiYXIifQ=="}]}'
You should see the function output in the third terminal.
Press
Ctrl+C
to abort.