Terraform
April 17, 2023

YC MongoDB 5.0

В данной статье рассматриваем установку кластера Managed MongoDB 5.0 в Yandex Cloud.

Создадим фолдер mongodb и в нём три файла.

variables.tf

variable "cloudID" { //cloud-id
  default = "b1gig0ogqtnk75jde2q8"
}

variable "folderID" { //folder-id
  default = "b1g62iqs14tk7mu0faun"
}

variable "tokenName" { //token that should be created by 'yc iam create-token'
  type    = string
  default = "t1.9euelZqczJaZzoucns_JzsiMlZWMte3rnpWazcqLj4yOkZSQnoHcnJuez5fl8PdyGgxi-e8tSkCM5fT3Mm0KXvnvLUPAjA.it-O02jXSamhoOriXKtwtl2npBUPAmyaxal9MDbJi0EANfj-gGWolV6DmG1df0BxuJ0Pr3PigzxDfVZWOWkaBQ"
}

variable "zoneName" { //default zone
    default = "ru-central1-a"
}

variable "networkID" { //default network
    default = "enpblgntu48fhohf30ml"
}

variable "subnetID" { //default subnet
    default = "e9b16nenl5a52h6o9djb"
}

variable "versionNum" { //cluster version
    default = "5.0"
}

provider.tf

terraform {
  required_providers {
    yandex = {
      source = "yandex-cloud/yandex"
      version = ">=0.85"
    }
  }
}

provider "yandex" {
  token     = var.tokenName
  cloud_id  = var.cloudID
  folder_id = var.folderID
  zone      = var.zoneName
}

main.tf

resource "yandex_mdb_mongodb_cluster" "cam-mon" {
  name        = "cam-mon"
  description = "Cameda prod cluster from tf"
  environment = "PRODUCTION"
  network_id  = var.networkID
  security_group_ids  = ["enpn9lfurm1427rb17l3"]
  deletion_protection = false

  cluster_config {
    version = var.versionNum
    feature_compatibility_version = 4.4
    backup_window_start {
      hours = 01
    }
  }

  database {
    name = "db1"
  }
  
  labels = {
    cluster = "main-prod"
  }

  user {
    name     = "cameda"
    password = "Pa$w0rd"
    permission {
      database_name = "db1"
      roles         = ["mdbDbAdmin"]
    }
  }

  resources_mongod {
    resource_preset_id = "s2.micro"
    disk_type_id       = "network-ssd"
    disk_size          = 20
  }

  host {
    zone_id = var.zoneName
    subnet_id = var.subnetID
    role = "PRIMARY"
    assign_public_ip = "true"
    type = "mongod"
  }

  maintenance_window {
    type = "WEEKLY"
    day  = "MON"
    hour = 17
  }
}

Для установки последовательно запускаем.

terraform validate
terraform plan
terraform apply

Удаление.

terraform destroy