Cloud Functions for Firebase
Already using Cloud Functions in Google Cloud? Learn more about how Firebase fits into the picture.
Key capabilities
The functions you write can respond to events generated by various Firebase and Google Cloud features, from Firebase Authentication triggers to Cloud Storage Triggers.
Integrate across Firebase features using the Admin SDK together with Cloud Functions, and integrate with third-party services by writing your own webhooks. Cloud Functions minimizes boilerplate code, making it easier to use Firebase and Google Cloud inside your function.How does it work?
After you write and deploy a function, Google's servers begin to manage the function immediately. You can fire the function directly with an HTTP request, the Admin SDK, or a scheduled job, or, in the case of background functions, Google's servers listen for events and run the function when it is triggered.
As the load increases or decreases, Google responds by rapidly scaling the number of virtual server instances needed to run your function. Each function runs in isolation, in its own environment with its own configuration.
Lifecycle of a background function
- You write code for a new function, selecting an event provider (such as Cloud Firestore), and defining the conditions under which the function should execute.
- When you deploy your function:
- The Firebase CLI creates a
.ziparchive of the function code, which is then uploaded to a Cloud Storage bucket (prefixed withgcf-sources) before Cloud Functions creates an Artifact Registry repository (namedgcf-artifacts) in your project. - Cloud Build retrieves the function code and builds the function source. You can view Cloud Build logs in the Google Cloud console.
- The container image for the built functions code is uploaded to a
private Artifact Registry repository in your project
(named
gcf-artifacts), and your new function is rolled out.
- The Firebase CLI creates a
- When the event provider generates an event that matches the function's conditions, the code is invoked. The function has a service account attached to it that can be used to access other Firebase services with the help of the Firebase Admin SDK.
- If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up.
- When you update the function by deploying updated code, instances for older versions are cleaned up along with build artifacts in Artifact Registry, and replaced by new instances.
- When you delete the function, all instances and zip archives are cleaned up, along with related build artifacts in Artifact Registry. The connection between the function and the event provider is removed.
In addition to listening for events with a background function, you can call functions directly with an HTTP request or a call from the client. You can also trigger functions on a fixed schedule or enqueue task functions via the Admin SDK.
Implementation path
Next steps
- Get started setting up, creating, and deploying functions.
- Learn more about what you can do with functions.
- Try the Cloud Functions codelab.