Merge pull request 'feat: Linode Provider v2 migration and infrastructure cleanup' (#35) from feat/01a-linode-terraform-cleanup into main

This commit was merged in pull request #35.
This commit is contained in:
bobby committed 2026-11-17 23:44:07 -05:00
commit 1b17000d16
26 files changed
+789 -248

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 ### Domain docs
Single-context layout: `GLOSSARY.md` at repo root and `docs/adr/` for architecture decisions. See `docs/agents/domain.md`. 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/`) #### PostgreSQL (`infra/postgres/`)
- LKE (Linode Kubernetes Engine) clusters for staging and production - 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`) - Separate database names (`hapi_staging`, `hapi_production`)
- Random password generation for database access - Random password generation for database access
@@ -128,9 +128,24 @@ make output # Show outputs
4. Create runbooks for common operations (scaling, backups, troubleshooting) 4. Create runbooks for common operations (scaling, backups, troubleshooting)
5. Set up monitoring for infrastructure resources 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 ## References
- [Issue 1: Linode Infrastructure with Terraform](../../issues/1) - [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) - [Terraform Best Practices](https://developer.hashicorp.com/terraform/tutorials/best-practices)
- [HAPI MVP Spec](../../MVP-SPEC.md) - [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 # Environment defaults
ENV ?= staging ENV ?= staging
REGION ?= us-east REGION ?= us-east
OBJECT_STORAGE_CLUSTER ?= us-east-1
# Initialize Terraform # Initialize Terraform
init: init:
@@ -17,38 +18,39 @@ init-backend:
terraform init -reconfigure \ terraform init -reconfigure \
-backend-config=bucket=hapi-terraform-state-${ENV} \ -backend-config=bucket=hapi-terraform-state-${ENV} \
-backend-config=key=terraform/${ENV}/terraform.tfstate \ -backend-config=key=terraform/${ENV}/terraform.tfstate \
-backend-config=region=${REGION} -backend-config=region=${OBJECT_STORAGE_CLUSTER}
# Generate plan # Generate plan
plan: plan:
@echo "Generating Terraform plan for ${ENV}..." @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 changes
apply: apply:
@echo "Applying Terraform changes for ${ENV}..." @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 with auto-approve
apply-auto: apply-auto:
@echo "Applying Terraform changes for ${ENV} (auto-approve)..." @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 infrastructure
destroy: destroy:
@echo "Destroying Terraform infrastructure for ${ENV}..." @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 # Show outputs
output: output:
@echo "Terraform outputs:" @echo "Terraform outputs:"
terraform output terraform output -json | jq -r 'to_entries | .[] | "\(.key) = \(.value.value)"' 2>/dev/null || terraform output
# Load environment variables # Load environment variables
env: env:
@echo "Environment variables:" @echo "Environment variables:"
@echo " ENV=${ENV}" @echo " ENV=${ENV}"
@echo " REGION=${REGION}" @echo " REGION=${REGION}"
@echo " OBJECT_STORAGE_CLUSTER=${OBJECT_STORAGE_CLUSTER}"
@echo " LINODE_TOKEN=${LINODE_TOKEN}" @echo " LINODE_TOKEN=${LINODE_TOKEN}"
@echo " OBJECT_STORAGE_ACCESS_KEY=${OBJECT_STORAGE_ACCESS_KEY}" @echo " OBJECT_STORAGE_ACCESS_KEY=${OBJECT_STORAGE_ACCESS_KEY}"
@echo " OBJECT_STORAGE_SECRET_KEY=${OBJECT_STORAGE_SECRET_KEY}" @echo " OBJECT_STORAGE_SECRET_KEY=${OBJECT_STORAGE_SECRET_KEY}"
@@ -56,6 +58,11 @@ env:
# Run all checks # Run all checks
check: init plan apply check: init plan apply
# Validate configuration
validate:
@echo "Validating Terraform configuration..."
terraform validate
# Clean up local state # Clean up local state
clean: clean:
@echo "Cleaning local Terraform files..." @echo "Cleaning local Terraform files..."
+44 -4
View File
@@ -2,6 +2,31 @@
This directory contains Terraform configurations for provisioning HAPI infrastructure on Linode. 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 ## Directory Structure
``` ```
@@ -21,6 +46,7 @@ infra/
- Linode account with API access - Linode account with API access
- Linode CLI (`linode-cli`) or direct API access for Object Storage credentials - Linode CLI (`linode-cli`) or direct API access for Object Storage credentials
- Terraform 1.9.0 or higher - Terraform 1.9.0 or higher
- Linode Terraform Provider v2.0+
## Setup ## Setup
@@ -62,7 +88,8 @@ terraform apply -var="environment=production"
## Resources Provisioned ## Resources Provisioned
### PostgreSQL ### 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 - Connection pooling configuration
- Automated backups - Automated backups
@@ -70,19 +97,21 @@ terraform apply -var="environment=production"
- Upload buckets for staging and production - Upload buckets for staging and production
- Terraform state storage - Terraform state storage
- S3-compatible API - S3-compatible API
- Object Storage cluster configuration
### Networking ### Networking
- Firewall rules for: - Firewall rules for:
- API (port 8000) - API (port 8000)
- PostgreSQL (port 5432) - PostgreSQL (port 5432)
- Adminer (port 8080) - Adminer (port 8080)
- Inbound and outbound rules - Inbound and outbound rules with DROP policy by default
- Environment-specific firewall labels
## Outputs ## Outputs
- PostgreSQL endpoint, port, database name, credentials - PostgreSQL endpoint (Kubernetes API endpoint), port, database name, credentials
- Object Storage endpoint, access keys - Object Storage endpoint, access keys
- Firewall configuration - Firewall configuration (rules, ID, and label)
- Terraform backend configuration - Terraform backend configuration
## Environment-Specific Configuration ## Environment-Specific Configuration
@@ -110,3 +139,14 @@ Terraform state is automatically stored in Linode Object Storage under the confi
### Adding New Resources ### Adding New Resources
Add modules to `main.tf` and create corresponding directories under `infra/` following the established pattern. 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 # Backend module example configuration
# Configure this in your main terraform.tfvars or environment variables
# Linode Object Storage as Terraform backend environment = "staging"
# Copy this to your terraform.tfvars or set as environment variables linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key"
linode_token = "<YOUR_LINODE_API_TOKEN>" object_storage_secret_key = "your-secret-key"
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>"
+10 -38
View File
@@ -1,44 +1,16 @@
# Backend configuration for Terraform state in Linode Object Storage # Backend configuration for HAPI Terraform state
# This module provides the configuration needed for remote state storage
resource "linode_object_storage_bucket" "terraform_state" { terraform {
label = "hapi-terraform-state-${var.environment}" backend "s3" {
cluster = var.linode_object_storage_cluster # State stored in Object Storage
access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key
}
locals {
backend_config = {
bucket = "hapi-terraform-state-${var.environment}" bucket = "hapi-terraform-state-${var.environment}"
key = "terraform/${var.environment}/terraform.tfstate" key = "hapi/terraform.tfstate"
region = var.linode_region region = var.linode_object_storage_cluster
endpoint = "https://${var.linode_region}.linodeobjects.com" endpoint = "${var.linode_object_storage_cluster}.linodeobjects.com"
encrypt = true
access_key = var.object_storage_access_key access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key secret_key = var.object_storage_secret_key
skip_region_validation = true skip_region = true
} skip_credentials_validation = true
} skip_metadata_api_check = 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
} }
} }
+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 # Backend example configuration
linode_region = "us-east"
environment = "staging"
linode_object_storage_cluster = "us-east-1" linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key" object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key" object_storage_secret_key = "your-secret-key"
environment = "staging"
+16 -30
View File
@@ -1,42 +1,28 @@
# Variables for Backend module # Backend variables
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
}
variable "environment" { variable "environment" {
description = "Deployment environment (staging or production)" description = "Deployment environment (staging or production)"
type = string type = string
default = "staging"
validation { validation {
condition = contains(["staging", "production"], var.environment) condition = contains(["staging", "production"], var.environment)
error_message = "Environment must be 'staging' or 'production'." error_message = "Environment must be 'staging' or 'production'."
} }
} }
variable "tags" { variable "linode_object_storage_cluster" {
description = "Common tags for all resources" description = "Linode Object Storage cluster for backend state storage"
type = map(string) type = string
default = { default = "us-east-1"
ManagedBy = "terraform"
Project = "hapi"
} }
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 # Main Terraform configuration for HAPI infrastructure
# This file orchestrates all infrastructure components # 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 # Provider configurations are in providers.tf
# Modules # 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" { module "postgres" {
source = "./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" { module "storage" {
source = "./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" { module "network" {
source = "./network" source = "./network"
}
# Backend configuration for Terraform state linode_region = var.linode_region
# State will be stored in Linode Object Storage environment = var.environment
# Backend configuration is defined in infra/backend/ }
+62 -56
View File
@@ -1,75 +1,81 @@
# Networking module for HAPI # Networking module for HAPI
# Creates firewall rules for API, PostgreSQL, and Adminer # 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" { resource "linode_firewall" "hapi" {
label = "hapi-firewall" label = "hapi-firewall-${var.environment}"
tags = var.tags 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 rules
inbound = [ inbound {
{ action = "ACCEPT"
label = "allow-api" label = "allow-api-${var.environment}"
protocol = "tcp" protocol = "TCP"
ports = "8000" ports = "8000"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
}, ipv6 = ["::/0"]
{ }
label = "allow-postgres"
protocol = "tcp" inbound {
action = "ACCEPT"
label = "allow-postgres-${var.environment}"
protocol = "TCP"
ports = "5432" ports = "5432"
cidrs = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"] ipv4 = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
}, }
{
label = "allow-adminer" inbound {
protocol = "tcp" action = "ACCEPT"
label = "allow-adminer-${var.environment}"
protocol = "TCP"
ports = "8080" ports = "8080"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
}, ipv6 = ["::/0"]
{ }
label = "allow-https"
protocol = "tcp" inbound {
action = "ACCEPT"
label = "allow-https-${var.environment}"
protocol = "TCP"
ports = "443" ports = "443"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
}, ipv6 = ["::/0"]
{ }
label = "allow-http"
protocol = "tcp" inbound {
action = "ACCEPT"
label = "allow-http-${var.environment}"
protocol = "TCP"
ports = "80" ports = "80"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
} }
]
# Outbound rules # Outbound rules
outbound = [ outbound {
{ action = "ACCEPT"
label = "allow-all-outbound" label = "allow-all-outbound-${var.environment}"
protocol = "tcp" protocol = "TCP"
ports = "0-65535" ports = "0-65535"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/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"]
} }
# Firewall rule configurations # Firewall rule configurations
+12 -1
View File
@@ -1,3 +1,14 @@
# Network module variables # Network module example configuration
# Linode region for infrastructure
linode_region = "us-east" 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" 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" { variable "environment" {
description = "Deployment environment (staging or production)" description = "Deployment environment (staging or production)"
type = string type = string
+19 -4
View File
@@ -1,7 +1,7 @@
# Outputs for HAPI Terraform infrastructure # Outputs for HAPI Terraform infrastructure
output "postgres_endpoint" { output "postgres_endpoint" {
description = "PostgreSQL endpoint for the environment" description = "PostgreSQL endpoint for the environment (Kubernetes API endpoint)"
value = module.postgres.endpoint value = module.postgres.endpoint
} }
@@ -47,11 +47,26 @@ output "firewall_rules" {
value = module.network.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" { output "terraform_backend_config" {
description = "Configuration for Terraform backend in Object Storage" description = "Configuration for Terraform backend in Object Storage"
value = { value = {
bucket = module.storage.state_bucket_name bucket = module.backend.backend_bucket
key = "terraform/${var.environment}/terraform.tfstate" key = module.backend.backend_key
region = var.linode_region region = module.backend.backend_region
} }
} }
output "backend_endpoint" {
description = "Object Storage endpoint for backend state"
value = module.backend.backend_endpoint
}
+18 -5
View File
@@ -1,12 +1,25 @@
# PostgreSQL module for HAPI # PostgreSQL module for HAPI
# Creates separate database instances for staging and production # 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 # Object Storage buckets for PostgreSQL state
resource "linode_object_storage_bucket" "state" { resource "linode_object_storage_bucket" "state" {
for_each = toset(["staging", "production"]) for_each = toset(["staging", "production"])
label = "hapi-postgres-state-${each.key}" 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 access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key secret_key = var.object_storage_secret_key
} }
@@ -17,9 +30,9 @@ resource "linode_lke_cluster" "postgres" {
region = var.linode_region region = var.linode_region
label = "hapi-postgres-${each.key}" label = "hapi-postgres-${each.key}"
k8s_version = var.linode_lke_version 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 count = each.key == "production" ? 2 : 1
type = var.linode_node_type type = var.linode_node_type
} }
@@ -40,8 +53,8 @@ locals {
} }
output "endpoint" { output "endpoint" {
description = "PostgreSQL endpoint" description = "PostgreSQL endpoint (Kubernetes API endpoint)"
value = linode_lke_cluster.postgres[var.environment].hostname value = linode_lke_cluster.postgres[var.environment].api_endpoints[0]
} }
output "port" { output "port" {
+16 -1
View File
@@ -1,5 +1,20 @@
# Postgres module variables # PostgreSQL module example configuration
# Linode region for LKE cluster
linode_region = "us-east" linode_region = "us-east"
# LKE Kubernetes version
linode_lke_version = "1.30" linode_lke_version = "1.30"
# Node type for LKE pool
linode_node_type = "g6-standard-4" linode_node_type = "g6-standard-4"
# Object Storage cluster/region for state storage
linode_object_storage_cluster = "us-east-1" 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" 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 for HAPI Terraform infrastructure
# Provider configurations
# These use the sources defined in main.tf terraform block
provider "linode" { provider "linode" {
token = var.linode_token token = var.linode_token
region = var.linode_region
} }
provider "random" {} provider "random" {}
+15 -79
View File
@@ -1,97 +1,33 @@
# Object Storage module for HAPI # Object Storage module for HAPI
# Creates buckets for uploads and Terraform state # 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" { resource "linode_object_storage_bucket" "uploads" {
for_each = toset(["staging", "production"]) for_each = toset(["staging", "production"])
label = "hapi-uploads-${each.key}" label = "hapi-uploads-${each.key}"
cluster = var.linode_object_storage_cluster region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key secret_key = var.object_storage_secret_key
depends_on = [linode_object_storage_cluster.this]
} }
resource "linode_object_storage_bucket" "state" { resource "linode_object_storage_bucket" "state" {
for_each = toset(["staging", "production"]) for_each = toset(["staging", "production"])
label = "hapi-terraform-state-${each.key}" 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 access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_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" linode_region = "us-east"
# Object Storage cluster (v2 uses 'region' instead of 'cluster')
linode_object_storage_cluster = "us-east-1" linode_object_storage_cluster = "us-east-1"
# Object Storage credentials
object_storage_access_key = "your-access-key" object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key" object_storage_secret_key = "your-secret-key"
# Environment (affects bucket and firewall naming)
environment = "staging" 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" 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"
}