Call Cloud Functions directly
To support quick iteration and debugging, Cloud Functions provides a
call
command in the command-line interface and testing functionality in the
Google Cloud console UI. This allows you to directly invoke a function to
ensure it is behaving as expected. This causes the function to execute
immediately, even though it may have been deployed to respond to a
specific event.
Test your function with Google Cloud CLI
1st gen
To directly invoke a 1st gen function using the gcloud CLI, use
the gcloud functions call
command and supply any data your function expects
as JSON in the --data
argument. For example:
gcloud functions call YOUR_FUNCTION_NAME --data '{"name":"Tristan"}'
where YOUR_FUNCTION_NAME
is the name of the function
you want to execute. The --data
argument is sent to your function as follows:
- For HTTP functions, the data you supply is sent as the body of a POST request.
- For background functions, the data is passed directly as the event data to your function. For more information on accessing event data in background functions, see Background function parameters.
- For CloudEvent functions, the data is passed directly as the event data to your function. For more information on accessing event data in CloudEvent functions, see CloudEvent function parameters.
2nd gen
To directly invoke a 2nd gen function using the gcloud CLI, use
the 2nd gen version of the gcloud functions call
command and supply any data
your function expects as JSON in the --data
argument. For example:
gcloud functions call YOUR_FUNCTION_NAME \
--region=REGION --gen2 \
--data '{"name":"Kalani"}'
Replace:
- YOUR_FUNCTION_NAME: the name of the function you're testing
- REGION: the Google Cloud region your function is deployed to
The --data
argument is sent to your function as follows:
- For HTTP functions, the data you supply is sent as the body of a POST request.
- For CloudEvent functions, the data is passed directly as the event data to your function. For more information on accessing event data in CloudEvent functions, see CloudEvent function parameters.
For more information, see the gcloud functions call
documentation.
Test your function with the Google Cloud Console
To directly invoke a function from the Google Cloud console, follow these steps:
1st gen
Click the name of the function you want to invoke.
Click the Testing tab.
In the Configure Triggering Event field, enter any data your function expects as JSON.
Click Test the function.
Your function's response appears in the Output field, and logs for the individual execution appear in the Logs field.
2nd gen
From the list, click the name of the function you want to invoke. This takes you to the Function details page.
Click the Testing tab.
In the Configure Triggering Event field, enter any data your function expects as JSON.
Click the +Add query parameter and +Add header parameter buttons to add query and header parameters to your function call as needed.
Google Cloud console assembles the parameters you specify into a
gcloud functions call
command in the CLI test command window.Select Run in cloud shell to open a Cloud Shell window to run this command.
Press Enter to trigger the
gcloud functions call
command after it appears in your Cloud Shell window.
Cloud Pub/Sub event-driven function example
This example shows how to directly invoke a 1st gen event-driven function triggered by Cloud Pub/Sub events:
Node.js
Python
Go
Java
C#
Ruby
PHP
To invoke the function directly, send a
PubsubMessage
,
which expects base64-encoded data, as the event data:
Node.js
DATA=$(printf 'Hello!'|base64) && gcloud functions call helloPubSub --data '{"data":"'$DATA'"}'
Python
DATA=$(printf 'Hello!'|base64) && gcloud functions call hello_pubsub --data '{"data":"'$DATA'"}'
Go
DATA=$(printf 'Hello!'|base64) && gcloud functions call HelloPubSub --data '{"data":"'$DATA'"}'
Java
DATA=$(printf 'Hello!'|base64) && gcloud functions call java-hello-pubsub --data '{"data":"'$DATA'"}'
C#
DATA=$(printf 'Hello!'|base64) && gcloud functions call csharp-hello-pubsub --data '{"data":"'$DATA'"}'
Ruby
DATA=$(printf 'Hello!'|base64) && gcloud functions call hello_pubsub --data '{"data":"'$DATA'"}'
PHP
DATA=$(printf 'Hello!'|base64) && gcloud functions call helloworldPubsub --data '{"data":"'$DATA'"}'
This CLI example uses bash
or sh
syntax. It works in Linux and Mac
environments but not Windows.
You can also invoke the function from the Google Cloud console by using the same event data in the Triggering event field.