Vinaya
Vinaya

Reputation: 29

How to get firebase project region value to be used in firebase cloud function

I have created 2 projects in two different locations. say us-central1 and asia-northeast1. Now for these two different projects I wants to deploy the cloud functions in their respective regions.

const function_location = 'asia-northeast1';// I want a region of the project selected through npm 
exports.AddWebLogs = functions.region(function_location).firestore.document(............

How can I get the project region value either in environment variables or somewhere in process.env so that I can use it to set that region in cloud function.

REACT_APP_API_KEY             = xyz
REACT_APP_AUTH_DOMAIN         = xyz
REACT_APP_DATABASE_URL        = xyz
REACT_APP_PROJECT_ID          = xyz
REACT_APP_STORAGE_BUCKET      = xyz
REACT_APP_MESSAGING_SENDER_ID = xyz

Upvotes: 2

Views: 623

Answers (1)

sllopis
sllopis

Reputation: 2368

If I understood your question correctly, you have two projects: Project A is located in us-central1 and Project B is located in asia-northeast1. You would like to know their respective regions and store this in an environment variable that you can use later on when deploying your Cloud Function.

To see what your default region and zone settings are in your project, you can use the cloud command-line tool:

gcloud compute project-info describe --project [PROJECT_ID]

To set a variable using the gcloud command-line tool, use the --set-env-vars flag at deploy time:

gcloud functions deploy FUNCTION_NAME --set-env-vars myRegion=region

You can access your environment variable at runtime. At runtime, environment variables are accessible via the process.env property in Node.js:

gcloud functions deploy envVar --runtime nodejs10 --set-env-vars myRegion=region --trigger-http

Then, you can use:

exports.envVar = (req, res) => {
  // Sends 'region' as response
  res.send(process.env.myRegion);
};

Hope this helps.

Upvotes: 1

Related Questions