Creating a VM Instance with a Custom Hostname

When you create a VM instance, Google Cloud Platform (GCP) creates an internal DNS name from the instance name. Unless you specify a custom hostname, GCP uses the automatically created internal DNS name as the hostname it provides to the VM.

You can create a VM with a custom hostname by specifying any fully-qualified DNS name. Custom hostnames are useful to maintain conventions or to support requirements for applications that expect a particular hostname.

Limitations

  • You must manually configure the DNS record for your custom hostname. Custom hostnames are not resolved by the automatically created records provided by Compute Engine internal DNS. You can use any of the following to host the DNS record for the custom hostname:

  • You cannot change a custom hostname after you have created the VM.

  • Even when you specify a custom hostname, GCP still creates the Compute Engine internal DNS name. You can still contact your VM by using this automatically created DNS record.

Requirements

The hostname must conform to RFC 1035 requirements for valid hostnames. To meet this requirement, hostnames must contain a series of labels matching the regular expression [a-z]([-a-z0-9]*[a-z0-9])?, and each label is concatenated with a dot. Each label can be 1-63 characters long, and the entire sequence must not exceed 253 characters.

Before you begin

gcloud

Follow the same instructions to create an instance from an image or a snapshot, add the --hostname=[HOST_NAME] flag and use the gcloud beta compute instances create command:

gcloud beta compute instances create [INSTANCE_NAME] \
--hostname=[HOST_NAME]

where:

  • [INSTANCE_NAME] is the name of the instance.
  • [HOST_NAME] is the fully qualified domain hostname that you want to assign.

For example, to create an instance called myinstance with the custom hostname test.example.com. You can run the following command:

gcloud beta compute instances create myinstance \
--hostname=test.example.com

API

Follow the API instructions to create an instance from an image or a snapshot, but specify the hostname field in the request body and use the beta API URL.

POST https://www.googleapis.com/compute/beta/projects/[PROJECT_ID]/zones/[ZONE]/instances

{
 "name": "[INSTANCE_NAME]",
 "hostname": "[HOST_NAME]",
 ...
}

where:

  • [INSTANCE_NAME] is the name of the instance.
  • [HOST_NAME] is the fully qualified domain hostname that you want to assign.
Was this page helpful? Let us know how we did:

Send feedback about...

Compute Engine Documentation