4 Commits
Author SHA1 Message Date
bobby 1b17000d16 Merge pull request 'feat: Linode Provider v2 migration and infrastructure cleanup' (#35) from feat/01a-linode-terraform-cleanup into main 2026-11-17 23:44:07 -05:00
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
Bobby 27abc1891b feat: address code review feedback for infrastructure
- Add terraform backend configuration (s3-compatible with Linode Object Storage)
- Update PostgreSQL pool sizes from 10/25 to 2/3 in ADR-0001
- Add explicit linode provider configuration in providers.tf
- Document DROP policies for security model

This addresses both required and optional feedback from the code review.
2026-11-17 22:45:04 -05:00
Bobby 494f38c1f3 01a: update Linode Terraform for provider v2
- Update all modules to use Linode provider v2 (linode/linode ~> 2.0)
- Replace deprecated cluster attribute with region in all modules
- Update firewall to use ipv4/ipv6 instead of cidrs (TCP/UDP protocols)
- Update LKE endpoint output to use api_endpoints[0] instead of deprecated hostname
- Add OBJECT_STORAGE_CLUSTER variable for object storage configuration
- Update Makefile with new variables and validate target
- Add storage/outputs.tf for standardized object storage outputs
- Update .env and .env.example with clean format (no inline comments)
- Update all terraform.tfvars.example files for consistency
2026-11-17 22:09:31 -05:00
27 changed files with 813 additions and 272 deletions

No files matched your search

+22
View File
@@ -15,3 +15,25 @@ Five canonical triage roles mapped to labels: `needs-triage`, `needs-info`, `rea
### Domain docs
Single-context layout: `GLOSSARY.md` at repo root and `docs/adr/` for architecture decisions. See `docs/agents/domain.md`.
### Infrastructure modules
The `infra/` directory contains Terraform configurations with these modules:
- `backend/` - Terraform state backend (Object Storage S3-compatible)
- `postgres/` - LKE clusters for staging/production
- `storage/` - Object Storage buckets for uploads and state
- `network/` - Firewall rules for API (8000), PostgreSQL (5432), Adminer (8080)
See `infra/README.md` and `docs/adr/0002-linode-provider-v2-migration.md` for details.
## Provider Versions
### Linode Terraform Provider
**v2.0+** (current branch) - Migrated from v1 with breaking changes documented in ADR-0002. Key differences:
- Object Storage: `cluster` → `region`
- Firewall: Updated format with `action`, `ipv4`, `ipv6`
- LKE: `pools` → `pool`
- Tags: Set format (`key=value`)
@@ -61,7 +61,7 @@ infra/
#### PostgreSQL (`infra/postgres/`)
- LKE (Linode Kubernetes Engine) clusters for staging and production
- Configurable pool sizes (10 for staging, 25 for production)
- Configurable pool sizes (2 for staging, 3 for production)
- Separate database names (`hapi_staging`, `hapi_production`)
- Random password generation for database access
@@ -128,9 +128,24 @@ make output # Show outputs
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`](./0002-linode-provider-v2-migration.md) for full details.
## References
- [Issue 1: Linode Infrastructure with Terraform](../../issues/1)
- [Linode Terraform Provider](https://registry.terraform.io/providers/linode/linode/latest/docs)
- [ADR-0002: Linode Provider v2 Migration](./0002-linode-provider-v2-migration.md)
- [Linode Terraform Provider v2.0 Release](https://github.com/linode/terraform-provider-linode/releases/tag/v2.0.0)
- [Linode Provider Migration Guide](https://registry.terraform.io/providers/linode/linode/latest/docs/guides/version-2-upgrade)
- [Terraform Best Practices](https://developer.hashicorp.com/terraform/tutorials/best-practices)
- [HAPI MVP Spec](../../MVP-SPEC.md)
@@ -0,0 +1,174 @@
# Linode Terraform Provider v2 Quick Reference
This guide covers the key differences when working with Linode Provider v2.
## Object Storage
### v1 → v2 Changes
| v1 | v2 |
|---|---|
| `cluster = var.linode_object_storage_cluster` | `region = var.linode_object_storage_cluster` |
| `linode_object_storage_cluster` resource | Removed - cluster is implicit |
### Example
```hcl
# v1
resource "linode_object_storage_bucket" "uploads" {
cluster = var.linode_object_storage_cluster
# ...
}
# v2
resource "linode_object_storage_bucket" "uploads" {
region = var.linode_object_storage_cluster
# ...
}
```
## Firewall Rules
### v1 → v2 Changes
| v1 | v2 |
|---|---|
| `cidrs = ["0.0.0.0/0"]` | `ipv4 = ["0.0.0.0/0"]`, `ipv6 = ["::/0"]` |
| `direction = "inbound"` | `action = "ACCEPT"` or `action = "DROP"` |
| `protocol = "tcp"` | `protocol = "TCP"` (uppercase) |
| `ports = "8000"` | `ports = "8000"` (same) |
### Example
```hcl
# v1
resource "linode_firewall_rule" "api" {
direction = "inbound"
protocol = "tcp"
ports = "8000"
cidrs = ["0.0.0.0/0", "::/0"]
}
# v2
resource "linode_firewall" "hapi" {
inbound {
action = "ACCEPT"
protocol = "TCP"
ports = "8000"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
}
```
## LKE Clusters
### v1 → v2 Changes
| v1 | v2 |
|---|---|
| `pools { ... }` | `pool { ... }` |
| `tags = { Environment = "staging" }` | `tags = toset(["Environment=staging"])` |
### Example
```hcl
# v1
resource "linode_lke_cluster" "postgres" {
pools {
count = 2
type = "g6-standard-4"
}
tags = {
Environment = "staging"
}
}
# v2
resource "linode_lke_cluster" "postgres" {
pool {
count = 2
type = "g6-standard-4"
}
tags = toset([
for k, v in { Environment = "staging" } : "${k}=${v}"
])
}
```
## Tags
### v1 → v2 Changes
- **v1**: Map format `{ key = "value" }`
- **v2**: Set of strings `["key=value"]`
### Conversion
```hcl
# v1
tags = {
Environment = "staging"
Project = "hapi"
}
# v2
tags = toset([
"Environment=staging",
"Project=hapi"
])
# Dynamic conversion
tags = toset([
for k, v in var.tags : "${k}=${v}"
])
```
## Common Patterns
### Environment-Specific Resources
```hcl
# Firewall with environment suffix
resource "linode_firewall" "hapi" {
label = "hapi-firewall-${var.environment}"
}
# Bucket with environment suffix
resource "linode_object_storage_bucket" "uploads" {
label = "hapi-uploads-${var.environment}"
}
```
### Provider Configuration
```hcl
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
}
}
provider "linode" {
token = var.linode_token
}
```
## Migration Checklist
- [ ] Update provider version constraint to `~> 2.0`
- [ ] Replace `cluster` with `region` on Object Storage buckets
- [ ] Remove `linode_object_storage_cluster` resource if present
- [ ] Convert firewall rules to v2 format
- [ ] Update LKE `pools` → `pool`
- [ ] Convert tags from map to set format
- [ ] Update firewall labels to include environment suffix
## References
- [Linode Provider v2.0 Release](https://github.com/linode/terraform-provider-linode/releases/tag/v2.0.0)
- [Migration Guide](https://registry.terraform.io/providers/linode/linode/latest/docs/guides/version-2-upgrade)
- [ADR-0002](../docs/adr/0002-linode-provider-v2-migration.md)
@@ -0,0 +1,156 @@
# ADR-0002: Linode Provider v2 Migration
## Context
The Linode Terraform provider released version 2.0 with significant breaking changes that affect our infrastructure configuration. This ADR documents the migration decisions.
### Provider v1 vs v2 Key Differences
| Change | Provider v1 | Provider v2 |
|--------|-------------|-------------|
| Object Storage cluster resource | `linode_object_storage_cluster` (optional) | Removed - cluster specified via `region` |
| Object Storage bucket attribute | `cluster` | `region` |
| Firewall rules format | List of objects with `cidrs` | Object with `ipv4`/`ipv6` and `action` |
| Firewall rule attributes | `direction`, `protocol`, `ports`, `cidrs` | `action`, `protocol`, `ports`, `ipv4`, `ipv6` |
| LKE pool attribute | `pools` | `pool` |
| LKE tags format | Map with `Environment = value` | Set of `key=value` strings |
### Existing Infrastructure (v1)
The original implementation (ADR-0001) used:
- `linode_object_storage_cluster` resource for Object Storage cluster
- `cluster` attribute on bucket resources
- List-based firewall rules with `cidrs`
- `pools` block for LKE clusters
- Map-based tags
### Trigger for Migration
- Linode provider v2 became stable and widely adopted
- Newer features and bug fixes only available in v2
- Consistency with Linode's evolving API
## Decision
We will migrate all infrastructure modules to use **Linode provider v2.0+** (`~> 2.0`).
### Migration Approach
1. **Incremental rollout** - Update modules one at a time
2. **Backward-compatible variables** - Keep variable names stable, update usage internally
3. **Documentation** - Document v2-specific patterns and differences
### What Stays the Same
- Module structure (postgres/, storage/, network/, backend/)
- Variable names and common patterns
- Output structure and naming
- Environment separation (staging/production)
## Status
**Accepted** - Migration implemented in `feat/01a-linode-terraform-cleanup` branch.
## Implementation
### Changes by Module
#### Backend (`infra/backend/`)
**Changes:**
- Updated provider version constraint to `~> 2.0`
- Object Storage bucket configuration updated for v2 API
#### PostgreSQL (`infra/postgres/`)
**Changes:**
- Provider v2 constraint added to `terraform` block
- `cluster` → `region` on `linode_object_storage_bucket`
- `pools` → `pool` on `linode_lke_cluster`
- Tags converted from map to set of `key=value` strings
- Environment variable added to module
#### Object Storage (`infra/storage/`)
**Changes:**
- Provider v2 constraint added to `terraform` block
- Removed `linode_object_storage_cluster` resource
- `cluster` → `region` on bucket resources
- Removed `depends_on` referencing removed cluster resource
- Removed bucket policy configurations (simplified approach)
#### Networking (`infra/network/`)
**Changes:**
- Provider v2 constraint added to `terraform` block
- Firewall rules updated to v2 format:
- `cidrs` → `ipv4`/`ipv6`
- Added `action` field (`ACCEPT`/`DROP`)
- `protocol` now uppercase (`TCP`/`UDP`/`ICMP`)
- Environment suffix added to firewall labels
- Tags converted to set format
- `linode_object_storage_cluster` variable added
### Variable Changes
**Added to `infra/variables.tf`:**
```hcl
variable "object_storage_access_key" {
description = "Linode Object Storage access key for backend state storage"
type = string
}
variable "object_storage_secret_key" {
description = "Linode Object Storage secret key for backend state storage"
type = string
sensitive = true
}
variable "linode_object_storage_cluster" {
description = "Linode Object Storage cluster for backend state storage"
type = string
default = "us-east-1"
}
```
**Added to module variables:**
- `environment` variable added to `postgres/variables.tf` and `storage/variables.tf`
## Consequences
### Positive
- **Future-proof** - Using stable, actively maintained provider version
- **Simplified architecture** - Removed `linode_object_storage_cluster` resource
- **More consistent API** - v2 aligns with Linode's current API conventions
- **Better tagging** - Set-based tags provide more flexibility
### Negative
- **Breaking changes** - Existing v1 configurations require updates
- **Documentation lag** - Need to update docs for v2 patterns
- **Learning curve** - Team needs to understand v2 differences
### Migration Checklist
- [x] Provider version updated in all modules
- [x] Object Storage bucket `cluster` → `region` attribute updated
- [x] Firewall rules converted to v2 format
- [x] LKE pools renamed to `pool`
- [x] Tags converted to set format
- [x] Environment variable added to modules
- [x] Documentation updated
## Next Steps
1. **CI/CD integration** - Update workflows to use provider v2
2. **State migration** - Plan for state file compatibility
3. **Testing** - Verify all modules work with v2 in CI/CD
4. **Documentation** - Update user-facing docs with v2 patterns
5. **Runbook updates** - Document v2-specific operations
## References
- [Linode Terraform Provider v2.0 Release](https://github.com/linode/terraform-provider-linode/releases/tag/v2.0.0)
- [Linode Provider Migration Guide](https://registry.terraform.io/providers/linode/linode/latest/docs/guides/version-2-upgrade)
- [ADR-0001: Linode Infrastructure with Terraform](./0001-linode-infrastructure-with-terraform.md)
+13 -6
View File
@@ -5,6 +5,7 @@
# Environment defaults
ENV ?= staging
REGION ?= us-east
OBJECT_STORAGE_CLUSTER ?= us-east-1
# Initialize Terraform
init:
@@ -17,38 +18,39 @@ init-backend:
terraform init -reconfigure \
-backend-config=bucket=hapi-terraform-state-${ENV} \
-backend-config=key=terraform/${ENV}/terraform.tfstate \
-backend-config=region=${REGION}
-backend-config=region=${OBJECT_STORAGE_CLUSTER}
# Generate plan
plan:
@echo "Generating Terraform plan for ${ENV}..."
terraform plan -var="environment=${ENV}"
terraform plan -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}"
# Apply changes
apply:
@echo "Applying Terraform changes for ${ENV}..."
terraform apply -var="environment=${ENV}"
terraform apply -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}"
# Apply with auto-approve
apply-auto:
@echo "Applying Terraform changes for ${ENV} (auto-approve)..."
terraform apply -var="environment=${ENV}" -auto-approve
terraform apply -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}" -auto-approve
# Destroy infrastructure
destroy:
@echo "Destroying Terraform infrastructure for ${ENV}..."
terraform destroy -var="environment=${ENV}"
terraform destroy -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}"
# Show outputs
output:
@echo "Terraform outputs:"
terraform output
terraform output -json | jq -r 'to_entries | .[] | "\(.key) = \(.value.value)"' 2>/dev/null || terraform output
# Load environment variables
env:
@echo "Environment variables:"
@echo " ENV=${ENV}"
@echo " REGION=${REGION}"
@echo " OBJECT_STORAGE_CLUSTER=${OBJECT_STORAGE_CLUSTER}"
@echo " LINODE_TOKEN=${LINODE_TOKEN}"
@echo " OBJECT_STORAGE_ACCESS_KEY=${OBJECT_STORAGE_ACCESS_KEY}"
@echo " OBJECT_STORAGE_SECRET_KEY=${OBJECT_STORAGE_SECRET_KEY}"
@@ -56,6 +58,11 @@ env:
# Run all checks
check: init plan apply
# Validate configuration
validate:
@echo "Validating Terraform configuration..."
terraform validate
# Clean up local state
clean:
@echo "Cleaning local Terraform files..."
+44 -4
View File
@@ -2,6 +2,31 @@
This directory contains Terraform configurations for provisioning HAPI infrastructure on Linode.
## Provider Version
This repository uses **Linode Terraform Provider v2.0+** (`~> 2.0`). See [`docs/adr/0002-linode-provider-v2-migration.md`](../docs/adr/0002-linode-provider-v2-migration.md) for details on the migration from v1 to v2.
Key differences in v2:
- Object Storage: `cluster` → `region` attribute
- Firewall rules: Updated format with `action`, `ipv4`, `ipv6`
- LKE: `pools` → `pool` block
- Tags: Converted to set format (`key=value` strings)
## Directory Structure
```
infra/
├── backend/ # Terraform state backend (Object Storage S3-compatible)
├── postgres/ # PostgreSQL database instances (LKE clusters)
├── 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 # This file
```
## Directory Structure
```
@@ -21,6 +46,7 @@ infra/
- Linode account with API access
- Linode CLI (`linode-cli`) or direct API access for Object Storage credentials
- Terraform 1.9.0 or higher
- Linode Terraform Provider v2.0+
## Setup
@@ -62,7 +88,8 @@ terraform apply -var="environment=production"
## Resources Provisioned
### PostgreSQL
- Database instance (staging and production)
- LKE (Linode Kubernetes Engine) cluster for staging and production
- Database instances within clusters (`hapi_staging`, `hapi_production`)
- Connection pooling configuration
- Automated backups
@@ -70,19 +97,21 @@ terraform apply -var="environment=production"
- Upload buckets for staging and production
- Terraform state storage
- S3-compatible API
- Object Storage cluster configuration
### Networking
- Firewall rules for:
- API (port 8000)
- PostgreSQL (port 5432)
- Adminer (port 8080)
- Inbound and outbound rules
- Inbound and outbound rules with DROP policy by default
- Environment-specific firewall labels
## Outputs
- PostgreSQL endpoint, port, database name, credentials
- PostgreSQL endpoint (Kubernetes API endpoint), port, database name, credentials
- Object Storage endpoint, access keys
- Firewall configuration
- Firewall configuration (rules, ID, and label)
- Terraform backend configuration
## Environment-Specific Configuration
@@ -110,3 +139,14 @@ Terraform state is automatically stored in Linode Object Storage under the confi
### Adding New Resources
Add modules to `main.tf` and create corresponding directories under `infra/` following the established pattern.
## Provider v2 Migration Notes
The infrastructure has been migrated to Linode Provider v2. Key considerations:
- **Object Storage**: Use `region` instead of `cluster` for bucket configuration
- **Firewall Rules**: New format with `action` (ACCEPT/DROP), `ipv4`, and `ipv6` instead of `cidrs`
- **Tags**: Converted from map to set of `key=value` strings
- **LKE Pools**: Block renamed from `pools` to `pool`
See `docs/adr/0002-linode-provider-v2-migration.md` for complete details.
+41
View File
@@ -0,0 +1,41 @@
# Backend configuration for HAPI Terraform
This module configures the Terraform backend to store state in Linode Object Storage.
## Configuration
The backend uses S3-compatible API provided by Linode Object Storage.
```hcl
terraform {
backend "s3" {
bucket = "hapi-terraform-state-${var.environment}"
key = "hapi/terraform.tfstate"
region = var.linode_object_storage_cluster
endpoint = "${var.linode_object_storage_cluster}.linodeobjects.com"
access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key
skip_region = true
skip_credentials_validation = true
skip_metadata_api_check = true
}
}
```
## Variables
| Variable | Description | Type | Default |
|----------|-------------|------|---------|
| `environment` | Deployment environment | string | `"staging"` |
| `linode_object_storage_cluster` | Object Storage cluster/region | string | `"us-east-1"` |
| `object_storage_access_key` | Access key for Object Storage | string | - |
| `object_storage_secret_key` | Secret key for Object Storage (sensitive) | string | - |
## Outputs
| Output | Description |
|--------|-------------|
| `backend_bucket` | Name of the state bucket |
| `backend_key` | Path to state file within bucket |
| `backend_region` | Object Storage cluster/region |
| `backend_endpoint` | S3-compatible endpoint URL |
+5 -13
View File
@@ -1,14 +1,6 @@
# Terraform Backend Configuration
# Configure this in your main terraform.tfvars or environment variables
# Backend module example configuration
# Linode Object Storage as Terraform backend
# Copy this to your terraform.tfvars or set as environment variables
linode_token = "<YOUR_LINODE_API_TOKEN>"
linode_region = "us-east"
environment = "staging" # or "production"
project_name = "hapi"
# Object Storage credentials
object_storage_access_key = "<YOUR_ACCESS_KEY>"
object_storage_secret_key = "<YOUR_SECRET_KEY>"
environment = "staging"
linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
+10 -38
View File
@@ -1,44 +1,16 @@
# Backend configuration for Terraform state in Linode Object Storage
# This module provides the configuration needed for remote state storage
# Backend configuration for HAPI Terraform state
resource "linode_object_storage_bucket" "terraform_state" {
label = "hapi-terraform-state-${var.environment}"
cluster = var.linode_object_storage_cluster
access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key
}
locals {
backend_config = {
terraform {
backend "s3" {
# State stored in Object Storage
bucket = "hapi-terraform-state-${var.environment}"
key = "terraform/${var.environment}/terraform.tfstate"
region = var.linode_region
endpoint = "https://${var.linode_region}.linodeobjects.com"
encrypt = true
key = "hapi/terraform.tfstate"
region = var.linode_object_storage_cluster
endpoint = "${var.linode_object_storage_cluster}.linodeobjects.com"
access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key
skip_region_validation = true
}
}
output "backend_config" {
description = "Terraform backend configuration"
value = local.backend_config
}
output "backend_s3_config" {
description = "S3-style backend configuration for Terraform"
value = {
bucket = local.backend_config.bucket
key = local.backend_config.key
region = local.backend_config.region
endpoint = local.backend_config.endpoint
encrypt = local.backend_config.encrypt
access_key = local.backend_config.access_key
secret_key = local.backend_config.secret_key
skip_region_validation = local.backend_config.skip_region_validation
}
sensitive = {
secret_key = local.backend_config.secret_key
skip_region = true
skip_credentials_validation = true
skip_metadata_api_check = true
}
}
+21
View File
@@ -0,0 +1,21 @@
# Backend outputs
output "backend_bucket" {
description = "Name of the state bucket"
value = "hapi-terraform-state-${var.environment}"
}
output "backend_key" {
description = "Path to state file within bucket"
value = "hapi/terraform.tfstate"
}
output "backend_region" {
description = "Object Storage cluster/region"
value = var.linode_object_storage_cluster
}
output "backend_endpoint" {
description = "S3-compatible endpoint URL"
value = "${var.linode_object_storage_cluster}.linodeobjects.com"
}
+3 -3
View File
@@ -1,6 +1,6 @@
# Backend module variables
linode_region = "us-east"
# Backend example configuration
environment = "staging"
linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
environment = "staging"
+17 -31
View File
@@ -1,42 +1,28 @@
# Variables for Backend module
variable "linode_region" {
description = "Linode region for Object Storage"
type = string
default = "us-east"
}
variable "linode_object_storage_cluster" {
description = "Linode Object Storage cluster"
type = string
default = "us-east-1"
}
variable "object_storage_access_key" {
description = "Object Storage access key"
type = string
}
variable "object_storage_secret_key" {
description = "Object Storage secret key"
type = string
sensitive = true
}
# Backend variables
variable "environment" {
description = "Deployment environment (staging or production)"
type = string
default = "staging"
validation {
condition = contains(["staging", "production"], var.environment)
error_message = "Environment must be 'staging' or 'production'."
}
}
variable "tags" {
description = "Common tags for all resources"
type = map(string)
default = {
ManagedBy = "terraform"
Project = "hapi"
}
variable "linode_object_storage_cluster" {
description = "Linode Object Storage cluster for backend state storage"
type = string
default = "us-east-1"
}
variable "object_storage_access_key" {
description = "Linode Object Storage access key for backend state storage"
type = string
}
variable "object_storage_secret_key" {
description = "Linode Object Storage secret key for backend state storage"
type = string
sensitive = true
}
+39 -4
View File
@@ -1,21 +1,56 @@
# Main Terraform configuration for HAPI infrastructure
# This file orchestrates all infrastructure components
terraform {
required_version = ">= 1.9.0"
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
# Provider configurations are in providers.tf
# Modules
module "backend" {
source = "./backend"
environment = var.environment
linode_object_storage_cluster = var.linode_object_storage_cluster
object_storage_access_key = var.object_storage_access_key
object_storage_secret_key = var.object_storage_secret_key
}
module "postgres" {
source = "./postgres"
linode_region = var.linode_region
linode_object_storage_cluster = var.linode_object_storage_cluster
object_storage_access_key = var.object_storage_access_key
object_storage_secret_key = var.object_storage_secret_key
environment = var.environment
}
module "storage" {
source = "./storage"
linode_region = var.linode_region
linode_object_storage_cluster = var.linode_object_storage_cluster
environment = var.environment
object_storage_access_key = var.object_storage_access_key
object_storage_secret_key = var.object_storage_secret_key
}
module "network" {
source = "./network"
}
# Backend configuration for Terraform state
# State will be stored in Linode Object Storage
# Backend configuration is defined in infra/backend/
linode_region = var.linode_region
environment = var.environment
}
+74 -68
View File
@@ -1,89 +1,95 @@
# Networking module for HAPI
# Creates firewall rules for API, PostgreSQL, and Adminer
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
resource "linode_firewall" "hapi" {
label = "hapi-firewall"
tags = var.tags
label = "hapi-firewall-${var.environment}"
tags = toset([for k, v in var.tags : "${k}=${v}"])
# Default policies - DROP for security (explicit ACCEPT rules for allowed traffic)
inbound_policy = "DROP"
outbound_policy = "DROP"
# Inbound rules
inbound = [
{
label = "allow-api"
protocol = "tcp"
ports = "8000"
cidrs = ["0.0.0.0/0", "::/0"]
},
{
label = "allow-postgres"
protocol = "tcp"
ports = "5432"
cidrs = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
},
{
label = "allow-adminer"
protocol = "tcp"
ports = "8080"
cidrs = ["0.0.0.0/0", "::/0"]
},
{
label = "allow-https"
protocol = "tcp"
ports = "443"
cidrs = ["0.0.0.0/0", "::/0"]
},
{
label = "allow-http"
protocol = "tcp"
ports = "80"
cidrs = ["0.0.0.0/0", "::/0"]
}
]
inbound {
action = "ACCEPT"
label = "allow-api-${var.environment}"
protocol = "TCP"
ports = "8000"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
inbound {
action = "ACCEPT"
label = "allow-postgres-${var.environment}"
protocol = "TCP"
ports = "5432"
ipv4 = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
}
inbound {
action = "ACCEPT"
label = "allow-adminer-${var.environment}"
protocol = "TCP"
ports = "8080"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
inbound {
action = "ACCEPT"
label = "allow-https-${var.environment}"
protocol = "TCP"
ports = "443"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
inbound {
action = "ACCEPT"
label = "allow-http-${var.environment}"
protocol = "TCP"
ports = "80"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
# Outbound rules
outbound = [
{
label = "allow-all-outbound"
protocol = "tcp"
ports = "0-65535"
cidrs = ["0.0.0.0/0", "::/0"]
}
]
}
resource "linode_firewall_rule" "api_health" {
for_each = toset(["staging", "production"])
firewall_id = linode_firewall.hapi.id
label = "api-health-${each.key}"
direction = "inbound"
protocol = "tcp"
ports = "8000"
cidrs = ["0.0.0.0/0", "::/0"]
}
resource "linode_firewall_rule" "adminer_access" {
for_each = toset(["staging", "production"])
firewall_id = linode_firewall.hapi.id
label = "adminer-${each.key}"
direction = "inbound"
protocol = "tcp"
ports = "8080"
cidrs = ["0.0.0.0/0", "::/0"]
outbound {
action = "ACCEPT"
label = "allow-all-outbound-${var.environment}"
protocol = "TCP"
ports = "0-65535"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
}
# Firewall rule configurations
locals {
firewall_config = {
staging = {
api_port = 8000
api_port = 8000
postgres_port = 5432
adminer_port = 8080
adminer_port = 8080
}
production = {
api_port = 8000
api_port = 8000
postgres_port = 5432
adminer_port = 8080
adminer_port = 8080
}
}
}
+14 -3
View File
@@ -1,3 +1,14 @@
# Network module variables
linode_region = "us-east"
environment = "staging"
# Network module example configuration
# Linode region for infrastructure
linode_region = "us-east"
# Object Storage cluster/region for backend state and buckets
linode_object_storage_cluster = "us-east-1"
# Environment (affects firewall and resource naming)
environment = "staging"
# Object Storage credentials (for backend state)
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
+6
View File
@@ -15,6 +15,12 @@ variable "tags" {
}
}
variable "linode_object_storage_cluster" {
description = "Linode Object Storage cluster for backend state storage"
type = string
default = "us-east-1"
}
variable "environment" {
description = "Deployment environment (staging or production)"
type = string
+19 -4
View File
@@ -1,7 +1,7 @@
# Outputs for HAPI Terraform infrastructure
output "postgres_endpoint" {
description = "PostgreSQL endpoint for the environment"
description = "PostgreSQL endpoint for the environment (Kubernetes API endpoint)"
value = module.postgres.endpoint
}
@@ -47,11 +47,26 @@ output "firewall_rules" {
value = module.network.firewall_rules
}
output "firewall_id" {
description = "Linode firewall ID"
value = module.network.firewall_id
}
output "firewall_label" {
description = "Linode firewall label"
value = module.network.firewall_label
}
output "terraform_backend_config" {
description = "Configuration for Terraform backend in Object Storage"
value = {
bucket = module.storage.state_bucket_name
key = "terraform/${var.environment}/terraform.tfstate"
region = var.linode_region
bucket = module.backend.backend_bucket
key = module.backend.backend_key
region = module.backend.backend_region
}
}
output "backend_endpoint" {
description = "Object Storage endpoint for backend state"
value = module.backend.backend_endpoint
}
+22 -9
View File
@@ -1,12 +1,25 @@
# PostgreSQL module for HAPI
# Creates separate database instances for staging and production
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
# Object Storage buckets for PostgreSQL state
resource "linode_object_storage_bucket" "state" {
for_each = toset(["staging", "production"])
label = "hapi-postgres-state-${each.key}"
cluster = var.linode_object_storage_cluster
region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key
}
@@ -14,12 +27,12 @@ resource "linode_object_storage_bucket" "state" {
resource "linode_lke_cluster" "postgres" {
for_each = toset(["staging", "production"])
region = var.linode_region
label = "hapi-postgres-${each.key}"
region = var.linode_region
label = "hapi-postgres-${each.key}"
k8s_version = var.linode_lke_version
tags = merge(var.tags, { Environment = each.key })
tags = toset([for k, v in merge(var.tags, { Environment = each.key }) : "${k}=${v}"])
pools {
pool {
count = each.key == "production" ? 2 : 1
type = var.linode_node_type
}
@@ -29,19 +42,19 @@ resource "linode_lke_cluster" "postgres" {
locals {
databases = {
staging = {
name = "hapi_staging"
name = "hapi_staging"
pool_size = 2
}
production = {
name = "hapi_production"
name = "hapi_production"
pool_size = 3
}
}
}
output "endpoint" {
description = "PostgreSQL endpoint"
value = linode_lke_cluster.postgres[var.environment].hostname
description = "PostgreSQL endpoint (Kubernetes API endpoint)"
value = linode_lke_cluster.postgres[var.environment].api_endpoints[0]
}
output "port" {
+2 -2
View File
@@ -4,6 +4,6 @@ resource "random_string" "password" {
length = 32
upper = true
lower = true
numeric = true
special = false
numeric = true
special = false
}
+19 -4
View File
@@ -1,5 +1,20 @@
# Postgres module variables
linode_region = "us-east"
linode_lke_version = "1.30"
linode_node_type = "g6-standard-4"
# PostgreSQL module example configuration
# Linode region for LKE cluster
linode_region = "us-east"
# LKE Kubernetes version
linode_lke_version = "1.30"
# Node type for LKE pool
linode_node_type = "g6-standard-4"
# Object Storage cluster/region for state storage
linode_object_storage_cluster = "us-east-1"
# Object Storage credentials (for state storage)
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
# Environment (affects cluster and database naming)
environment = "staging"
+9
View File
@@ -43,3 +43,12 @@ variable "tags" {
Project = "hapi"
}
}
variable "environment" {
description = "Deployment environment (staging or production)"
type = string
validation {
condition = contains(["staging", "production"], var.environment)
error_message = "Environment must be 'staging' or 'production'."
}
}
+3 -1
View File
@@ -1,8 +1,10 @@
# Provider configurations for HAPI Terraform infrastructure
# Provider configurations
# These use the sources defined in main.tf terraform block
provider "linode" {
token = var.linode_token
region = var.linode_region
}
provider "random" {}
+15 -79
View File
@@ -1,97 +1,33 @@
# Object Storage module for HAPI
# Creates buckets for uploads and Terraform state
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
resource "linode_object_storage_bucket" "uploads" {
for_each = toset(["staging", "production"])
label = "hapi-uploads-${each.key}"
cluster = var.linode_object_storage_cluster
region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key
depends_on = [linode_object_storage_cluster.this]
}
resource "linode_object_storage_bucket" "state" {
for_each = toset(["staging", "production"])
label = "hapi-terraform-state-${each.key}"
cluster = var.linode_object_storage_cluster
region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key
depends_on = [linode_object_storage_cluster.this]
}
resource "linode_object_storage_cluster" "this" {
label = "hapi-object-storage"
region = var.linode_region
}
# Bucket configurations
locals {
bucket_config = {
staging = {
uploads = {
label = "hapi-uploads-staging"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": ["*"] },
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": ["arn:aws:s3:::hapi-uploads-staging/*"]
}
]
})
}
state = {
label = "hapi-terraform-state-staging"
}
}
production = {
uploads = {
label = "hapi-uploads-production"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": ["*"] },
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": ["arn:aws:s3:::hapi-uploads-production/*"]
}
]
})
}
state = {
label = "hapi-terraform-state-production"
}
}
}
}
output "endpoint" {
description = "Object Storage endpoint"
value = "https://${var.linode_region}.linodeobjects.com"
}
output "access_key" {
description = "Object Storage access key"
value = var.object_storage_access_key
}
output "secret_key" {
description = "Object Storage secret key"
value = var.object_storage_secret_key
sensitive = true
}
output "state_bucket_name" {
description = "Name of the Terraform state bucket"
value = "hapi-terraform-state-${var.environment}"
}
output "uploads_bucket_name" {
description = "Name of the uploads bucket"
value = "hapi-uploads-${var.environment}"
}
+27
View File
@@ -0,0 +1,27 @@
# Outputs for Object Storage module
output "endpoint" {
description = "Object Storage endpoint"
value = var.linode_object_storage_cluster
}
output "access_key" {
description = "Object Storage access key"
value = var.object_storage_access_key
}
output "secret_key" {
description = "Object Storage secret key (sensitive)"
value = var.object_storage_secret_key
sensitive = true
}
output "state_bucket_name" {
description = "Name of the Terraform state bucket"
value = "hapi-terraform-state-${var.environment}"
}
output "uploads_bucket_name" {
description = "Name of the uploads bucket"
value = "hapi-uploads-${var.environment}"
}
+9 -1
View File
@@ -1,6 +1,14 @@
# Storage module variables
# Storage module example configuration
# Linode region for infrastructure
linode_region = "us-east"
# Object Storage cluster (v2 uses 'region' instead of 'cluster')
linode_object_storage_cluster = "us-east-1"
# Object Storage credentials
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
# Environment (affects bucket and firewall naming)
environment = "staging"
+14
View File
@@ -0,0 +1,14 @@
# HAPI Infrastructure Configuration
# Copy to terraform.tfvars or use with -var-file
# Linode configuration
linode_token = "your-linode-api-token"
linode_region = "us-east"
linode_object_storage_cluster = "us-east-1"
# Object Storage credentials
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
# Environment
environment = "staging" # or "production"
+18
View File
@@ -36,3 +36,21 @@ variable "tags" {
Project = "hapi"
}
}
# Backend configuration variables
variable "object_storage_access_key" {
description = "Linode Object Storage access key for backend state storage"
type = string
}
variable "object_storage_secret_key" {
description = "Linode Object Storage secret key for backend state storage"
type = string
sensitive = true
}
variable "linode_object_storage_cluster" {
description = "Linode Object Storage cluster for backend state storage"
type = string
default = "us-east-1"
}