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_eachkeys for predictable resource management
| Name | Version |
|---|---|
| terraform | >= 1.3.0 |
| yandex | >= 0.100 |
provider "yandex" {
# authentication configuration
}| 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 |
YC_MODULE_VM/
├── main.tf
├── variables.tf
├── output.tf
├── versions.tf
├── examples/
│ ├── example-1-basic/
│ └── example-2-cluster-qa-dev-prod/
module "vm_group" {
source = "./YC_MODULE_VM"
vm_configure = {
app = {
name = "app"
node_count = 2
resources = {
cores = 2
memory = 4
}
}
}
}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
}
}
}
}
}| 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 |
map(object({
name = string
node_count = number
resources = object({
cores = number
memory = number
})
extra_disks = optional(map(object({
name = string
type = string
size = number
})), {})
}))| Name | Description |
|---|---|
| external_IP | Map of VM name → external IP |
| internal_IP | Map of VM name → internal IP |
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.
Working examples are available in:
examples/example-1-basicexamples/example-2-cluster-qa-dev-prod
To test:
cd examples/example-1-basic
terraform init
terraform plan- All VMs are created within a single subnet
- NAT is enabled by default
- Additional disks are created with
auto_delete = false for_eachis used instead ofcountfor stable addressing- Image is selected via
data.yandex_compute_imageusingfamily
MIT