Google Cloud Functions is a compute solution for developers to create single-purpose, stand-alone functions that respond to cloud events without the need to manage a server or runtime environment, this is why Cloud Functions are considered serverless architecture.
Cloud Functions for Firebase provides a way to extend the behavior of Firebase and integrate Firebase features through the addition of server-side code. It is a joint product between the Google Cloud Platform team and the Firebase team.
There are 2 distinct types of Cloud Functions:
- HTTP functions
- Background functions
I assume you have a prior experience using Firebase.
- Create or select a Firebase project in the Firebase console.
- Install the Firebase CLI.
- In new firebase project, create database
gcloud auth application-default login
Initialize the Firebase SDK for Cloud Functions
- Create a new project folder.
- cd /NewProjectFolderName
When complete, your project structure should look similar to this:
. ├── .firebaserc #Hidden file that helps you quickly switch projects | #using 'firebase use' ├── firebase.json #Describes properties for your project └── functions #Directory containing all your functions code ├── index.js #main source file for your Cloud Functions code | └── package.json #npm package file describing your Cloud Functions #code
- Inside /functions/index.js, lets Import required modules and initialize an app:
2. Let’s add our first Cloud Function.
This function passes a text value and inserts it into the database under the path
3. Let’s add a background function that modifies our data
makeUppercase() function executes when Cloud Firestore is written to.
ref.set defines the document to listen to.
- For now, this is all we need to start testing our function
The Firebase Local Emulator Suite allows us to build and test apps on our local machine instead of deploying to a project.
- cd /functions, run:
*If you receive an error “
Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.
gcloud auth application-default login*
Using the emulator suite, we can locally test our functions:
- Navigate to the given URL in the browser.
- Add the text parameter, and give it a value.
Deploy to a production environment
- Let’s deploy our functions and then test a trigger
$ firebase deploy --only functions
- Navigate to console.firebase.google.com.
- Select your project.
- Click the Functions tab
Here we see our deployed functions with additional details.
- Grab the URL from the HTTP function.
Google Cloud Functions is a lightweight compute solution for developers to create single-purpose, stand-alone functions that respond to cloud events without the need to manage a server or runtime environment. Be sure to check out some function examples below!
Cassandra.Link is a knowledge base that we created for all things Apache Cassandra. Our goal with Cassandra.Link was to not only fill the gap of Planet Cassandra, but to bring the Cassandra community together. Feel free to reach out if you wish to collaborate with us on this project in any capacity.
We are a technology company that specializes in building business platforms. If you have any questions about the tools discussed in this post or about any of our services, feel free to send us an email!