External IP address configuration is an essential piece of instance configuration. If you host a website or application or point it to the external domain name, it is desired to have a configured static IP address.
Once the instance is created with an external IP address, it is ephemeral. When your instance runs, it might have an IP address of 35.210.248.81, but it might be different after stopping it and starting again.
Therefore, for such use cases, we should reserve external IP addresses.
Console
In the VPC section of the Cloud console, we can reserve an external static IP address:
Figure 4.60 – The VPC section where the static external IP address can be configured
Once we click the RESERVE EXTERNAL STATIC ADDRESS button, we are navigated to a new page where we can provide information about the IP address.
Figure 4.61 – The external static IP address reservation page
Once the RESERVE button is clicked, the IP address starts creating.
To attach the newly created external IP address to the running instance in the IP addresses section, we need to click the Attach to dropdown and select the desired instance:
Figure 4.62 – The external static IP address attached to an instance
After a moment, IP address assignment to the running instance is completed.
Command line
To make the external static IP address reservation, we will use the gcloud command:
gcloud compute addresses create ADDRESS_NAME –region=REGION_NAME –subnet=SUBNET –addresses=IP_ADDRESS
Once the IP address is created, we can change the instance’s configuration to use the newly created IP address.
The process requires us to get the instance details, remove the accessConfigs section of the instance, and then update the config with the new IP address.
To retrieve accessConfigs, we run the gcloud command:
gcloud compute instances describe VM_NAME
Figure 4.63 – The output of gcloud compute instances describes VM_NAME, with a focus on the accessConfigs section where the IP address is defined
Before we can add a new accessConfigs section, we need to delete the existing one using the following command:
gcloud compute instances delete-access-config VM_NAME –access-config-name=”ACCESS_CONFIG_NAME”
The following screenshot shows the removal of the accessConfigs section from the instance:
Figure 4.64 – accessConfig has been removed from the instance
Once the accessConfigs section is deleted, the previously associated IP address is removed. To attach the IP address, we need to use the following command:
gcloud compute instances add-access-config INSTANCE_NAME –access-config-name=”ACCESS_CONFIG_NAME” –address=ADDRESS_IP
Replace ADDRESS_IP with the actual IP address and not its name. We used the following command to attach the external IP to our instance:
gcloud compute instances add-access-config instance-1 –access-config-name=”External Static IP” –address=35.210.248.80
After a moment, the instance gets a new external IP address:
Figure 4.65 – The instance with a new external static IP
IP addresses in Google Cloud have the following essential characteristics:
- Used IPs can be version 4 and version 6
- There are two network tiers – a premium and a standard one
- IPs can be global and regional
We will discuss the networking section in upcoming chapters, but if you wish to learn more about network tiers, you can visit the Google Cloud documentation: https://cloud.google.com/network-tiers/docs/overview.