Neurus
Neurus

Reputation: 887

How to know if Google Cloud function is running in dev or prod?

Trying to find out if there is a programmatic way to know, when running a cloud function, if it's running in dev (locally with functions-framework) or in prod (deployed). E.g. in Google AppEngine, we could know if it was running in dev or prod by:

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running in prod!
else:
    # Running in dev!

Is there anything similar with GCF?

Thanks!

Upvotes: 5

Views: 2023

Answers (1)

Gabe Weiss
Gabe Weiss

Reputation: 3342

So, I found this blog post: https://rominirani.com/google-cloud-functions-tutorial-using-environment-variables-20b4f0f82aa0

Turns out, there's lots of env vars in the GCF environment when it deploys. Now, having said that, I don't know how many of those are present in the test framework but I suspect not many of them. So you could rely on checking for the presence of one of the env vars that's in the production deployment and isn't present in the dev.

e.g. FUNCTION_REGION is one of the vars that probably isn't set up for the framework since the framework likely doesn't care what region it's in.

It's a bit hacky, but it'll work.

Even hackier, if you wanted, you could when you deploy the GCF, set your own env var that you rely on (that way, in the case of any of those vars changing over time, you're still safe).

Upvotes: 2

Related Questions