Reputation: 22518
I have two Google Cloud Platform projects (staging and production). I would like my application deployed on Google Compute Engine instance to automatically detected the project id. What is the way to do it?
Upvotes: 6
Views: 6137
Reputation: 341
In my environment I've got:
curl: (6) Could not resolve host: metadata.google.internal
The following worked for me:
PROJECTID=`gcloud projects list | awk 'FNR>1 {print$1}'`
which returns the project details, which is then parsed for project id.
Upvotes: 0
Reputation: 1571
You can determine project ID directly in shell script with:
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
Or use metadata lib for your language to get required data.
I'm able to get project ID in Golang with:
import "cloud.google.com/go/compute/metadata"
...
var ProjectId, _ = metadata.ProjectID()
Upvotes: 2
Reputation: 181
You can query the metadata server programmatically for a variety of information, including the project id.
For the project id:
$ curl "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google"
This will return your project id, eg: my-gcp-project
Or if you need the numeric project id:
$ curl "http://metadata.google.internal/computeMetadata/v1/project/numeric-project-id" -H "Metadata-Flavor: Google"
This will return the numeric project id, eg: 0123456789
Upvotes: 10