Skip to main content
Logo
Infrastructure Setup: Packer and Terraform
Overview

Infrastructure Setup: Packer and Terraform

December 17, 2024
2 min read

This guide covers the configuration of Packer for creating optimized VM templates and Terraform for deploying your homelab infrastructure on Proxmox.

Packer Configuration

credentials.pkr.hcl

This file holds sensitive credentials like passwords and API tokens. Do not upload this file to public repositories.

proxmox_api_url = "https://your-proxmox-ip-or-fqdn/api2/json"
proxmox_api_token_id = "terraform_user@pam!homelab"
proxmox_api_token_secret = "your-proxmox-api-token-secret"

To generate your Proxmox API token, follow this tutorial.
More about packer: here.

packer.pkr.hcl

This file contains the plugin configuration required for deployment on Proxmox.

packer {
required_plugins {
proxmox-iso = {
version = ">= 1.0.0"
source = "github.com/hashicorp/proxmox"
}
}
}

ubuntu-*folder*/ubuntu-server-jammy-docker.pkr.hcl

  1. Replace YOUR_PROXMOX_NODE_NAME with the name of your Proxmox node where you want the template to be created.
  2. Replace YOUR_IP_DEPLOYMENT_MACHINE with the IP of the machine where the deployment script will run.

ubuntu-*folder*/http/user-data

  1. Replace YOUR_SSH_KEY with the SSH key generated by the homelab_deploy.sh script.

Terraform Configuration

terraform.tfvars

This file holds sensitive credentials like passwords and API tokens. Do not upload this file to public repositories.

Example:

proxmox_api_url = "https://your-proxmox-ip-or-fqdn/api2/json"
proxmox_api_token_id = "terraform_user@pam!homelab"
proxmox_api_token_secret = "your-proxmox-api-token-secret"
proxmox_host = "proxmox-host-ip"
proxmox_user = "proxmox-user"
proxmox_password = "proxmox-password"

To generate your Proxmox API token, follow this tutorial.

modules/docker_vm/main.tf

Defines the deployment’s detailed configuration.

Replace the placeholders with your specific values:

  • YOUR_PROXMOX_NODE_NAME
  • IP_MACHINE (VM deployment IP)
  • GateWay_IP
  • IP_DEPLOYMENT (same-as-IP_MACHINE)
  • YOUR_SSH_KEY (generated by the homelab_deploy.sh script)

Key features enabled:

NFS/CIFS Mounting: Supports external storage mounts.

Warning

Note: If you don’t want to use any NFS mount, simply comment out this block:

resource "null_resource" "run_ansible_docker" {
depends_on = [local_file.ansible_inventory]
provisioner "local-exec" {
command = "LC_ALL=C.UTF-8 LANG=C.UTF-8 ansible-playbook -i ../ansible/inventory/inventory.ini ../ansible/roles/nfs_mount/main.yml"
}
}
  • Locales Configuration: Sets up default locales.
  • Root SSH Login: Allows root access via SSH.
  • SSH Key Authentication: Adds your SSH public key for secure access.
  • Ansible Integration: Automates further configurations.

Next Steps

With infrastructure configured, proceed to the Deployment guide to complete the setup and verify your homelab environment.