Files
hapi/docs/adr/0001-linode-infrastructure-with-terraform.md
T
Bobby a5047cb72e feat: Linode Provider v2 migration and backend module
- Documented Linode Terraform Provider v2 migration in ADR-0002
- Created dedicated backend module for state storage
- Updated all modules for v2 patterns (cluster→region, pools→pool, tags)
- Added firewall outputs and backend outputs
- Updated example tfvars files with v2 configurations
- Updated AGENTS.md, README.md, and documentation
2026-11-17 23:29:19 -05:00

5.8 KiB

ADR-0001: Linode Infrastructure with Terraform

Context

We need to provision infrastructure on Linode for the HAPI project. This includes PostgreSQL database instances for staging and production, Object Storage buckets for uploads and Terraform state, and networking with firewall rules for API, PostgreSQL, and Adminer access.

Previously, infrastructure was provisioned manually via Linode dashboard CLI or planned for future setup. With HAPI MVP scope including infrastructure as a core component, we need a repeatable, version-controlled approach.

Options Considered

  1. Manual provisioning - Use Linode dashboard/CLI directly

    • Pros: Quick to get started, no tooling overhead
    • Cons: Not repeatable, hard to track changes, difficult to reproduce
  2. Terraform - Infrastructure as Code with Linode provider

    • Pros: Declarative, state management, reusable modules, multi-environment support
    • Cons: Learning curve, state file management
  3. Ansible - Configuration management focused on VMs

    • Pros: Powerful for configuration, good for stateful workloads
    • Cons: More complex for infrastructure provisioning, less Linode-native

Decision

We chose Terraform because:

  1. Declarative infrastructure - Clear, versionable infrastructure definitions
  2. Built-in state management - Tracks resource state with remote backend support
  3. Linode provider maturity - Well-maintained official provider with good coverage
  4. Modularity - Resources naturally organize into logical modules (postgres, storage, network)
  5. CI/CD ready - Terraform workflows integrate cleanly with Gitea Actions

Status

Accepted - Implementation complete.

Implementation

Directory Structure

infra/
├── backend/          # Terraform state backend configuration
├── postgres/         # PostgreSQL database instances
├── storage/          # Object Storage buckets
├── network/          # Firewall rules
├── main.tf           # Main orchestration file
├── providers.tf      # Provider configurations
├── variables.tf      # Common variables
├── outputs.tf        # Common outputs
└── README.md         # Infrastructure documentation

Module Details

Backend (infra/backend/)

  • Terraform state stored in Linode Object Storage
  • S3-compatible API for state access
  • Environment-specific state buckets
  • Provides backend configuration for main Terraform

PostgreSQL (infra/postgres/)

  • LKE (Linode Kubernetes Engine) clusters for staging and production
  • Configurable pool sizes (2 for staging, 3 for production)
  • Separate database names (hapi_staging, hapi_production)
  • Random password generation for database access

Object Storage (infra/storage/)

  • Upload buckets for staging and production
  • Terraform state buckets for each environment
  • S3-compatible API configuration
  • Bucket policies for access control

Networking (infra/network/)

  • Firewall with rules for:
    • API: port 8000 (from anywhere)
    • PostgreSQL: port 5432 (internal networks)
    • Adminer: port 8080 (from anywhere)
  • Inbound and outbound rules
  • Environment-specific firewall labels

Outputs

All modules export standardized outputs:

  • PostgreSQL: endpoint, port, database, username, password, pool_size
  • Object Storage: endpoint, access_key, secret_key, bucket names
  • Firewall: rules configuration, firewall ID and label
  • Terraform Backend: bucket, key, region, endpoint configuration

Configuration

Environment variables are used for credentials (not committed to Git):

  • LINODE_TOKEN: Linode API token
  • OBJECT_STORAGE_ACCESS_KEY: Object Storage access key
  • OBJECT_STORAGE_SECRET_KEY: Object Storage secret key

Makefile Commands

make init           # Initialize Terraform
make plan           # Generate plan
make apply          # Apply changes (ENV=staging|production)
make destroy        # Destroy infrastructure
make output         # Show outputs

Consequences

Positive

  • Reproducibility - Infrastructure can be recreated from code
  • Version control - Infrastructure changes tracked in Git
  • CI/CD ready - Terraform workflows integrate with Gitea Actions
  • Multi-environment support - Clean separation between staging and production
  • Documentation - Outputs and README provide clear understanding

Negative

  • Learning curve - Team needs to learn Terraform syntax and concepts
  • State management - Requires careful state file handling
  • Provider dependencies - Linode provider updates may be needed

Next Steps

  1. Complete Terraform implementation
  2. Set up CI/CD pipeline for infrastructure changes
  3. Document deployment process
  4. Create runbooks for common operations (scaling, backups, troubleshooting)
  5. Set up monitoring for infrastructure resources

Provider Versions

Linode Terraform Provider v2

ADR-0002 documents the migration from provider v1 to v2, which is implemented in the current branch. Key changes include:

  • Object Storage cluster → region attribute
  • Firewall rules updated to v2 format with action, ipv4, ipv6
  • LKE pools → pool
  • Tags converted to set format

See 0002-linode-provider-v2-migration.md for full details.

References