Skip to content

Instantly share code, notes, and snippets.

@steren
Last active April 16, 2021 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steren/6bc5e011632e8c0f13cf8578d7944d97 to your computer and use it in GitHub Desktop.
Save steren/6bc5e011632e8c0f13cf8578d7944d97 to your computer and use it in GitHub Desktop.
Query GCP metadata server
$ curl -H "Metadata-Flavor:Google" http://metadata.google.internal/computeMetadata/v1/project/project-id
// https://cloud.google.com/compute/docs/storing-retrieving-metadata
const express = require('express');
const axios = require('axios');
const app = express();
const axiosInstance = axios.create({
baseURL: 'http://metadata.google.internal/',
timeout: 1000,
headers: {'Metadata-Flavor': 'Google'}
});
app.get('/', (req, res) => {
let path = req.query.path || 'computeMetadata/v1/project/project-id';
axiosInstance.get(path).then(response => {
console.log(response.status)
console.log(response.data);
res.send("" + response.data);
});
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log('Hello world listening on port', port);
});
{
"name": "metadata",
"version": "1.0.0",
"description": "Metadata server",
"main": "app.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"axios": "^0.18.0",
"express": "^4.16.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment