Creating an API using Cloud Functions (Node.js) | C2C Community
Solved

Creating an API using Cloud Functions (Node.js)

  • 29 September 2022
  • 4 replies
  • 53 views

Userlevel 1

I am currently evaluating different Google Cloud products in trying to set up an API for developers to use in a mobile app.

 

How should I evaluate thinking between:

  • A single Cloud Function using Express.js to create multiple routes in an API (accessed via API Gateway), vs.
  • A single Cloud Function per API endpoint (accessed via API Gateway)

 

 

icon

Best answer by yuval 29 September 2022, 06:26

View original

4 replies

Userlevel 4
Badge +3

Hello @davidressler 

Cloud Functions are based on Cloud Run. I like Cloud Run because you can build and run your own containers instead of having to think about supported environments in Cloud Functions.

Read “Getting started with API Gateway and Cloud Run”[1].

Regarding single Cloud Run service for multiple methods vs multiple Cloud Run services - consider resources usage by different methods, if you have a method that uses more memory or it is CPU intensive you might want to separate it to a different Cloud Run service with larger container size. You might want different scaling for different methods. You can also run each Cloud Run service using a separate service account with different roles - one service needs only permission to write to a specific GCS bucket and another needs permission to read a secret from Google Secret Manager.

 

[1] https://cloud.google.com/api-gateway/docs/get-started-cloud-run

Userlevel 7
Badge +58

Hi @davidressler,

have you checked @yuval’s answer? What do you think? Does it answer to your question?

In case you need to better understand read the following post and watch the recording

 

Userlevel 1

@yuval -- does that mean it makes more sense to organize Cloud Run instances by permissions needed? So, if it’s fine to have one Cloud Function (supported by Cloud Run) that accounts for a decent chunk of the CRUD operations with a Cloud SQL instance and other service providers (and then uses a service like Express.js to further route requests), an additional instance for interfacing with Cloud Storage, etc.? And then abstract all of that way with API Gateway?

Userlevel 4
Badge +3

Hey @davidressler.

There are a few considerations for a single service vs multiple services:

Scaling independently / permissions to access Google Cloud APIs from different services / implementing services with different languages or frameworks / segmenting ingress type - services that are accessed externally vs internally / authenticated services vs unauthenticated services / services egrees through VPC seeverless connector vs services that egress directly to the internet.

Reply