DEV Community

Cover image for Using Terraform to create LXC in Proxmox
Sibelius Seraphini for Woovi

Posted on

Using Terraform to create LXC in Proxmox

Terraform is a well-known solution for defining, provisioning, and managing Infrastructure as Code (IaC).
Most of the content out there focuses on major clouds as AWS, GCP, and Azure.
I'd like to focus on Proxmox, an open source hypervisor used in a lot of home labs and also in production for companies that follow the baremetal approach, like Woovi.

Proxmox Infrastructure

Proxmox is a virtualization platform. You can create VMs and LXC (Linux Containers) using the UI, but when your team grows, and you want to be able to reproduce the same staging environment at production, you need to use IaC.
IaC is also great to make sure something is reproducible.

VMs vs LXC

You can think of LXC as simpler and lighter VMs.
LXC provides less isolation, and some software can't run on top of it easily, like microk8s nodes.
We are going to focus on LXC to make this article simpler.

Provisioning using Terraform

You can have the whole definition in the same file or split it into many files in the same folder.

First, define the provider Proxmox from Telmate.

Also, define the Proxmox provider variable like api_url, token ID, and token secret.
You can follow this documentation terraform for proxmox to generate the token ID and token secret.

terraform {
  required_providers {
    proxmox = {
      source  = "telmate/proxmox"
      version = "3.0.2-rc04"
    }
  }
}

provider "proxmox" {
  pm_api_url = "https://myproxmoxnode:8006/api2/json"
  pm_tls_insecure = true
  pm_log_enable = true
  pm_debug      = true
  pm_api_token_id = var.PM_API_TOKEN_ID
  pm_api_token_secret = var.PM_API_TOKEN_SECRET
  pm_log_levels = {
    _default    = "debug"
    _capturelog = ""
  }
}

variable "PM_API_TOKEN_ID" {}
variable "PM_API_TOKEN_SECRET" {}
Enter fullscreen mode Exit fullscreen mode

define the LXC resource

resource "proxmox_lxc" "mongo-1" {
  target_node = "dev1"
  hostname    = "mongo1"
  ostemplate  = "local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst"
  password    = var.LXC_PASSWORD

  unprivileged = true

  cores   = 1
  memory  = 512
  swap    = 512

  rootfs {
    storage = "local-lvm"
    size    = "50G"
  }

  network {
    name   = "eth0"
    bridge = "vmbr0"
    ip     = "10.99.90.2/16"
    gw     = "10.99.0.3"
  }

  ssh_public_keys = file("~/.ssh/id_ansible.pub")

  start = true
}
Enter fullscreen mode Exit fullscreen mode

Run ini to initialize Terraform

terraform init
Enter fullscreen mode Exit fullscreen mode

Run apply to provision the lxc resource

terraform apply
Enter fullscreen mode Exit fullscreen mode

Run destroy to destroy everything

terraform destroy
Enter fullscreen mode Exit fullscreen mode

In this setup, you need to save the local Terraform files to avoid recreating the resources. For production, the best approach is to keep Terraform state in an S3 bucket

In Conclusion

You don't need to start your MVP Startup using Terraform to provision everything in your Proxmox.
My recommendation is to keep improving automation as you and your team grow.
After you get the first Terraform working, the rest gets easier.

How are you using Terraform in your company?


Woovi
Woovi is a fintech platform revolutionizing how businesses and developers handle payments in Brazil. Built with a developer-first mindset, Woovi simplifies integration with instant payment methods like Pix, enabling companies to receive payments seamlessly and automate financial workflows.

If you want to work with us, we are hiring!


Photo by Amélie Mourichon on Unsplash

Top comments (0)