1

In a Node.js script, How can I check if I'm running in Google Cloud VM or locally?

I'm not using Google App Engine.

Is it possible to do this without making any network requests, synchronously?

3
  • 1
    This might also be of value ... stackoverflow.com/questions/30911775/… Commented May 10, 2019 at 22:57
  • 1
    The correct method as mention by Kolban is to check the instance metadata. Environment variables are not reliable. Commented May 10, 2019 at 23:03
  • Thanks @Kolban! That's what I was looking for (although would be better to not have to make a network request). Commented May 10, 2019 at 23:24

2 Answers 2

3

You could, for example, check for the various environment variables that will be set, such as GOOGLE_CLOUD_PROJECT or GAE_INSTANCE, accessible from a Node.js script as:

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

1 Comment

is this the offial way?
2

Edit /etc/environment on the VM, append the following:

GCLOUD=1 

And then in Node:

const isRunningInVM = Boolean(process.env.GCLOUD) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.