Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

100 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-yc-compute-instances-node

Terraform module for creating scalable groups of virtual machines in Yandex Cloud based on reusable VM templates.

This module allows you to:

  • Define VM templates
  • Scale each template using node_count
  • Automatically create and attach additional disks
  • Retrieve internal and external IP addresses
  • Use stable for_each keys for predictable resource management

Requirements

Name Version
terraform >= 1.3.0
yandex >= 0.100

Providers

provider "yandex" {
  # authentication configuration
}

Resources

Name Type
yandex_compute_instance.vm resource
yandex_compute_disk.extra_disks resource
yandex_compute_image.my_image data source
yandex_vpc_subnet.default_subnet data source

Module Structure

YC_MODULE_VM/
├── main.tf
├── variables.tf
├── output.tf
├── versions.tf
├── examples/
│   ├── example-1-basic/
│   └── example-2-cluster-qa-dev-prod/

Usage

Basic Example

module "vm_group" {
  source = "./YC_MODULE_VM"

  vm_configure = {
    app = {
      name       = "app"
      node_count = 2

      resources = {
        cores  = 2
        memory = 4
      }
    }
  }
}

Example: Cluster with Additional Disks

module "cluster" {
  source = "./YC_MODULE_VM"

  vm_configure = {
    db = {
      name       = "db"
      node_count = 3

      resources = {
        cores  = 4
        memory = 16
      }

      extra_disks = {
        data = {
          name = "data"
          type = "network-ssd"
          size = 200
        }
      }
    }
  }
}

Inputs

Name Description Type Default Required
vm_configure VM templates configuration map(object) n/a yes
family Image family string "ubuntu-2004-lts" no
subnet Subnet name string "default-ru-central1-a" no
ssh_key Path to SSH public key string "~/.ssh/id_ed25519.pub" no

vm_configure Structure

map(object({
  name       = string
  node_count = number

  resources = object({
    cores  = number
    memory = number
  })

  extra_disks = optional(map(object({
    name = string
    type = string
    size = number
  })), {})
}))

Outputs

Name Description
external_IP Map of VM name → external IP
internal_IP Map of VM name → internal IP

How Scaling Works

Each object in vm_configure:

  • Is treated as a VM template
  • Is replicated according to node_count
  • Generates instance names in the format:
<template_name>-<index>

Additional disks receive deterministic keys:

<template_key>-<index>-<disk_key>

This guarantees stable for_each behavior and prevents unnecessary resource recreation.


Examples

Working examples are available in:

  • examples/example-1-basic
  • examples/example-2-cluster-qa-dev-prod

To test:

cd examples/example-1-basic
terraform init
terraform plan

Notes

  • All VMs are created within a single subnet
  • NAT is enabled by default
  • Additional disks are created with auto_delete = false
  • for_each is used instead of count for stable addressing
  • Image is selected via data.yandex_compute_image using family

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages