0
votes

I have setup a ML model with basic API on Cloud Run and I want to call it from my a Firebase project.

They are both in the same project and I have tested Cloud Run independently:

  • Deploying a NextJS site that I am deploying to Firebase, so if I run it locally as NextJS site (next dev) calling my Cloud Run function over https it works fine:
  async rewrites() {
    return [
      {
        source: '/predict',
        destination: 'https://<cloud-run-domain>.run.app/predict',
      },
    ]
  }
    const response = await fetch('/predict', {
      method: 'POST',
      body: JSON.stringify(CallData)
    })

However, when I deploy it to Firebase and setup rewrites in firebase.json. I do not get any invocations on Cloud Run side too.

  "rewrites": [ {
    "source": "**",
    "run": {
      "serviceId": "<cloud-run-service-name>",
      "region": "europe-north1"
    }
  } ]

The POST calls returns 404

{
body: (...)
bodyUsed: true
headers: Headers {}
ok: false
redirected: false
status: 404
statusText: "Not Found"
type: "basic"
url: "http://localhost:5000/predict"
}

This example is form a local testing using firebase serve --only hosting but returns the same on deployed version as well.

How do I debug Firebase rewrites and networking to figure out where the break down happens when calling the Cloud Run function?

1

1 Answers

0
votes

You first register /api/:predict and then call fetch('/predict'), which might explain the 404.
I'd assume that wildcard "source": "**" would also rewrite index.html and everything else. The documentation / documentation doesn't show any ** wildcard, because it makes no sense.

The pattern to follow is clear (notice the source parameter, which maps the entry-point):

"hosting": {
 // ...

 // Directs all requests from the page `/helloworld` to trigger and run a `helloworld` container
 "rewrites": [ {
   "source": "/helloworld",
   "run": {
     "serviceId": "helloworld",  // "service name" (from when you deployed the container image)
     "region": "us-central1"  // optional (if omitted, default is us-central1)
   }
 } ]
}

But one can still could map various source to the same serviceId ...
the mere question is, if http://localhost:5000/api/predict would return.


Not sure if one can use functions.logger with Cloud Run, since I use it for Cloud Functions:

import * as functions from "firebase-functions";
functions.logger.info(`${email} was found; UID: ${user.uid}.`);

But this may permit to develop suitable logging for whatever is being processed.

Also see: https://cloud.google.com/logging/docs/view/logs-viewer-interface