5

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!

0

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Gabe - turns out os.environ from dev is almost completely different than os.environ in prod :P so any variable other than PATH or PWD will work lol
Nice! Well there ya go! :) I wouldn't necessarily RELY on the built-in os.environ that GCF gives you, just because I don't think we guarantee it, but you could totally set your own if you're paranoid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.