Reserving a static internal IP address

If a virtual machine (VM) instance requires a fixed internal IP address that does not change, you can obtain a static internal IP address for that VM using one of the following options:

To learn how to manage secondary internal IP addresses, read Alias IP ranges.

In Compute Engine, each VM instance can have multiple network interfaces. Each interface can have one external IP address, one primary internal IP address, and one or more secondary internal IP addresses. Forwarding rules can have external IP addresses for external load balancing or internal addresses for internal load balancing. To learn about IP addresses, read the IP Addresses documentation.

Static internal IPs provide the ability to reserve internal IP addresses from the IP range configured in the subnet, then assign those reserved internal addresses to resources as needed. Reserving an internal IP address takes that address out of the dynamic allocation pool and prevents it from being used for automatic allocations. Reserving static internal IP addresses requires specific IAM permissions so that only authorized users can reserve a static internal IP address.

With the ability to reserve static internal IP addresses, you can always use the same IP address for the same resource even if you have to delete and recreate the resource.

To reserve a static external IP address instead of an internal IP address, read Reserving a static external address.

Before you begin

Permissions

To reserve and manage static internal IP addresses, you need to be granted the compute.networkAdmin role. To learn more about roles, read Compute Engine IAM Roles.

Restrictions

  • You cannot change the internal IP address of an existing resource. For example, you cannot assign a new static internal IP address to a running VM instance. You can, however, promote the ephemeral internal IP address of a resource to a static internal IP so that the address remains reserved even after the resource is deleted.

  • The number of static internal IP addresses that you can reserve cannot exceed your project's quota. For more information, see the per-project quotas in the VPC documentation.

  • Only one resource at a time can use a static internal IP address.

  • There is no way to tell whether an IP address is static or ephemeral after it has been assigned to a resource, except to compare the IP address against the list of static internal IP addresses reserved to that project. Use the addresses list sub-command to see a list of static internal IP addresses available to the project.

  • Reserving a static internal IP address is only supported for VPC networks. It is not supported for legacy mode networks.

  • Deleting a resource does not automatically release a static internal IP address. You must manually release static internal IP addresses when you no longer require them.

How to reserve a static internal IP address

You can reserve a static internal IP address before creating the associated resource, or you can create the resource with an ephemeral internal IP address and then promote that ephemeral IP address to a static internal IP address.

In order to use a static internal IP address, you must have a VPC network in place for your project. Read Using VPC Networks to learn how to create your VPC network.

Reserve a specific address and then associate it with a specific resource

In this scenario, you separately reserve a static internal IP address and then assign it to a resource. In summary:

  1. Create a subnet from your VPC network.

  2. Reserve an internal IP address from the subnet's primary IP range. This step creates an internal IP address resource containing that specific internal IP address. This step also prevents Google Cloud from automatically allocating that address as an ephemeral address.

  3. Use the reserved internal IP address by associating it with a VM instance or an internal load balancer when you create the VM or load balancer resource.

Specify an ephemeral internal IP address for a resource and then promote the address

In this scenario, you promote an ephemeral internal IP address that is still attached to a resource to a static internal IP address.

  1. Create a subnet.

  2. Create a VM instance or an internal load balancer with either an automatically allocated ephemeral IP address or a specifically chosen IP address.

  3. Promote the internal IP address to a static internal IP address.

The two methods are outlined in the diagram below.

Internal IP reservation.

Reserving a new static internal IP address

Before you can reserve a new static internal IP address, you must create a VPC network with a subnet.

To reserve a new static internal IP address, either automatically allocated or an unused address from an existing subnet, you can use the Google Cloud Console, the gcloud tool, or the API.

Console

  1. In the Cloud Console, go to the VPC networks page.

    Go to the VPC networks page

  2. Identify the VPC network that you want to associate with your instance.
  3. Go to the VM instances page.

    Go to the VM instances page

  4. Select your project.
  5. Click Create an Instance and specify a name for your instance.
  6. Select the Region where your VPC network is located.
  7. Select a Zone within that region.
  8. Complete the other fields for your instance.
  9. Expand the Management, security, disks, networking, sole tenancy menu.
  10. Click on Networking.
  11. Click Networking interfaces to edit.
  12. Under Network, select the VPC network you created.
  13. Choose the subnet.
  14. Under Primary Internal IP select Reserve static IP address.
  15. Complete the rest of the networking options.
  16. Click Done.
  17. Complete your instance requirements.
  18. Click Create.

gcloud

Using the gcloud tool, run the compute addresses create command:

gcloud compute addresses create ADDRESS_NAME [ADDRESS_NAME..] \
    --region REGION --subnet SUBNETWORK \
    --addresses IP_ADDRESS

Replace the following:

  • ADDRESS_NAME: The desired names of one or more addresses to create.
  • REGION: The region for this request.
  • SUBNETWORK: The subnet for this internal IP address.
  • IP_ADDRESS: The IP address to reserve, which must be within the subnet's primary IP range. If unspecified, one will be automatically allocated from the subnet.

For example, to reserve an automatically allocated internal IP address from a subnet:

gcloud compute addresses create example-address-1 \
    --region us-central1 --subnet subnet-1

To reserve a specific internal IP address from a subnet:

gcloud compute addresses create example-address-1 \
    --region us-central1 --subnet subnet-1 --addresses 10.128.0.12

You can create multiple addresses by passing in more than one address name. However, all the addresses will be reserved in the same subnet. For example:

gcloud compute addresses create example-address-1 example-address-2 \
    --region us-central1 --subnet subnet-1 \
    --addresses 10.128.0.12,10.128.0.13

API

Call the addresses.insert method:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/addresses

The body of the request must include the addressType which should be INTERNAL, the name of the address, and the subnetwork the IP address belongs to. You can let the system automatically allocate an IP address for you, or specify the address for a specific IP address. The IP address must belong to the subnet's primary IP address range.

{
  "addressType": "INTERNAL",
  "name": "ADDRESS_NAME",
  "subnetwork": "regions/REGION/subnetworks/SUBNETWORK",
  "address": "IP_ADDRESS"
}

For example:

POST https://compute.googleapis.com/compute/v1/projects/example-project/regions/us-central1/addresses

{
  "name": "example-address-1",
  "addressType": "INTERNAL",
  "subnetwork": "regions/us-central1/subnetworks/my-custom-subnet",
  "address": "10.128.0.12"
}

Promoting an in-use ephemeral internal IP address to a static address

If you have ephemeral IP addresses that are currently in use, you can promote these addresses to static internal IP addresses so the addresses remain with your project until you actively remove them.

Before you reserve an existing IP address, you will need the value of the IP address you want to promote. Make a describe request to the resource to get the IP address. For example, if promoting an IP address currently in use by a VM instance, use:

gcloud compute instances describe INSTANCE_NAME --zone ZONE | grep "networkIP"

The gcloud tool returns the networkIP value, which is the internal IP being used by the resource. For example:

networkIP: 10.240.0.2

Next, promote the address.

Console

To promote an ephemeral internal IP address to a static IP address in the console:

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Select your project.
  3. Select the instance you want to change.
  4. Click on the Edit tool at the top of the page.
  5. Click on the edit tool for Network Interfaces.
  6. Under Internal IP type, select Static.
  7. Complete the information for a static IP address.
  8. Click Done to save your changes.
  9. Click Save to update your instance.

gcloud

To promote one or more existing IP address, use the compute addresses create command and provide the --addresses flag with the explicit internal IPs to promote:

gcloud compute addresses create ADDRESS_NAME_1 [ADDRESS_NAME_2..] \
    --addresses IP_ADDRESS_1,[IP_ADDRESS_2,..] \
    --region REGION \
    --subnet SUBNETWORK

Replace the following:

  • ADDRESS_NAME: The desired names of the address. Declare the names in the same order that you declare the IP addresses. In this case ADDRESS_NAME_1 corresponds with IP_ADDRESS_1 and ADDRESS_NAME_2 corresponds with IP_ADDRESS_2.
  • IP_ADDRESS_1,[IP_ADDRESS_2,...]: The IP addresses to promote. For example, 10.128.1.9.
  • REGION: The region to reserve this address.
  • SUBNETWORK: The subnetwork for this request.

For example:

gcloud compute addresses create example-address-1 example-address-2 \
    --addresses 10.128.4.90,10.128.0.232 \
    --region us-central1 \
    --subnet subnet-1

API

Call the addresses.insert method:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/addresses

The body of the request must include the addressType which should be INTERNAL, the name of the address, the address to promote, and the subnetwork the IP address belongs to:

{
  "addressType": "INTERNAL",
  "address": "IP_ADDRESS",
  "name": "ADDRESS_NAME",
  "subnetwork": "regions/REGION/subnetworks/SUBNETWORK"
}

For example:

POST https://compute.googleapis.com/compute/v1/projects/example-project/regions/us-central1/addresses

{
  "name": "example-address-1",
  "addressType": "INTERNAL",
  "address": "10.128.0.2",
  "subnetwork": "regions/us-central1/subnetworks/my-custom-subnet"
}

Creating a VM instance with a specific internal IP address

You can choose a specific internal IP address to assign to an instance when you create the instance. The IP address must be a valid IP address of the subnet of the instance, and the IP address must not already be in use. The IP address remains attached to the instance until you delete the instance, which releases the IP address back into the pool. If you stop and restart the instance, the instance retains the same internal IP address.

If you do not specify an IP address, Compute Engine automatically allocates one from the subnet or network. You can specify an internal IP address using the Google Cloud Console, the gcloud tool, or the Compute Engine API.

Console

  1. In the Cloud Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click Create instance.
  3. On the Create a new instance page, fill in the properties for your instance.
  4. Expand the Management, security, disks, networking, sole tenancy section.
  5. Click Networking.
  6. Under Network interfaces, click on the default network interface to edit it.
  7. Optionally, under Network and Subnetwork, select the network and subnetwork that you want to use.
  8. Under Primary internal IP, select an internal IP address option from the drop-down list.
  9. Click Done to finish modifying the network interface.
  10. Click Create to create the instance.

gcloud

Specify a valid IP address when creating the instance:

gcloud compute instances create INSTANCE_NAME
     --private-network-ip IP_ADDRESS

Replace the following:

  • INSTANCE_NAME: The name of the instance you want to create.
  • IP_ADDRESS: The IP address you want to assign.

If you are using a custom subnet mode network, you must also specify the subnet using the --subnet SUBNET parameter.

API

To create an instance with a static internal IP address, use the Compute Engine API. Make a request to create a new instance, as you would normally but explicitly provide the networkInterfaces[].networkIP property with the internal IP you want to use. For example:

{
  "name": "INSTANCE_NAME",
  "machineType": "zones/us-central1-f/machineTypes/f1-micro",
  "networkInterfaces": [{
    "accessConfigs": [{
      "type": "ONE_TO_ONE_NAT",
      "name": "External NAT",
     }],
    "network": "global/networks/default",
    "networkIP": "IP_ADDRESS"
  }],
  "disks": [{
     "autoDelete": "true",
     "boot": "true",
     "type": "PERSISTENT",
     "initializeParams": {
        "sourceImage": "projects/debian-cloud/global/images/v20150818"
     }
   }]
 }

Replace the following:

  • INSTANCE_NAME: The name of the instance.
  • IP_ADDRESS: The IP address to assign to the instance.

If you delete an instance with a specified IP address, the address goes back into the unallocated address pool. If you need an internal IP address to persist beyond the life of the instance, you can reserve a static internal IP address.

Creating an internal load balancer with a static internal IP address

To create an internal load balancer that uses a static internal IP address, follow the instructions at:

Using a static internal IP address for a secondary network interface

When you create a VM instance with multiple network interfaces, you can use a reserved static internal IP address for both primary and secondary network interfaces.

To assign a static internal IP address to a secondary network interface, do the following:

  1. First, reserve a static internal IP address. For example, the following example reserves an address called my-second-ip-address in the us-central1 region. Because the command omits the --addresses flag, Compute Engine reserves a random IP address:

    gcloud compute addresses create my-second-ip-address \
        --region us-central1 --subnet subnet-b
  2. Use the address as the IP address for the secondary interface when you create a VM instance. For example:

    gcloud compute instances create my-instance \
        --image-family IMAGE_FAMILY \
        --image-project IMAGE_PROJECT \
        --network-interface subnet=subnet-a,no-address \
        --network-interface \
          subnet=subnet-b,private-network-ip=my-second-ip-address,no-address

Using a static internal IP with Shared VPC

You can create a reserved static internal IP in a shared subnet of a Shared VPC network. The IP address object itself is created in the same service project as the resource that will use it, even though its value comes from the range of available IPs in the selected shared subnet of the Shared VPC network. Refer to these pages for more information about this use case:

Deleting a static internal IP address

You can delete a static internal IP address if you no longer need it. You can delete an address whether or not it is currently being used by another resource. If the address is being used by a resource, it remains attached to the resource until the resource is deleted, in which case, the address is returned to the pool of available addresses for other projects.

Console

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Select your project.
  3. Select the VM instance from the project list.
  4. Select the Edit tool at the top of the page.
  5. Click the edit tool for Network Interfaces.
  6. Change the Internal IP type from Static to Ephemeral.
  7. Select Release to release your static IP address.
  8. Click Done to save your changes.

    Your instance continues to use the static IP address until the instance is deleted or restarted. After that occurs, the IP address is returned to the subnet's default pool of available internal IP addresses.

gcloud

Using the gcloud tool, run the compute addresses delete command:

gcloud compute addresses delete ADDRESS_NAME \
    --region REGION

Replace the following:

  • ADDRESS_NAME: The name of the address to delete.
  • REGION: The region the address belongs to.

For example:

gcloud compute addresses delete example-address-to-delete \
    --region us-west1

API

Call the addresses.delete method:

DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/addresses/ADDRESS_NAME

For example, to delete an address in the us-west1 region:

DELETE https://compute.googleapis.com/compute/v1/projects/myproject/regions/us-west1/addresses/example-address-to-delete

What's next