Direct Triggers

To support quick iteration and debugging, Cloud Functions provides a call command in the command-line interface and testing functionality in the GCP 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.

Using the gcloud command-line interface

To directly invoke a function using the gcloud tool, 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":"Keyboard Cat"}'

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. This means it is accessible in the Node.js 6 runtime through event.data and in the Node.js 8 and Python runtimes through the data parameter. For more information on accessing event data in background functions, see Background function parameters.

Using the GCP Console

To directly invoke a function from the GCP Console, follow these steps:

  1. Go to the Cloud Functions Overview page.

  2. From the list, click the name of the function you want to invoke.

    This takes you to the Function details page.

  3. Click the Testing tab.

  4. In the Triggering event field, enter any data your function expects as JSON.

  5. Click Test the function.

Your function's response appears in the Output field, and logs for the individual execution appear in the Logs field.

Cloud Pub/Sub example

This example shows how to directly invoke a background function triggered by Cloud Pub/Sub events. See the following example function:

Node.js 6

/**
 * Background Cloud Function to be triggered by Pub/Sub.
 * This function is exported by index.js, and executed when
 * the trigger topic receives a message.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.helloPubSub = (event, callback) => {
  const pubsubMessage = event.data;
  const name = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World';

  console.log(`Hello, ${name}!`);

  callback();
};

Node.js 8 (Beta)

/**
 * Background Cloud Function to be triggered by Pub/Sub.
 * This function is exported by index.js, and executed when
 * the trigger topic receives a message.
 *
 * @param {object} data The event payload.
 * @param {object} context The event metadata.
 */
exports.helloPubSub = (data, context) => {
  const pubSubMessage = data;
  const name = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString() : 'World';

  console.log(`Hello, ${name}!`);
};

Python (Beta)

def hello_pubsub(data, context):
    """Background Cloud Function to be triggered by Pub/Sub.
    Args:
         data (dict): The dictionary with data specific to this type of event.
         context (google.cloud.functions.Context): The Cloud Functions event
         metadata.
    """
    import base64

    if 'data' in data:
        name = base64.b64decode(data['data']).decode('utf-8')
    else:
        name = 'World'
    print('Hello, {}!'.format(name))

To directly invoke the function, you need to send a PubsubMessage, which expects base64-encoded data, as the event data:

Node.js 6

DATA=$(printf 'PubSub!'|base64) && gcloud functions call helloPubSub --data '{"data":"'$DATA'"}'

Node.js 8 (Beta)

DATA=$(printf 'PubSub!'|base64) && gcloud functions call helloPubSub --data '{"data":"'$DATA'"}'

Python (Beta)

DATA=$(printf 'PubSub!'|base64) && gcloud functions call hello_pubsub --data '{"data":"'$DATA'"}'

You can also invoke the function from the GCP Console UI: simply use the same event data in the Triggering event field.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation