<p>I have nodejs code running inside a pod. From inside the pod I want to find the zone of the node where this pod is running. What is the best way do do that? Do I need extra permissions?</p>

<p>I have not been able to find a library but I post the code that does it below. The getContent function was slightly adapted from this <a href="https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/" rel="nofollow noreferrer">post</a> This code should work inside a GKE pod or and GCE host.</p>

<p>Use it as following:</p>

<pre><code>const gcp = require('./gcp.js')
gcp.zone().then(z  =&gt; console.log('Zone is: ' + z))
</code></pre>

<p>Module: gcp.js</p>

<pre><code>const getContent = function(lib, options) {
  // return new pending promise
  return new Promise((resolve, reject) =&gt; {
    // select http or https module, depending on reqested url
    const request = lib.get(options, (response) =&gt; {
      // handle http errors
      if (response.statusCode &lt; 200 || response.statusCode &gt; 299) {
         reject(new Error('Failed to load page, status code: ' + response.statusCode));
       }
      // temporary data holder
      const body = [];
      // on every content chunk, push it to the data array
      response.on('data', (chunk) =&gt; body.push(chunk));
      // we are done, resolve promise with those joined chunks
      response.on('end', () =&gt; resolve(body.join('')));
    });
    // handle connection errors of the request
    request.on('error', (err) =&gt; reject(err))
    })
};

exports.zone = () =&gt; {
    return getContent(
        require('http'),
        {
            hostname: 'metadata.google.internal',
            path: '/computeMetadata/v1/instance/zone',
            headers: {
                'Metadata-Flavor': 'Google'
            },
            method: 'GET'
        }) 
}
</code></pre>
