# 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 ```bash 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 ## References - [Issue 1: Linode Infrastructure with Terraform](../../issues/1) - [Linode Terraform Provider](https://registry.terraform.io/providers/linode/linode/latest/docs) - [Terraform Best Practices](https://developer.hashicorp.com/terraform/tutorials/best-practices) - [HAPI MVP Spec](../../MVP-SPEC.md)