This page describes the concept of events in the context of Google Cloud Functions. It also covers how to create and associate triggers with functions so that they will execute when an event is fired.
Events
Events are things that happen within your cloud environment that you might want to take action on. These might be changes to data in a database, files added to a storage system, or a new virtual machine instance being created. Currently, Cloud Functions supports events from the following providers:
- HTTP
- Cloud Storage
- Cloud Pub/Sub
- Cloud Firestore
- Firebase (Realtime Database, Storage, Analytics, Auth)
- Stackdriver Logging—forward log entries to a Pub/Sub topic by creating a sink. You can then trigger the function.
For examples of how to associate triggers with functions so that they execute when an event is fired, see the tutorials.
Event data
When an event triggers the execution of your Cloud Function, data associated with the event is passed via the function's parameters. The type of event determines the parameters passed to your function. HTTP request events trigger HTTP functions, and the other event types trigger background functions.
Node.js
In the Node.js runtime, functions take the following parameters:
-
HTTP Functions
Your function is passed the ExpressJS parameters
(request, response)
. Use theresponse
parameter to send a response. -
Background Functions
Your function is passed the parameters
(data, context, callback)
. See Background Functions for more details on these parameters.
Python
In the Python runtime, functions take the following parameters:
-
HTTP Functions
Your function is passed a single parameter,
(request)
, which is a FlaskRequest
object. Return any value from your function that can be handled by the Flaskmake_response
method. The result will be the HTTP response. -
Background Functions
Your function is passed the parameters
(data, context)
. See Background Functions for more details on these parameters.
Go
In the Go runtime, functions take the following parameters:
-
HTTP Functions
Your function is passed the
net/http
parameters(http.ResponseWriter, *http.Request)
. Use theResponseWriter
parameter to send a response. -
Background Functions
Your function is passed the parameters
(context.Context, Event)
. See Background Functions for more details on these parameters.
Java
In the Java runtime, functions take the following parameters:
-
HTTP Functions
Your function is passed the parameters
(HttpRequest, HttpResponse)
. Use theHttpResponse
parameter to send a response. -
Background Functions
Your function is passed the parameters
(event, context)
. See Background Functions for more details on these parameters.
Triggers
Creating a response to an event is done with a trigger. A trigger is a declaration that you are interested in a certain event or set of events. Binding a function to a trigger allows you to capture and act on events.
Below is a table of the types of triggers supported and the flags used to specify them during command-line deployment:
Trigger | Command-line flag |
---|---|
HTTP | --trigger-http |
Google Cloud Pub/Sub | --trigger-topic TOPIC_NAME |
Other sources (e.g. Firebase) | --trigger-event EVENT_TYPE --trigger-resource RESOURCE |
For more information on the command-line flags, see the
gcloud functions deploy
reference.
Binding of triggers to functions happens at deployment time either via the gcloud command-line tool, the UI or Cloud Functions API. Functions and triggers are bound to each other on a many-to-one basis. In other words, you cannot bind the same function to more than a single trigger at a time. You can, however, have the same trigger cause multiple functions to execute by simply deploying two different functions with the same trigger.