This tutorial walks you through the process of assigning an external IP address to a Google Cloud Compute Engine VM instance using the gcloud command-line tool.
Prerequisites
- A Google Cloud account with a project set up
- gcloud CLI installed and configured
- An existing VM instance in your project
Steps
- Check the current IP configuration
gcloud compute instances describe INSTANCE_NAME --zone=ZONE | grep -A 10 networkInterfaces
Replace INSTANCE_NAME and ZONE with your VM's details. This shows the current network interface configuration.
- Assign a static external IP First, reserve a static IP address in your project:
gcloud compute addresses create ADDRESS_NAME --region=REGION
Then, attach it to your VM:
gcloud compute instances delete-access-config INSTANCE_NAME --access-config-name="external-nat" --zone=ZONE
gcloud compute instances add-access-config INSTANCE_NAME --access-config-name="external-nat" --address=IP_ADDRESS --zone=ZONE
- Assign an ephemeral external IP If you just need a temporary IP (which changes on stop/start), use:
gcloud compute instances add-access-config INSTANCE_NAME --access-config-name="external-nat" --zone=ZONE
- Verify connectivity Test that the VM is reachable from the internet:
ping EXTERNAL_IP
Or SSH into the VM using the external IP:
gcloud compute ssh INSTANCE_NAME --zone=ZONE --tunnel-through-iap
Best Practices
- Use static IPs for services that require a fixed address, such as web servers or APIs.
- Use ephemeral IPs for temporary workloads or testing.
- Always secure your VM with firewall rules to restrict inbound traffic.
- Regularly review and release unused static IPs to avoid charges.
By following these steps, you can manage external network access for your Google Cloud VMs directly from the terminal, streamlining your cloud networking tasks.