Determining the public IP address of your Google Cloud Platform (GCP) virtual machine instance is a common task for any GCP admin or developer. But did you know there are a few different ways to get that external IP right from your VM?
In this comprehensive guide, I‘ll provide some insider tips and tricks on the various methods to find the external IP on Google Cloud.
Why Do You Need the External IP?
First, let‘s discuss why you would even need to know the public IP address of your GCP instance.
There are a few common use cases:
- Configuring firewall rules to allow traffic to your VM from the Internet
- Enabling SSH or RDP access from your local machine to the instance
- Integrating the VM with external web services that whitelist specific IP addresses
- Remote troubleshooting when you can‘t resolve a DNS name
- Automating scripts that pass the IP address into other systems
Basically, knowing the external IP gives you more control and visibility into how your VM instances communicate over the public internet.
The Instance Networking Model
Before we dive into the methods, it‘s useful to understand how networking and IP addresses are allocated on GCP:
-
When an instance is launched, it receives an internal RFC 1918 IP address from a subnet range you define. This internal IP is only routable within the VPC network.
-
The instance also receives an external IP address that‘s internet routable. By default, this external IP is ephemeral and can change on reboot.
-
You can reserve a static external IP and attach it to the instance to prevent changes.
-
External IPs are regional resources, while internal IPs are subnet-based.
-
Instances have no awareness of their external IP from the inside. The internal IP takes precedence.
Okay, with that quick primer out of the way, let‘s look at how to find that external IP!
Method #1 – gcloud CLI
The gcloud CLI provides a handy command to list out all the IP addresses allocated in your Google Cloud project:
gcloud compute addresses list
This prints out a table with the resource name, region, IP address, and status:
NAME REGION ADDRESS STATUS
server1 us-east1 35.237.24.1 IN_USE
server2 us-east1 35.196.118.2 IN_USE
To find your instance‘s specific external IP, lookup the row where:
- The name matches your instance name
- The status is
IN_USE
That row shows your public IP!
Now one catch is that this list contains ALL IP addresses in use across the entire project. So you may have to filter down to just your instance:
gcloud compute addresses list --filter="name=(INSTANCE_NAME)"
This makes it easy to pinpoint the desired IP if you have a long list of project resources.
According to Google, over 5 million businesses rely on GCP as their cloud provider. So chances are, you‘ll have quite a few IP addresses allocated!
Method #2 – Instance Metadata Service
Here‘s a slick way to get the external IP without any CLI installed.
GCP instances have a metadata server running at http://metadata.google.internal. This metadata server exposes information about the instance like service accounts, SSH keys, and networking details via a REST API.
We can craft a curl request to query the metadata API for just the external IP:
EXTERNAL_IP=$(curl -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip)
Breaking down this request:
- The base URL is:
http://metadata.google.internal/computeMetadata/v1
- We append the path
/instance/network-interfaces/0/access-configs/0/external-ip
to get the external IP specifically. - The
-H "Metadata-Flavor: Google"
header is required.
The metadata API will return only information about the current instance. So there‘s no need to filter like the gcloud command.
However, one downside is the instance must have internet access to query the API. But for most use cases, this shouldn‘t be a problem.
Method #3 – Query the GCP REST API
This method is a bit more advanced but useful in automated workflows. We can query the official GCP Compute REST API to get the external IP:
EXTERNAL_IP=$(curl -H "Authorization: Bearer $(gcloud auth print-access-token)" https://www.googleapis.com/compute/v1/projects/${PROJECT}/zones/${ZONE}/instances/${INSTANCE_NAME} | jq -r .networkInterfaces[0].accessConfigs[0].natIP)
Here we are making a request to the GCP REST API to get the full instance resource data, and then parsing it with jq to extract just the external IP field.
The key points:
- We use the
gcloud
CLI to generate an access token for authorization. This prevents needing a service account key. - Replace ${PROJECT}, ${ZONE}, and ${INSTANCE_NAME} with your values.
- The jq command at the end parses the JSON output to just return the natIP field.
This returns the same external IP but through the official API, which opens up more automation possibilities.
External IP Gotchas
Before we wrap up, be aware of a few "gotchas" when dealing with external IPs in GCP:
-
The external IP can change on instance reboot if not a static reserved IP.
-
Firewall rules tend to block the metadata API and REST API access by default. Make sure to allow this traffic.
-
External IPs are regional resources. Make sure to lookup the IP in the instance‘s region.
-
You may see a different public IP from inside vs outside the VPC if there is a NAT gateway in use.
Recap and Conclusion
Finding the external IP on a Google Cloud VM is easy once you know where to look:
- gcloud CLI – List all IPs project-wide
- Instance metadata service – Query instance metadata
- GCP REST API – Programmatically get the external IP
No matter which method you use, having visibility and access to the public IP opens up more flexibility in securing and connecting your GCP resources.
So now you‘re armed with a few different options to find that pesky external IP! Let me know if any other questions come up.