What is gcp-metadata?
The gcp-metadata npm package is a library that allows users to query Google Cloud Platform's (GCP) metadata service for information about the currently running instance and project. This can be useful for applications that need to understand their environment within GCP, such as retrieving instance attributes or project details.
What are gcp-metadata's main functionalities?
Instance Metadata
Retrieve metadata of the current GCP instance. This includes details like the instance ID, zone, and custom metadata.
const gcpMetadata = require('gcp-metadata');
async function getInstanceMetadata() {
if (await gcpMetadata.isAvailable()) {
const instanceMetadata = await gcpMetadata.instance();
console.log(instanceMetadata);
}
}
getInstanceMetadata();
Project Metadata
Fetch metadata of the current GCP project. This can include project-wide attributes like project ID and numeric project number.
const gcpMetadata = require('gcp-metadata');
async function getProjectMetadata() {
if (await gcpMetadata.isAvailable()) {
const projectMetadata = await gcpMetadata.project();
console.log(projectMetadata);
}
}
getProjectMetadata();
Check Metadata Server Availability
Determine if the GCP metadata server is available and reachable from the instance.
const gcpMetadata = require('gcp-metadata');
async function checkAvailability() {
const isAvailable = await gcpMetadata.isAvailable();
console.log('Metadata service available:', isAvailable);
}
checkAvailability();
Other packages similar to gcp-metadata
aws-sdk
The AWS SDK for JavaScript allows developers to interact with AWS services. It includes a module for querying the AWS EC2 instance metadata service, which is similar to what gcp-metadata does for GCP.
gcp-metadata
Get the metadata from a Google Cloud Platform environment
$ npm install --save gcp-metadata
const gcpMetadata = require('gcp-metadata');
Access all metadata
const res = await gcpMetadata.instance();
console.log(res.data);
Access specific properties
const res = await gcpMetadata.instance('hostname');
console.log(res.data)
Access specific properties with query parameters
const res = await gcpMetadata.instance({
property: 'tags',
params: { alt: 'text' }
});
console.log(res.data)