Yandex Cloud
Search
Contact UsGet started
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • AI Studio
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
© 2025 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
      • Transferring a WordPress website from a different hosting provider to Yandex Cloud
      • Setting up virtual hosting
        • Overview
        • Management console, CLI, and API
        • Terraform
      • Creating a Python web application with Flask
      • Hosting a static Gatsby website
      • Migrating to Cloud CDN from a third-party CDN provider
      • Getting website traffic statistics with S3 Select

In this article:

  • Get your cloud ready
  • Required paid resources
  • Delegate your domain to Cloud DNS
  • Create an infrastructure
  • Test the website
  • Delete the resources you created
  1. Application solutions
  2. Creating a website
  3. Assigning a domain name to a web server VM
  4. Terraform

Assigning a domain name to a VM with a web server using Terraform

Written by
Yandex Cloud
Updated at June 9, 2025
  • Get your cloud ready
    • Required paid resources
  • Delegate your domain to Cloud DNS
  • Create an infrastructure
  • Test the website
  • Delete the resources you created

To create an infrastructure for assigning a domain name to a VM with a web server using Terraform:

  1. Get your cloud ready.
  2. Delegate your domain to Cloud DNS.
  3. Create an infrastructure.
  4. Test the website.

If you no longer need the resources you created, delete them.

Get your cloud readyGet your cloud ready

Sign up in Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or register a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can navigate to the cloud page to create or select a folder for your infrastructure to operate in.

Learn more about clouds and folders.

Required paid resourcesRequired paid resources

The support cost includes:

  • Fee for using a public IP address (see Yandex Virtual Private Cloud pricing).
  • Fee for VM computing resources and disks (see Yandex Compute Cloud pricing).
  • Fee for using a public DNS zone and public DNS requests (see Yandex Cloud DNS pricing).

Delegate your domain to Cloud DNSDelegate your domain to Cloud DNS

To delegate a domain to Cloud DNS, in your account on your domain registrar's website, specify the DNS server addresses in the domain settings:

  • ns1.yandexcloud.net
  • ns2.yandexcloud.net

Delegation does not take effect immediately. Internet provider servers normally update records within 24 hours (86,400 seconds). This depends on the TTL value which specifies how long domain records are cached.

You can check domain delegation using Whois or the dig utility:

dig +short NS example.com

Result:

ns2.yandexcloud.net.
ns1.yandexcloud.net.

Create an infrastructureCreate an infrastructure

With Terraform, you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.

Terraform is distributed under the Business Source License. The Yandex Cloud provider for Terraform is distributed under the MPL-2.0 license.

For more information about the provider resources, see the documentation on the Terraform website or its mirror.

To create an infrastructure using Terraform:

  1. Install Terraform, get the authentication credentials, and specify the source for installing the Yandex Cloud provider (see Configure a provider, Step 1).

  2. Prepare the infrastructure description file:

    Ready-made configuration
    Manually
    1. Clone the repository with configuration files.

      git clone https://github.com/yandex-cloud-examples/yc-compute-dns-binding
      
    2. Navigate to the repository directory. Make sure it contains the following files:

      • bind-domain-to-vm.tf: Your infrastructure configuration.
      • bind-domain-to-vm.auto.tfvars: User data file.
    1. Create a folder for the infrastructure description file.
    2. In the folder, create:
      1. bind-domain-to-vm.tf configuration file:

        bind-domain-to-vm.tf
        # Declaring variables for custom parameters
        
        variable "folder_id" {
          type = string
        }
        
        variable "domain_name" {
          type = string
        }
        
        variable "ssh_key_path" {
          type = string
        }
        
        # Adding other variables
        
        locals {
          zone             = "ru-central1-a"
          network_name     = "webserver-network"
          subnet_name      = "webserver-subnet-ru-central1-a"
          sg_name          = "webserver-sg"
          vm_name          = "mywebserver"
          domain_zone_name = "my-domain-zone"
        }
        
        # Configuring a provider 
        
        terraform {
          required_providers {
            yandex = {
              source  = "yandex-cloud/yandex"
              version = ">= 0.47.0"
            }
          }
        }
        
        provider "yandex" {
          zone      = local.zone
          folder_id = var.folder_id
        }
        
        # Creating a cloud network
        
        resource "yandex_vpc_network" "webserver-network" {
          name = local.network_name
        }
        
        # Create subnet
        
        resource "yandex_vpc_subnet" "webserver-subnet-b" {
          name           = local.subnet_name
          zone           = local.zone
          network_id     = "${yandex_vpc_network.webserver-network.id}"
          v4_cidr_blocks = ["192.168.1.0/24"]
        }
        
        # Creating a security group
        
        resource "yandex_vpc_security_group" "webserver-sg" {
          name        = local.sg_name
          network_id  = "${yandex_vpc_network.webserver-network.id}"
          ingress {
            protocol       = "TCP"
            description    = "http"
            v4_cidr_blocks = ["0.0.0.0/0"]
            port           = 80
          }
        
          ingress {
            protocol       = "TCP"
            description    = "https"
            v4_cidr_blocks = ["0.0.0.0/0"]
            port           = 443
          }
        
          ingress {
            protocol       = "TCP"
            description    = "ssh"
            v4_cidr_blocks = ["0.0.0.0/0"]
            port           = 22
          }
        
          egress {
            protocol       = "ANY"
            description    = "any"
            v4_cidr_blocks = ["0.0.0.0/0"]
            from_port      = 0
            to_port        = 65535
          }
        }
        
        # Creating an image
        
        resource "yandex_compute_image" "osimage" {
          source_family = "lamp"
        }
        
        # Creating a disk
        
        resource "yandex_compute_disk" "boot-disk" {
          name     = "web-server-boot"
          type     = "network-hdd"
          image_id = yandex_compute_image.osimage.id
        }
        
        # Creating a VM instance
        
        resource "yandex_compute_instance" "mywebserver" {
          name        = local.vm_name
          platform_id = "standard-v2"
          zone        = local.zone
        
          resources {
            cores  = "2"
            memory = "2"
          }
        
          boot_disk {
            disk_id = yandex_compute_disk.boot-disk.id
          }
        
          network_interface {
            subnet_id          = "${yandex_vpc_subnet.webserver-subnet-b.id}"
            nat                = true
            security_group_ids = ["${yandex_vpc_security_group.webserver-sg.id}"]
          }
        
          metadata = {
            user-data = "#cloud-config\nusers:\n  - name: yc-user\n    groups: sudo\n    shell: /bin/bash\n    sudo: 'ALL=(ALL) NOPASSWD:ALL'\n    ssh_authorized_keys:\n      - ${file("${var.ssh_key_path}")}"
          }
        }
        
        # Creating a DNS zone
        
        resource "yandex_dns_zone" "my-domain-zone" {
          name    = local.domain_zone_name
          zone    = "${var.domain_name}."
          public  = true
        }
        
        # Creating a type A resource record
        
        resource "yandex_dns_recordset" "rsA1" {
          zone_id = yandex_dns_zone.my-domain-zone.id
          name    = "${yandex_dns_zone.my-domain-zone.zone}"
          type    = "A"
          ttl     = 600
          data    = ["${yandex_compute_instance.mywebserver.network_interface.0.nat_ip_address}"]
        }
        
      2. bind-domain-to-vm.auto.tfvars user data file:

        bind-domain-to-vm.auto.tfvars
        folder_id    = "<folder_ID>"
        ssh_key_path = "<path_to_SSH_key>"
        domain_name  = "<domain_name>"
        

    For more information about the properties of Terraform resources, see the provider documentation:

    • Network: yandex_vpc_network
    • Subnets: yandex_vpc_subnet
    • Security groups: yandex_vpc_security_group
    • Image: yandex_compute_image
    • Disk: yandex_compute_disk
    • VM: yandex_compute_instance
    • DNS zone: yandex_dns_zone
    • DNS resource record: yandex_dns_recordset
  3. In the bind-domain-to-vm.auto.tfvars file, set the following user-defined properties:

    • folder_id: Folder ID.
    • ssh_key_path: Path to the public SSH key file to authenticate the user on the VM, e.g., ~/.ssh/id_ed25519.pub. For more information, see Creating an SSH key pair.
    • domain_name: Your domain name, e.g., example.com.
  4. Create the resources:

    1. In the terminal, go to the directory where you edited the configuration file.

    2. Make sure the configuration file is correct using this command:

      terraform validate
      

      If the configuration is correct, you will get this message:

      Success! The configuration is valid.
      
    3. Run this command:

      terraform plan
      

      You will see a detailed list of resources. No changes will be made at this step. Terraform will show any errors found in your configuration.

    4. Apply the changes:

      terraform apply
      
    5. Type yes and press Enter to confirm the changes.

  5. Get the VM public IP address: you will use it later to test the hosting.

After creating the infrastructure, test the website.

Test the websiteTest the website

The website on your web server is now accessible by its domain name. To test the site, enter its IP address or domain name in your browser:

  • http://<VM_public_IP_address>
  • http://example.com

Delete the resources you createdDelete the resources you created

To shut down the hosting and stop paying for the resources you created:

  1. Open the bind-domain-to-vm.tf configuration file and delete your infrastructure description.

  2. Apply the changes:

    1. In the terminal, go to the directory where you edited the configuration file.

    2. Make sure the configuration file is correct using this command:

      terraform validate
      

      If the configuration is correct, you will get this message:

      Success! The configuration is valid.
      
    3. Run this command:

      terraform plan
      

      You will see a detailed list of resources. No changes will be made at this step. Terraform will show any errors found in your configuration.

    4. Apply the changes:

      terraform apply
      
    5. Type yes and press Enter to confirm the changes.

See alsoSee also

  • Assigning a domain name to a VM with a web server using the management console, CLI, or API

Was the article helpful?

Previous
Management console, CLI, and API
Next
Creating a Python web application with Flask
© 2025 Direct Cursus Technology L.L.C.