Where is data stored and retrieved for the Google Cloud Functions API?

I am playing with the Google Cloud Functions API and I am confused as to how to properly use it for a simple REST API to fetch some data from the database and respond to the client with the data it needs.

I am using serverless to deploy my code that looks like this for a function.

exports.http = (request, response) => {
  response.status(200).send('Hello World!');
};

      

but this is just a simple answer 200 (OK)

with no data from the database.

Coming from MVC frameworks like Rails or Django, I would write a controller action to fetch some data from my database and provide JSON for a response, but Google cloud functions work differently, just having functions and nothing else.

Where should I store my database (using Google Cloud Services) and how can I get the data in the function? Should I call some kind of database API and process the result and send it to JSON inside the function?

+3


source to share


1 answer


With Google cloud features, you usually use a hosted database like Firebase Realtime Database for persistent storage. In this case, you are using GCF as the API gateway for the Firebase database.

You can also deploy your own virtual machine that runs the database of your choice. Then it will take over the role of the Firebase database in the previous example and you will still be using GCF as your API gateway.



In these two approaches, you have two microservices: the database itself is the service, and the GCF functions (functions) are the service that wraps the database.

The final option is to deploy the database in a GCF container that runs your functions. Then you can connect to this database from your function code without connecting to an external service. See this answer for more information on deploying a custom binary in a GCF container.

+2


source







All Articles