evolved-npcs-infra/main.tf
2025-09-27 23:22:22 -07:00

139 lines
No EOL
3.6 KiB
HCL

terraform {
required_version = ">= 1.0"
required_providers {
talos = {
source = "siderolabs/talos"
version = "~> 0.4"
}
}
}
# Load variables from a .tfvars file
variable "cluster_name" {
description = "The name of the Talos cluster"
type = string
default = "evolved-npcs-cluster"
}
variable "memory_mb" {
description = "Memory for each VM in MB"
type = number
default = 2048
}
variable "vcpu_count" {
description = "Number of vCPUs for each VM"
type = number
default = 2
}
variable "talos_version" {
description = "The version of Talos to use"
type = string
default = "v1.11.1"
}
# Configure providers
provider "talos" {}
# Generate machine secrets
resource "talos_machine_secrets" "this" {}
# Create control plane configuration
data "talos_machine_configuration" "this" {
cluster_name = var.cluster_name
cluster_endpoint = "https://10.5.0.2:6443"
machine_type = "controlplane"
machine_secrets = talos_machine_secrets.this.machine_secrets
talos_version = var.talos_version
}
data "talos_client_configuration" "this" {
cluster_name = var.cluster_name
client_configuration = talos_machine_secrets.this.client_configuration
nodes = ["10.5.0.2"]
}
resource "null_resource" "talos_cluster" {
triggers = {
cluster_name = var.cluster_name
memory_mb = var.memory_mb
vcpu_count = var.vcpu_count
}
# Download Talos kernel and initramfs
provisioner "local-exec" {
command = <<EOT
mkdir -p _out/
curl -L -o _out/vmlinuz-arm64 https://github.com/siderolabs/talos/releases/download/${var.talos_version}/vmlinuz-arm64
curl -L -o _out/initramfs-arm64.xz https://github.com/siderolabs/talos/releases/download/${var.talos_version}/initramfs-arm64.xz
EOT
}
provisioner "local-exec" {
command = <<EOT
sudo --preserve-env=HOME talosctl cluster create \
--provisioner=qemu \
--name ${var.cluster_name} \
--controlplanes 1 \
--workers 0 \
--memory ${var.memory_mb} \
--cpus ${var.vcpu_count}
EOT
}
provisioner "local-exec" {
when = destroy
command = <<EOT
sudo --preserve-env=HOME talosctl cluster destroy \
--name ${self.triggers.cluster_name}
EOT
}
}
# resource "talos_machine_configuration_apply" "this" {
# client_configuration = talos_machine_secrets.this.client_configuration
# machine_configuration_input = data.talos_machine_configuration.this.machine_configuration
# node = "10.5.0.2"
# }
# resource "talos_machine_bootstrap" "this" {
# depends_on = [
# talos_machine_configuration_apply.this
# ]
# node = "10.5.0.2"
# client_configuration = talos_machine_secrets.this.client_configuration
# }
# resource "talos_cluster_kubeconfig" "this" {
# depends_on = [
# talos_machine_bootstrap.this
# ]
# client_configuration = talos_machine_secrets.this.client_configuration
# node = "10.5.0.2"
# }
# Output important information
output "controlplane_ip" {
value = "10.5.0.2"
}
output "talos_client_config" {
value = data.talos_client_configuration.this.talos_config
sensitive = true
}
output "machine_config" {
value = data.talos_machine_configuration.this.machine_configuration
sensitive = true
}
output "next_steps" {
value = <<EOT
After applying:
1. The control plane will be available at 10.5.0.2
2. The configuration is embedded in the kernel parameters
3. Terraform will automatically apply the config and bootstrap the cluster
4. Check status with: talosctl --talosconfig talosconfig version --nodes 10.5.0.2
EOT
}