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
- 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.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
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.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.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
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.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.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine 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:
- In the Google Cloud console, go to theCreate an instance page.
- In theOS and Storage section, clickChange to begin configuring your boot disk.
- On thePublic images tab, chooseDebian GNU/Linux.
- ClickSelect.
- In theFirewall section, selectAllow HTTP traffic.
- To create the VM, clickCreate.
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
- In the Google Cloud console, go to theVM instances page.
- In the list of virtual machine instances, clickSSH in the row of the instance that you want to connect to.

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.
From the SSH window, use the Debian package manager to install the
apache2package.sudo apt-get update && sudo apt-get install apache2 -y
After installing Apache, the operating system automatically starts the Apache server.
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
- In the Google Cloud console, go to theVM instances page.
- In theExternal IP column, copy the external IP address for yourinstance.
- In a browser, navigate to
http://[EXTERNAL_IP]. Don't usehttpstoconnect because the server will return aConnection Refusederror.
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
In the Google Cloud console, go to theCreate a DNS zone page.
For theZone type, selectPublic.
For theZone name, enter
my-new-zone.For theDNS name, enter a DNS name suffix for the zone by using a domainname that you registered (for example,
example.com).ForDNSSEC, ensure that the
Offsetting is selected.ClickCreate to create a zone populated with the NS and SOA records.
To point your registered domain name to the IP address of the hosting server,you must add an A record to your zone:
- On theZone details page, clickAdd Standard.
- SelectA from theResource Record Type menu.
- ForIPv4 Address, enter the external IP address for your instance.
- ClickCreate to create the A record for your zone.
Optional: Add a CNAME record to account for a prefix to your domain name(for example,
www.):- ClickAdd Standard.
- In theDNS Name field, add the prefix
wwwfor the domain. - ForResource Record Type, chooseCNAME.
- ForCanonical name, enter the domain name, followed by a period (forexample,
example.com.). - ClickCreate.
Update name servers
To update name servers in Cloud Domains, follow these steps:
In the Google Cloud console, go to theCloud Domains page.
Click the domain name that you want to edit. You can also clickMorenext to the domain name to see the edit menu.
To edit the DNS details, clickEdit DNS details.
SelectUse Cloud DNS (Recommended).
In theCloud DNS zone list, select
my-new-zone.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
dig 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
In the Google Cloud console, go to theCloud DNS zones page.
Click a zone name (for example,
my-new-zone) to get to theZone details page.Select the A and CNAME records that you created.
ClickDelete record sets.
To delete the zone, clickdeleteDelete zone forthe zone name
my-new-zone.Go to theVM instances page.
Select the instance that you want to delete.
On themore_vertMoremenu in the row of the instance, clickDelete.
What's next
- To add, delete, or update records, seeManage records.
- To work with managed zones, seeCreate, modify, and delete zones.
- To find solutions for common issues that you might encounter when usingCloud DNS, seeTroubleshooting.
- To reference the API, seeCloud DNS REST API.
- To determine costs, seeCloud DNS Pricing.
- To get an overview of Cloud DNS, seeCloud DNS overview.
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.