Cloud-Init with Terraform on Proxmox¶
This is a sample guide for using the cloud-init
command with Terraform to deploy new VMs.
Table of Contents¶
- 1. Install
cloud-init
(If Not Already Installed) - 2. Create a Cloud-Init Image Using the
cloud-localds
Command - 3. Convert a Base VM into a Cloud-Init Template
- 4. Convert the VM into a Proxmox Cloud-Init Template
- 5. Use Terraform to Deploy the Cloud-Init Image
- 6. Deploy the Terraform Configuration
- 7. Verify the Cloud-Init VM
- Conclusion
1. Install cloud-init
(If Not Already Installed)¶
Ensure cloud-init
is installed on your base VM before creating an image.
Verify installation:
2. Create a Cloud-Init Image Using the cloud-localds
Command¶
Use cloud-localds
to create a cloud-init disk with a user-defined configuration.
Create a meta-data
file¶
This file provides instance-specific information, such as instance ID.
Create a user-data
file¶
This file contains initial setup, such as users, passwords, SSH keys, and packages.
cat <<EOF > user-data
#cloud-config
users:
- name: kolkhis
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa AAAAB3...your-public-ssh-key...
package_update: true
packages:
- qemu-guest-agent
- htop
runcmd:
- echo "Cloud-init configured successfully!" > /etc/motd
EOF
Generate a Cloud-Init Image¶
Create a cloud-init
seed image:
This creates a cloud-init.img
file, which can be used to initialize VMs.
3. Convert a Base VM into a Cloud-Init Template¶
Now, take a clean base VM, install necessary dependencies, and configure it for cloud-init.
Install Dependencies on the Base VM¶
sudo dnf install cloud-init cloud-utils-growpart qemu-guest-agent
sudo systemctl enable --now qemu-guest-agent
Clean Up the Base VM for Cloning¶
Before turning this VM into a template, clean up persistent machine-specific settings:
Shut down the VM:
4. Convert the VM into a Proxmox Cloud-Init Template¶
On the Proxmox node, create a template for future Terraform use.
This can either be done with the qm
command line tool, or with the Proxmox
Web UI.
Convert the VM to a Template¶
-
Locate the VM ID:
-
Convert it to a Template:
-
Create a
cloud-init
disk: -
Configure boot options for
cloud-init
: -
Optionally, resize the disk if you need to:
5. Use Terraform to Deploy the Cloud-Init Image¶
After setting up the cloud-init
template, you can use Terraform to deploy VM instances.
Terraform Configuration¶
provider "proxmox" {
pm_api_url = "https://192.168.1.10:8006/api2/json"
pm_user = "root@pam"
pm_password = "password"
pm_tls_insecure = true
}
resource "proxmox_vm_qemu" "cloud_vm" {
name = "cloud-vm-1"
target_node = "proxmox-node"
clone = "cloud-init-template"
disks {
scsi {
disk {
size = "10G"
storage = "local-lvm"
}
}
}
network {
model = "virtio"
bridge = "vmbr0"
}
os_type = "cloud-init"
ciuser = "kolkhis"
cipassword = "your-password"
sshkeys = <<EOT
ssh-ed25519 AAAAC3...your-public-ssh-key...
EOT
}
Fill in the information in the appropriate fields.
-
provider "proxmox"
: This should be your PVE node's info.-
pm_api_url
: Should be the IP to your proxmox node, pointing towards the Proxmox API endpoint. -
pm_user
: Default login for the root user on PVE isroot@pam
. pm_password
: The password for your root login.resource "proxmox_vm_qemu" "cloud_vm"
: The new resource to be created.name
: The name of your new VM.target_node
: The Proxmox node that it should be created on.clone
: Thecloud-init
template that you are using.disks
: Fill in the disk information here (size and name of storage pool).-
ciuser
/cipassword
: The username to create on the new VM and its password. -
sshkeys
: The public SSH keys to upload to theauthorized_keys
file.
-
6. Deploy the Terraform Configuration¶
Run the following Terraform commands to deploy your VM.
7. Verify the Cloud-Init VM¶
Once Terraform has created the VM, verify that cloud-init applied the settings.
Check logs:tl;dr¶
cloud-init
automates VM initialization for Terraform in Proxmox.- Using
cloud-localds
, you preconfigure VM settings (users, SSH keys, packages). - Converting a base VM to a Proxmox
cloud-init
template allows dynamic deployment via Terraform.