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
This commit is contained in:
Bobby committed 2026-11-17 22:09:31 -05:00
1 parent 341cd05ac5
commit 494f38c1f3
17 files changed
+285 -203

No files matched your search

+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..."
+4 -4
View File
@@ -4,10 +4,10 @@
# Linode Object Storage as Terraform backend # Linode Object Storage as Terraform backend
# Copy this to your terraform.tfvars or set as environment variables # Copy this to your terraform.tfvars or set as environment variables
linode_token = "<YOUR_LINODE_API_TOKEN>" linode_token = "<YOUR_LINODE_API_TOKEN>"
linode_region = "us-east" linode_region = "us-east"
environment = "staging" # or "production" environment = "staging" # or "production"
project_name = "hapi" project_name = "hapi"
# Object Storage credentials # Object Storage credentials
object_storage_access_key = "<YOUR_ACCESS_KEY>" object_storage_access_key = "<YOUR_ACCESS_KEY>"
+29 -18
View File
@@ -1,22 +1,35 @@
# Backend configuration for Terraform state in Linode Object Storage # Backend configuration for Terraform state in Linode Object Storage
# This module provides the configuration needed for remote state storage # This module provides the configuration needed for remote state storage
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
resource "linode_object_storage_bucket" "terraform_state" { resource "linode_object_storage_bucket" "terraform_state" {
label = "hapi-terraform-state-${var.environment}" label = "hapi-terraform-state-${var.environment}"
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
} }
locals { locals {
backend_config = { backend_config = {
bucket = "hapi-terraform-state-${var.environment}" bucket = "hapi-terraform-state-${var.environment}"
key = "terraform/${var.environment}/terraform.tfstate" key = "terraform/${var.environment}/terraform.tfstate"
region = var.linode_region region = var.linode_region
endpoint = "https://${var.linode_region}.linodeobjects.com" endpoint = "https://${var.linode_region}.linodeobjects.com"
encrypt = true 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_validation = true
} }
} }
@@ -29,16 +42,14 @@ output "backend_config" {
output "backend_s3_config" { output "backend_s3_config" {
description = "S3-style backend configuration for Terraform" description = "S3-style backend configuration for Terraform"
value = { value = {
bucket = local.backend_config.bucket bucket = local.backend_config.bucket
key = local.backend_config.key key = local.backend_config.key
region = local.backend_config.region region = local.backend_config.region
endpoint = local.backend_config.endpoint endpoint = local.backend_config.endpoint
encrypt = local.backend_config.encrypt encrypt = local.backend_config.encrypt
access_key = local.backend_config.access_key access_key = local.backend_config.access_key
secret_key = local.backend_config.secret_key secret_key = local.backend_config.secret_key
skip_region_validation = local.backend_config.skip_region_validation skip_region_validation = local.backend_config.skip_region_validation
} }
sensitive = { sensitive = true
secret_key = local.backend_config.secret_key
}
} }
+6 -5
View File
@@ -1,6 +1,7 @@
# Backend module variables # Backend module example configuration
linode_region = "us-east"
linode_region = "us-east"
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" environment = "staging"
+41
View File
@@ -1,21 +1,62 @@
# 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 "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"
linode_region = var.linode_region
environment = var.environment
} }
# Backend configuration for Terraform state # Backend configuration for Terraform state
# State will be stored in Linode Object Storage # State will be stored in Linode Object Storage
# Backend configuration is defined in infra/backend/ # Backend configuration is defined in infra/backend/
# Backend module (for terraform state storage configuration)
module "backend" {
source = "./backend"
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
}
+76 -68
View File
@@ -1,89 +1,97 @@
# 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}"])
# Inbound policy
inbound_policy = "DROP"
# Outbound policy
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 {
ports = "5432" action = "ACCEPT"
cidrs = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"] label = "allow-postgres-${var.environment}"
}, protocol = "TCP"
{ ports = "5432"
label = "allow-adminer" ipv4 = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
protocol = "tcp" }
ports = "8080"
cidrs = ["0.0.0.0/0", "::/0"] inbound {
}, action = "ACCEPT"
{ label = "allow-adminer-${var.environment}"
label = "allow-https" protocol = "TCP"
protocol = "tcp" ports = "8080"
ports = "443" ipv4 = ["0.0.0.0/0"]
cidrs = ["0.0.0.0/0", "::/0"] ipv6 = ["::/0"]
}, }
{
label = "allow-http" inbound {
protocol = "tcp" action = "ACCEPT"
ports = "80" label = "allow-https-${var.environment}"
cidrs = ["0.0.0.0/0", "::/0"] 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 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
locals { locals {
firewall_config = { firewall_config = {
staging = { staging = {
api_port = 8000 api_port = 8000
postgres_port = 5432 postgres_port = 5432
adminer_port = 8080 adminer_port = 8080
} }
production = { production = {
api_port = 8000 api_port = 8000
postgres_port = 5432 postgres_port = 5432
adminer_port = 8080 adminer_port = 8080
} }
} }
} }
+5 -3
View File
@@ -1,3 +1,5 @@
# Network module variables # Network module example configuration
linode_region = "us-east"
environment = "staging" linode_region = "us-east"
linode_object_storage_cluster = "us-east-1"
environment = "staging"
+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
+22 -9
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
} }
@@ -14,12 +27,12 @@ resource "linode_object_storage_bucket" "state" {
resource "linode_lke_cluster" "postgres" { resource "linode_lke_cluster" "postgres" {
for_each = toset(["staging", "production"]) for_each = toset(["staging", "production"])
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
} }
@@ -29,19 +42,19 @@ resource "linode_lke_cluster" "postgres" {
locals { locals {
databases = { databases = {
staging = { staging = {
name = "hapi_staging" name = "hapi_staging"
pool_size = 2 pool_size = 2
} }
production = { production = {
name = "hapi_production" name = "hapi_production"
pool_size = 3 pool_size = 3
} }
} }
} }
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" {
+2 -2
View File
@@ -4,6 +4,6 @@ resource "random_string" "password" {
length = 32 length = 32
upper = true upper = true
lower = true lower = true
numeric = true numeric = true
special = false special = false
} }
+8 -4
View File
@@ -1,5 +1,9 @@
# Postgres module variables # PostgreSQL module example configuration
linode_region = "us-east"
linode_lke_version = "1.30" linode_region = "us-east"
linode_node_type = "g6-standard-4" linode_lke_version = "1.30"
linode_node_type = "g6-standard-4"
linode_object_storage_cluster = "us-east-1" linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
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'."
}
}
+2 -4
View File
@@ -1,8 +1,6 @@
# Provider configurations for HAPI Terraform infrastructure # Provider configurations for HAPI Terraform infrastructure
provider "linode" { # Provider configurations
token = var.linode_token # These use the sources defined in main.tf terraform block
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}"
}
+2 -1
View File
@@ -1,4 +1,5 @@
# Storage module variables # Storage module example configuration
linode_region = "us-east" linode_region = "us-east"
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"
+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"
}