Locally test your function

This guide shows you how to locally call a function from source code using a gcloud functions command. To learn how to locally develop and test a Cloud Run functions using the Functions Framework, see Local functions development .

To support quick iteration and debugging for functions that were created with the Cloud Functions v2 API , you can use the call command in the command-line interface. This lets you 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

To directly invoke your 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 
 
  
 \ 
  
 -- 
 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 event-driven functions,the data is passed directly as the event data to your function.

For more information, see the gcloud functions call documentation.

Cloud Pub/Sub event-driven function example

This example shows how to directly invoke an event-driven function triggered by Pub/Sub events:

Node.js

  const 
  
 functions 
  
 = 
  
 require 
 ( 
 '@google-cloud/functions-framework' 
 ); 
 // Register a CloudEvent callback with the Functions Framework that will 
 // be executed when the Pub/Sub trigger topic receives a message. 
 functions 
 . 
 cloudEvent 
 ( 
 'helloPubSub' 
 , 
  
 cloudEvent 
  
 = 
>  
 { 
  
 // The Pub/Sub message is passed as the CloudEvent's data payload. 
  
 const 
  
 base64name 
  
 = 
  
 cloudEvent 
 . 
 data 
 . 
 message 
 . 
 data 
 ; 
  
 const 
  
 name 
  
 = 
  
 base64name 
  
 ? 
  
 Buffer 
 . 
 from 
 ( 
 base64name 
 , 
  
 'base64' 
 ). 
 toString 
 () 
  
 : 
  
 'World' 
 ; 
  
 console 
 . 
 log 
 ( 
 `Hello, 
 ${ 
 name 
 } 
 !` 
 ); 
 }); 
 

Python

  import 
  
 base64 
 from 
  
 cloudevents.http 
  
 import 
 CloudEvent 
 import 
  
 functions_framework 
 # Triggered from a message on a Cloud Pub/Sub topic. 
 @functions_framework 
 . 
 cloud_event 
 def 
  
 subscribe 
 ( 
 cloud_event 
 : 
 CloudEvent 
 ) 
 - 
> None 
 : 
 # Print out the data from Pub/Sub, to prove that it worked 
 print 
 ( 
 "Hello, " 
 + 
 base64 
 . 
 b64decode 
 ( 
 cloud_event 
 . 
 data 
 [ 
 "message" 
 ][ 
 "data" 
 ]) 
 . 
 decode 
 () 
 + 
 "!" 
 ) 
 

Go

  // Package helloworld provides a set of Cloud Functions samples. 
 package 
  
 helloworld 
 import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "log" 
  
 "github.com/GoogleCloudPlatform/functions-framework-go/functions" 
  
 "github.com/cloudevents/sdk-go/v2/event" 
 ) 
 func 
  
 init 
 () 
  
 { 
  
 functions 
 . 
 CloudEvent 
 ( 
 "HelloPubSub" 
 , 
  
 helloPubSub 
 ) 
 } 
 // MessagePublishedData contains the full Pub/Sub message 
 // See the documentation for more details: 
 // https://cloud.google.com/eventarc/docs/cloudevents#pubsub 
 type 
  
 MessagePublishedData 
  
 struct 
  
 { 
  
 Message 
  
 PubSubMessage 
 } 
 // PubSubMessage is the payload of a Pub/Sub event. 
 // See the documentation for more details: 
 // https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage 
 type 
  
 PubSubMessage 
  
 struct 
  
 { 
  
 Data 
  
 [] 
 byte 
  
 `json:"data"` 
 } 
 // helloPubSub consumes a CloudEvent message and extracts the Pub/Sub message. 
 func 
  
 helloPubSub 
 ( 
 ctx 
  
 context 
 . 
 Context 
 , 
  
 e 
  
 event 
 . 
 Event 
 ) 
  
 error 
  
 { 
  
 var 
  
 msg 
  
 MessagePublishedData 
  
 if 
  
 err 
  
 := 
  
 e 
 . 
 DataAs 
 ( 
& msg 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "event.DataAs: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 name 
  
 := 
  
 string 
 ( 
 msg 
 . 
 Message 
 . 
 Data 
 ) 
  
 // Automatically decoded from base64. 
  
 if 
  
 name 
  
 == 
  
 "" 
  
 { 
  
 name 
  
 = 
  
 "World" 
  
 } 
  
 log 
 . 
 Printf 
 ( 
 "Hello, %s!" 
 , 
  
 name 
 ) 
  
 return 
  
 nil 
 } 
 

Java

  import 
  
 com.google.cloud.functions.CloudEventsFunction 
 ; 
 import 
  
 com.google.gson.Gson 
 ; 
 import 
  
 functions.eventpojos.PubSubBody 
 ; 
 import 
  
 io.cloudevents.CloudEvent 
 ; 
 import 
  
 java.nio.charset.StandardCharsets 
 ; 
 import 
  
 java.util.Base64 
 ; 
 import 
  
 java.util.logging.Logger 
 ; 
 public 
  
 class 
 SubscribeToTopic 
  
 implements 
  
 CloudEventsFunction 
  
 { 
  
 private 
  
 static 
  
 final 
  
 Logger 
  
 logger 
  
 = 
  
 Logger 
 . 
 getLogger 
 ( 
 SubscribeToTopic 
 . 
 class 
 . 
 getName 
 ()); 
  
 @Override 
  
 public 
  
 void 
  
 accept 
 ( 
 CloudEvent 
  
 event 
 ) 
  
 { 
  
 // The Pub/Sub message is passed as the CloudEvent's data payload. 
  
 if 
  
 ( 
 event 
 . 
 getData 
 () 
  
 != 
  
 null 
 ) 
  
 { 
  
 // Extract Cloud Event data and convert to PubSubBody 
  
 String 
  
 cloudEventData 
  
 = 
  
 new 
  
 String 
 ( 
 event 
 . 
 getData 
 (). 
 toBytes 
 (), 
  
 StandardCharsets 
 . 
 UTF_8 
 ); 
  
 Gson 
  
 gson 
  
 = 
  
 new 
  
 Gson 
 (); 
  
 PubSubBody 
  
 body 
  
 = 
  
 gson 
 . 
 fromJson 
 ( 
 cloudEventData 
 , 
  
 PubSubBody 
 . 
 class 
 ); 
  
 // Retrieve and decode PubSub message data 
  
 String 
  
 encodedData 
  
 = 
  
 body 
 . 
 getMessage 
 (). 
 getData 
 (); 
  
 String 
  
 decodedData 
  
 = 
  
 new 
  
 String 
 ( 
 Base64 
 . 
 getDecoder 
 (). 
 decode 
 ( 
 encodedData 
 ), 
  
 StandardCharsets 
 . 
 UTF_8 
 ); 
  
 logger 
 . 
 info 
 ( 
 "Hello, " 
  
 + 
  
 decodedData 
  
 + 
  
 "!" 
 ); 
  
 } 
  
 } 
 } 
 

C#

  using 
  
 CloudNative.CloudEvents 
 ; 
 using 
  
 Google.Cloud.Functions.Framework 
 ; 
 using 
  
 Google.Events.Protobuf.Cloud.PubSub.V1 
 ; 
 using 
  
 Microsoft.Extensions.Logging 
 ; 
 using 
  
 System.Threading 
 ; 
 using 
  
 System.Threading.Tasks 
 ; 
 namespace 
  
 HelloPubSub 
 ; 
 public 
  
 class 
  
 Function 
  
 : 
  
 ICloudEventFunction<MessagePublishedData> 
 { 
  
 private 
  
 readonly 
  
 ILogger 
  
 _logger 
 ; 
  
 public 
  
 Function 
 ( 
 ILogger<Function> 
  
 logger 
 ) 
  
 = 
>  
 _logger 
  
 = 
  
 logger 
 ; 
  
 public 
  
 Task 
  
 HandleAsync 
 ( 
 CloudEvent 
  
 cloudEvent 
 , 
  
 MessagePublishedData 
  
 data 
 , 
  
 CancellationToken 
  
 cancellationToken 
 ) 
  
 { 
  
 string 
  
 nameFromMessage 
  
 = 
  
 data 
 . 
 Message 
 ?. 
 TextData 
 ; 
  
 string 
  
 name 
  
 = 
  
 string 
 . 
 IsNullOrEmpty 
 ( 
 nameFromMessage 
 ) 
  
 ? 
  
 "world" 
  
 : 
  
 nameFromMessage 
 ; 
  
 _logger 
 . 
 LogInformation 
 ( 
 "Hello {name}" 
 , 
  
 name 
 ); 
  
 return 
  
 Task 
 . 
 CompletedTask 
 ; 
  
 } 
 } 
 

Ruby

  require 
  
 "functions_framework" 
 require 
  
 "base64" 
 FunctionsFramework 
 . 
 cloud_event 
  
 "hello_pubsub" 
  
 do 
  
 | 
 event 
 | 
  
 # The event parameter is a CloudEvents::Event::V1 object. 
  
 # See https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1.html 
  
 name 
  
 = 
  
 Base64 
 . 
 decode64 
  
 event 
 . 
 data 
 [ 
 "message" 
 ][ 
 "data" 
 ] 
  
 rescue 
  
 "World" 
  
 # A cloud_event function does not return a response, but you can log messages 
  
 # or cause side effects such as sending additional events. 
  
 logger 
 . 
 info 
  
 "Hello, 
 #{ 
 name 
 } 
 !" 
 end 
 

PHP

  use CloudEvents\V1\CloudEventInterface; 
 use Google\CloudFunctions\FunctionsFramework; 
 // Register the function with Functions Framework. 
 // This enables omitting the `FUNCTIONS_SIGNATURE_TYPE=cloudevent` environment 
 // variable when deploying. The `FUNCTION_TARGET` environment variable should 
 // match the first parameter. 
 FunctionsFramework::cloudEvent('helloworldPubsub', 'helloworldPubsub'); 
 function helloworldPubsub(CloudEventInterface $event): void 
 { 
 $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb'); 
 $cloudEventData = $event->getData(); 
 $pubSubData = base64_decode($cloudEventData['message']['data']); 
 $name = $pubSubData ? htmlspecialchars($pubSubData) : 'World'; 
 fwrite($log, "Hello, $name!" . PHP_EOL); 
 } 
 

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.

Design a Mobile Site
View Site in Mobile | Classic
Share by: