Ashar Rahman
Ashar Rahman

Reputation: 325

How do I invoke firebase cloud functions within the client using HTTP if the functions URLS will change depending on environment?

For example, my firebase cloud functions URL will be different in my emulator instance, my development Firebase project (online), and my production Firebase project (online).

On the client side, I would hardcode the URL and change the URL for each invocation upon deployment to match the function HTTP URL for the particular environment I am in. This is unsustainable and tedious. For example, if I had 10 invocations I would have to go through and change the fetch URLS each time I changed environments. If I wanted to test them in a non-prod environment later I would have to change them back. Is there any solution to this, I am using one codebase.

Per Google Cloud docs, eventually, the functions will feature deterministic URLS which means I can programmatically do this in the future using the .env. files but until then I'm not sure how to do this.

Google cloud docs declaration of deterministic functions

TLDR I am trying to make it so that regardless of the environment the client is in it will be able to use the same URL across them to execute the respective function. i.e a DEV client would execute a DEV cloud function that would communicate with DEV servers.

Upvotes: 0

Views: 148

Answers (1)

John Hanley
John Hanley

Reputation: 81454

There are several options:

  1. Use an HTTP load balancer and assign a custom domain name.

Set up a global external HTTP(S) load balancer (classic) with Cloud Run, App Engine, or Cloud Functions

  1. Use Firebase Hosting. See this answer.

  2. Migrate your functions to Cloud Run and use custom domains.

Mapping custom domains

My preference is the third option, as Cloud Run is the big brother to Cloud Functions with many features that I wish Cloud Function had.

Firebase Hosting is another good option that brings another set of features for web-based applications.

The first option is a good choice for hiding Functions behind a proxy that protects your functions.

Upvotes: 1

Related Questions