4

I am playing with the Google Cloud Functions API and I´m confused how to use it the right way for a simple REST API to retrieve some data from a database and respond to the client with the data its need.

I´m 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 200 (OK) response with no data from a database.

Coming from MVC frameworks like Rails or Django I would write a controller action to retrieve some data from my database and render a JSON for the response but Google Cloud Functions is working different by just having functions and nothing else.

Where should I store my database (with Google Cloud Services) and how can I retrieve the data in a function? Do I have to call some kind of Database API and process the result and send it back as JSON inside a function?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dude
  • 1,045
  • 2
  • 15
  • 36

1 Answers1

2

With Google Cloud Functions you'd typically use a hosted database, such as the Firebase Realtime Database, for the persistent storage. In that scenario you'd use GCF as an API gateway to the Firebase Database.

You could also spin up your own VM that runs a database of your liking. That would then take the role of the Firebase Database in the previous example and you'd still use GCF as the API gateway.

In these two approaches you have two micro-services: the database itself is a service, and the GCF function(s) are a service that wraps the database.

A final option is to deploy a database into the GCF container that your functions run in. You could then connect to this database from your functions code, without having to connect to an external service. See this answer for some additional information on deploying a custom binary into the GCF container.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807