Get the metadata from a Google Cloud Platform environment.
$ npm install --save gcp-metadata
const gcpMetadata = require('gcp-metadata');
const isAvailable = await gcpMetadata.isAvailable();
const data = await gcpMetadata.instance();
console.log(data); // ... All metadata properties
const data = await gcpMetadata.instance('hostname');
console.log(data); // ...Instance hostname
const projectId = await gcpMetadata.project('project-id');
console.log(projectId); // ...Project ID of the running instance
const data = await gcpMetadata.instance('service-accounts/default/email');
console.log(data); // ...Email address of the Compute identity service account
const data = await gcpMetadata.instance({
property: 'tags',
params: { alt: 'text' }
});
console.log(data) // ...Tags as newline-delimited list
await gcpMetadata.instance({
headers: { 'no-trace': '1' }
}); // ...Request is untraced
In some cases number valued properties returned by the Metadata Service may be
too large to be representable as JavaScript numbers. In such cases we return
those values as BigNumber
objects (from the bignumber.js library). Numbers
that fit within the JavaScript number range will be returned as normal number
values.
const id = await gcpMetadata.instance('id');
console.log(id) // ... BigNumber { s: 1, e: 18, c: [ 45200, 31799277581759 ] }
console.log(id.toString()) // ... 4520031799277581759
- GCE_METADATA_HOST: provide an alternate host or IP to perform lookup against (useful, for example, you're connecting through a custom proxy server).
For example:
export GCE_METADATA_HOST = '169.254.169.254'
- DETECT_GCP_RETRIES: number representing number of retries that should be attempted on metadata lookup.