Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
    • Cloud Interconnect
    • Cloud Backup
    • Cloud Registry
    • Yandex AI Studio
    • Compute Cloud
    • Object Storage
    • Managed Service for Kubernetes®
    • Yandex BareMetal
    • Smart Web Security
    • Security Deck
    • Managed Service for PostgreSQL
    • Managed Service for ClickHouse®
    • Monium
    • Cloud CDN
    • Network Load Balancer
    • Virtual Private Cloud
    • Cloud DNS
    • Application Load Balancer
    • Yandex Cloud Video
    • Stackland
    • Yandex Cloud Router
    • Yandex Managed Service for Trino
    • Managed Service for MySQL®
    • Managed Service for Valkey™
    • Managed Service for Apache Spark™
    • Yandex StoreDoc
    • Managed Service for OpenSearch
    • Managed Service for Apache Kafka®
    • Data Transfer
    • Yandex MPP Analytics Engine for PostgreSQL
    • Yandex Managed Service for Apache Airflow®
    • Data Processing
    • Yandex MetaData Hub
    • Managed Service for YDB
    • Managed Service for Sharded PostgreSQL
    • Managed Service for YTsaurus
    • Yandex WebSQL
    • DataLens
    • Yandex Search API
    • SpeechSense
    • SpeechKit
    • DataSphere
    • Vision OCR
    • Translate
    • Yandex Identity Hub
    • Key Management Service
    • Certificate Manager
    • Yandex Lockbox
    • Audit Trails
    • SmartCaptcha
    • Cloud Desktop
    • SourceCraft Code Assistant
    • Container Registry
    • Managed Service for GitLab
    • Managed Service for Prometheus®
    • Cloud Functions
    • API Gateway
    • Yandex Cloud Postbox
    • Message Queue
    • Serverless Integrations
    • IoT Core
    • Data Streams
    • Serverless Containers
    • Cloud Notification Service
    • Yandex Query
    • Identity and Access Management
    • Yandex Cloud Console
    • Resource Manager
    • Yandex Cloud Billing
    • Yandex Cloud Quota Manager
    • Cloud Apps
  • System Status
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • 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
    • Center for Technologies and Society
    • Yandex Cloud Partner program
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
    • Architecture and protection of a basic web service
    • Cost analysis by resource using Object Storage
    • Obtaining the information you need to request the Russian Ministry of Digital Development to whitelist a resource
      • Configuring time synchronization using NTP
      • DHCP settings for interfacing with a corporate DNS server
        • Overview
        • Management console
        • Terraform
      • Installing the Cisco CSR 1000v virtual router
      • Installing a Mikrotik CHR virtual router

In this article:

  • Getting started
  • Required paid resources
  • Create your infrastructure
  • Test the NAT gateway instance
  • How to delete the resources you created
  1. Basic infrastructure
  2. Network
  3. Routing through a NAT instance
  4. Terraform

NAT instance routing with Terraform

Written by
Yandex Cloud
Updated at July 13, 2026
View in Markdown
  • Getting started
    • Required paid resources
  • Create your infrastructure
  • Test the NAT gateway instance
  • How to delete the resources you created

To set up NAT instance routing with Terraform:

  1. Get your cloud ready.
  2. Create an infrastructure.
  3. Test the NAT instance.

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

Getting startedGetting started

Sign up for Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or create 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 create or select a folder for your infrastructure on the cloud page.

Learn more about clouds and folders here.

Required paid resourcesRequired paid resources

The cost of NAT instance support includes:

  • Fee for continuously running VMs (see Yandex Compute Cloud pricing).
  • Fee a dynamic or static external IP address (see Yandex Virtual Private Cloud pricing).

Create your infrastructureCreate your 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 guides on the Terraform website or its mirror.

To create an infrastructure with Terraform:

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

  2. Set up your infrastructure description file:

    Ready-made configuration
    Manually
    1. Clone the configuration file repository:

      git clone https://github.com/yandex-cloud-examples/yc-compute-nat-instance.git
      
    2. Navigate to the repository directory. It should now contain the following files:

      • nat-instance.tf: New infrastructure configuration.
      • nat-instance.auto.tfvars: User data.
    1. Create a folder for the infrastructure description file.

    2. In this folder, create the nat-instance.tf configuration file:

      nat-instance.tf
      # Declaring variables for custom parameters
      
      variable "folder_id" {
        type = string
      }
      
      variable "vm_user" {
        type = string
      }
      
      variable "vm_user_nat" {
        type = string
      }
      
      variable "ssh_key_path" {
        type = string
      }
      
      # Adding other variables
      
      locals {
        network_name     = "my-vpc"
        subnet_name1     = "public-subnet"
        subnet_name2     = "private-subnet"
        sg_nat_name      = "nat-instance-sg"
        vm_test_name     = "test-vm"
        vm_nat_name      = "nat-instance"
        route_table_name = "nat-instance-route"
      }
      
      # Configuring the provider
      
      terraform {
        required_providers {
          yandex = {
            source  = "yandex-cloud/yandex"
            version = ">= 0.47.0"
          }
        }
      }
      
      provider "yandex" {
        folder_id = var.folder_id
      }
      
      # Creating a cloud network
      
      resource "yandex_vpc_network" "my-vpc" {
        name = local.network_name
      }
      
      # Creating subnets
      
      resource "yandex_vpc_subnet" "public-subnet" {
        name           = local.subnet_name1
        zone           = "ru-central1-a"
        network_id     = yandex_vpc_network.my-vpc.id
        v4_cidr_blocks = ["192.168.1.0/24"]
      }
      
      resource "yandex_vpc_subnet" "private-subnet" {
        name           = local.subnet_name2
        zone           = "ru-central1-a"
        network_id     = yandex_vpc_network.my-vpc.id
        v4_cidr_blocks = ["192.168.2.0/24"]
        route_table_id = yandex_vpc_route_table.nat-instance-route.id
      }
      
      # Creating a security group
      
      resource "yandex_vpc_security_group" "nat-instance-sg" {
        name       = local.sg_nat_name
        network_id = yandex_vpc_network.my-vpc.id
      
        egress {
          protocol       = "ANY"
          description    = "any"
          v4_cidr_blocks = ["0.0.0.0/0"]
          from_port      = 0
          to_port        = 65535
        }
      
        ingress {
          protocol       = "TCP"
          description    = "ssh"
          v4_cidr_blocks = ["0.0.0.0/0"]
          port           = 22
        }
      
        ingress {
          protocol       = "TCP"
          description    = "ext-http"
          v4_cidr_blocks = ["0.0.0.0/0"]
          port           = 80
        }
      
        ingress {
          protocol       = "TCP"
          description    = "ext-https"
          v4_cidr_blocks = ["0.0.0.0/0"]
          port           = 443
        }
      }
      
      # Adding a prebuilt VM image
      
      resource "yandex_compute_image" "ubuntu-1804-lts" {
        source_family = "ubuntu-1804-lts"
      }
      
      resource "yandex_compute_image" "nat-instance-ubuntu" {
        source_family = "nat-instance-ubuntu"
      }
      
      # Creating boot disks
      
      resource "yandex_compute_disk" "boot-disk-ubuntu" {
        name     = "boot-disk-ubuntu"
        type     = "network-hdd"
        zone     = "ru-central1-a"
        size     = "20"
        image_id = yandex_compute_image.ubuntu-1804-lts.id
      }
      
      resource "yandex_compute_disk" "boot-disk-nat" {
        name     = "boot-disk-nat"
        type     = "network-hdd"
        zone     = "ru-central1-a"
        size     = "20"
        image_id = yandex_compute_image.nat-instance-ubuntu.id
      }
      
      # Creating a VM
      
      resource "yandex_compute_instance" "test-vm" {
        name        = local.vm_test_name
        platform_id = "standard-v3"
        zone        = "ru-central1-a"
      
        resources {
          core_fraction = 20
          cores         = 2
          memory        = 2
        }
      
        boot_disk {
          disk_id = yandex_compute_disk.boot-disk-ubuntu.id
        }
      
        network_interface {
          subnet_id          = yandex_vpc_subnet.private-subnet.id
          security_group_ids = [yandex_vpc_security_group.nat-instance-sg.id]
        }
      
        metadata = {
          user-data = "#cloud-config\nusers:\n  - name: ${var.vm_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 an NAT instance
      
      resource "yandex_compute_instance" "nat-instance" {
        name        = local.vm_nat_name
        platform_id = "standard-v3"
        zone        = "ru-central1-a"
      
        resources {
          core_fraction = 20
          cores         = 2
          memory        = 2
        }
      
        boot_disk {
          disk_id = yandex_compute_disk.boot-disk-nat.id
        }
      
        network_interface {
          subnet_id          = yandex_vpc_subnet.public-subnet.id
          security_group_ids = [yandex_vpc_security_group.nat-instance-sg.id]
          nat                = true
        }
      
        metadata = {
          user-data = "#cloud-config\nusers:\n  - name: ${var.vm_user_nat}\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 route table and static route
      
      resource "yandex_vpc_route_table" "nat-instance-route" {
        name       = "nat-instance-route"
        network_id = yandex_vpc_network.my-vpc.id
        static_route {
          destination_prefix = "0.0.0.0/0"
          next_hop_address   = yandex_compute_instance.nat-instance.network_interface.0.ip_address
        }
      }
      
    3. Create the nat-instance.auto.tfvars user data file:

      nat-instance.auto.tfvars
      folder_id    = "<folder_ID>"
      vm_user      = "<VM_username>"
      vm_user_nat  = "<NAT_instance_username>"
      ssh_key_path = "<path_to_public_SSH_key>"
      

    For more on the properties of resources used in Terraform, see these provider guides:

    • Network: yandex_vpc_network.
    • Subnets: yandex_vpc_subnet.
    • Security groups: yandex_vpc_security_group.
    • Image: yandex_compute_image.
    • Disk: yandex_compute_disk.
    • VM instance: yandex_compute_instance.
    • Route table: yandex_vpc_route_table.
  3. In the nat-instance.auto.tfvars file, set the following user-defined properties:

    • folder_id: Folder ID.
    • vm_user: VM user name.
    • vm_user_nat: NAT instance username.
    • ssh_key_path: Path to the public SSH key to authenticate the user on the VM. For more information, see Creating an SSH key pair.
  4. Create the resources:

    1. In the terminal, navigate to the configuration file directory.

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

      terraform validate
      

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

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

      terraform plan
      

      You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.

    4. Apply the configuration changes:

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

After you create the infrastructure, test the NAT instance.

Test the NAT gateway instanceTest the NAT gateway instance

  1. Connect to the VM via a private IP address, using the NAT instance as a jump host:

    ssh -J <NAT_instance_username>@<NAT_instance_public_IP_address> \
      <VM_user_name>@<VM_internal_IP_address>
    

    You can also connect to the test VM using the standard input/output redirection (-W flag) to forward the connection through a NAT instance:

    ssh -o ProxyCommand="ssh -i <NAT_key_file_path/name> -W %h:%p <NAT_username>@<NAT_public_IP_address>" \
      -i <VM_key_file_path/name> <VM_user_name>@<VM_internal_IP_address>
    

    Use this command for connection in the following cases:

    • Your VM is running an OpenSSH version below 7.3.
    • Your SSH keys are stored outside the default directory or have non-standard names.
  2. Type yes to connect to the NAT instance and re-enter yes to connect to the test VM.

    Note

    When you type yes, the command may not be displayed in the terminal, but it will run anyway.

  3. Make sure the test VM is connected to the internet via the public IP address of the NAT instance. Run this command:

    curl ifconfig.co
    

    If it returns the public IP address of the NAT instance, the configuration is correct.

How to delete the resources you createdHow to delete the resources you created

To stop paying for the resources you created:

  1. Open the nat-instance.tf file and delete your infrastructure description from it.

  2. Apply the changes:

    1. In the terminal, navigate to the configuration file directory.

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

      terraform validate
      

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

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

      terraform plan
      

      You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.

    4. Apply the configuration changes:

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

Useful linksUseful links

  • Configuring NAT routing from the management console

Was the article helpful?

Previous
Management console
Next
Installing the Cisco CSR 1000v virtual router
© 2026 Direct Cursus Technology L.L.C.