Direct Firebase Hosting Requests to Cloud Run Service Container (Rewrite Maps)

In the rewrites attribute of firebase.json, we can map a request URL path to a Cloud Run container to serve dynamic content. For example to direct all requests from yoursite.com/foo or PROJECT_ID.web.app/foo to a Cloud Run service (container/instance) called bar, you can put the following configuration in your firebase.json:

{
  "hosting": {
    // ...

    // Map requests from `/foo` (request path) `bar` service container
    "rewrites": [
      {
        "source": "/foo",
        "run": {
        "serviceId": "bar",  // "service name"
        "region": "us-central1"  // optional (if omitted, default is us-central1)
      },
      // ...
    ]
  },

  // ...
}

Once you deploy firebase hosting with the new configuration – firebase deploy --only hosting – the Cloud Run service will be accessible at:

  • FIREBASE_PROJECT_ID.web.app/foo or FIREBASE_PROJECT_ID.firebaseapp.com/foo.
  • yoursite.com/foo, if you have a connected custom domain.

As soon as the first request goes to /foo, a container for the service will be started up that’ll serve the response. All the widely used HTTP methods are supported – GETPOSTHEADPUTDELETEPATCH, and OPTIONS.

Leave a Reply

Your email address will not be published.