Tutorial: Set up a domain by using Cloud DNS

This tutorial provides a walkthrough of the process for registeringa domain, setting up a sample web server, and using Cloud DNS to pointthe domain URL to the server.

If you are new to using Terraform for Google Cloud, seeGet started with Terraform.

Objectives

This tutorial demonstrates how to complete the following tasks:

  • Register a domain name by using Cloud Domains
  • Create a Compute Engine virtual machine (VM) instance
  • Run a basic Apache web server
  • Set up your domain using Cloud DNS
  • Update name servers
  • Verify your setup

Costs

There is a cost associated with registering a domain name.For Cloud Domains pricing, seeCloud Domainspricing.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. Enable the Compute Engine API.

    Enable the API

Register a domain name

If you already have a registered domain, skip this section.

You can use Cloud Domains to register a domain.Cloud Domains lets you use the same billing accountfor your domain that you have created in theBefore youbegin section.For instructions about how to register a domain using Cloud Domains,seeRegister a domain.

Important: If you use Cloud Domains to register a domain, youcannot clean up the domain at the end of this tutorial and are billed for thedomain. For details, see theCloud Domains pricing page.

Create a virtual machine instance

Console

To create a Linux virtual machine (VM) instance inCompute Engine, follow these instructions:

  1. In the Google Cloud console, go to theCreate an instance page.

    Go to Create an instance

  2. In theOS and Storage section, clickChange to begin configuring your boot disk.
  3. On thePublic images tab, chooseDebian GNU/Linux.
  4. ClickSelect.
  5. In theFirewall section, selectAllow HTTP traffic.
  6. To create the VM, clickCreate.
Note: By default, the VM instance that you create receives an ephemeralexternal IP address. The ephemeral IP address is released if you stop,reset, or delete the instance. To preserve the IP address,reserve a static external IP address.

Allow a short time for the instance to start. After the instance is ready, itis listed on theVM instances page with a green status icon.

Connect to your instance

  1. In the Google Cloud console, go to theVM instances page.

    Go to VM instances

  2. In the list of virtual machine instances, clickSSH in the row of the instance that you want to connect to.

    SSH button next to instance name.

You now have a terminal window for interacting with your Linux instance.

For details, see theQuickstart using a Linux VM.

Run a basic Apache web server

In this section, you run and test an Apache web server.

  1. From the SSH window, use the Debian package manager to install theapache2 package.

     sudo apt-get update && sudo apt-get install apache2 -y

    After installing Apache, the operating system automatically starts the Apache server.

  2. Overwrite the default web page for the Apache web server by using the following command:

     echo "<!doctype html><html><body><h1>Hello World!</h1></body></html>"
    | sudo tee /var/www/html/index.html

Test your server

Test that your instance is serving traffic on its external IP address.

Console

  1. In the Google Cloud console, go to theVM instances page.

    Go to VM instances

  2. In theExternal IP column, copy the external IP address for yourinstance.
  3. In a browser, navigate tohttp://[EXTERNAL_IP]. Don't usehttps toconnect because the server will return aConnection Refused error.

You should now see theHello World! page.

For further details, seeRunning a basic Apache webserver.

Set up your domain using Cloud DNS

If you are migrating from an existing provider, you can import your existing zones to Cloud DNS. For instructions, seeExport your DNS configuration from your existing provider. Otherwise, follow these steps to create a new public zone.

Console

  1. In the Google Cloud console, go to theCreate a DNS zone page.

    Go to Create a DNS zone

  2. For theZone type, selectPublic.

  3. For theZone name, entermy-new-zone.

  4. For theDNS name, enter a DNS name suffix for the zone by using a domainname that you registered (for example,example.com).

  5. ForDNSSEC, ensure that theOff setting is selected.

  6. ClickCreate to create a zone populated with the NS and SOA records.

  7. To point your registered domain name to the IP address of the hosting server,you must add an A record to your zone:

    1. On theZone details page, clickAdd Standard.
    2. SelectA from theResource Record Type menu.
    3. ForIPv4 Address, enter the external IP address for your instance.
    4. ClickCreate to create the A record for your zone.
  8. Optional: Add a CNAME record to account for a prefix to your domain name(for example,www.):

    1. ClickAdd Standard.
    2. In theDNS Name field, add the prefixwww for the domain.
    3. ForResource Record Type, chooseCNAME.
    4. ForCanonical name, enter the domain name, followed by a period (forexample,example.com.).
    5. ClickCreate.
Note: If you want to set up SMTP servers for delivering email to your domain,you must add MX records to your zone. For instructions about setting up MXrecords if you are using Google Workspace, see theSet up MX records support page.If you are using a different SMTP provider, use the MX record details from yourprovider and follow the same instructions.

Update name servers

To update name servers in Cloud Domains, follow these steps:

  1. In the Google Cloud console, go to theCloud Domains page.

    Go to Cloud Domains

  2. Click the domain name that you want to edit. You can also clickMorenext to the domain name to see the edit menu.

  3. To edit the DNS details, clickEdit DNS details.

  4. SelectUse Cloud DNS (Recommended).

  5. In theCloud DNS zone list, selectmy-new-zone.

  6. ClickSave.

Terraform

You can useTerraform resources to create a virtual machine (VM) instance, run anApache Web server, set up your domain using Cloud DNS, and updatename servers.

You can also useTerraform module to set up Cloud DNS with public, private,peering, or forwarding zones. For more information about zones, see the DNS zones overview.

# to setup a web-serverresource "random_id" "rnd" {  byte_length = 4}resource "google_compute_instance" "default" {  name         = "dns-compute-instance"  machine_type = "g1-small"  zone         = "us-central1-b"  boot_disk {    initialize_params {      image = "debian-cloud/debian-11"    }  }  network_interface {    network = "default"    access_config {      // Ephemeral public IP    }  }  metadata_startup_script = <<-EOF  sudo apt-get update && \  sudo apt-get install apache2 -y && \  echo "<!doctype html><html><body><h1>Hello World!</h1></body></html>" > /var/www/html/index.html  EOF}# to allow http trafficresource "google_compute_firewall" "default" {  name    = "allow-http-traffic"  network = "default"  allow {    ports    = ["80"]    protocol = "tcp"  }  source_ranges = ["0.0.0.0/0"]}# to create a DNS zoneresource "google_dns_managed_zone" "default" {  name          = "example-zone-googlecloudexample"  dns_name      = "example-${random_id.rnd.hex}.com."  description   = "Example DNS zone"  force_destroy = "true"}# to register web-server's ip address in DNSresource "google_dns_record_set" "default" {  name         = google_dns_managed_zone.default.dns_name  managed_zone = google_dns_managed_zone.default.name  type         = "A"  ttl          = 300  rrdatas = [    google_compute_instance.default.network_interface[0].access_config[0].nat_ip  ]}

Verify your setup

To verify that your configuration is working,after the name servers areupdated, navigate to your domain name (for example,example.com). The domainshould resolve to your IP address and should point to the Compute EngineVM displaying theHello World! page that you created inRun a basic Apache Web server.

To verify that your setup is correct, you can also run thedig +trace example.com command on your terminal window. Replaceexample.com with your registered domain name.

dig +trace example.com

The end of the output should include the following.IP_ADDRESS is your web server's IP address.

example.com.    300 IN  AIP_ADDRESS;; Received 62 bytes from 216.239.34.109#53(ns-cloud-d2.googledomains.com) in 62 ms
Note: If you're using Microsoft Windows anddig is not installed on yoursystem, you can run adig -t NS query from the Google Workspaceweb interface.

To verify that the changes are successful, run the following command:

example.com IN NS <your Cloud DNS name servers>

After waiting forDNS propagation tocomplete,you can also run thenslookup command to verify your setup:

nslookup example.com

The output should include the following.IP_ADDRESS is yourweb server's IP address.

Server:     127.0.0.1Address:    127.0.0.1#53Non-authoritative answer:Name:   example.comAddress:IP_ADDRESS

Clean up

Note: To avoid incurring charges for the resources that you created,you can delete them by following the instructions in this section. However, ifyou use Cloud Domains to register a domain, you mightcontinue to be billed for the domain. For details, see theCloud Domains pricing page.

Console

  1. In the Google Cloud console, go to theCloud DNS zones page.

    Go to Cloud DNS zones

  2. Click a zone name (for example,my-new-zone) to get to theZone details page.

  3. Select the A and CNAME records that you created.

  4. ClickDelete record sets.

  5. To delete the zone, clickDelete zone forthe zone namemy-new-zone.

  6. Go to theVM instances page.

    Go to VM instances

  7. Select the instance that you want to delete.

  8. On theMoremenu in the row of the instance, clickDelete.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.