diff --git a/infra/Makefile b/infra/Makefile index 062d1d5..b0c6b72 100644 --- a/infra/Makefile +++ b/infra/Makefile @@ -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..." diff --git a/infra/backend/example.tfvars b/infra/backend/example.tfvars index 51f9b69..ee9a620 100644 --- a/infra/backend/example.tfvars +++ b/infra/backend/example.tfvars @@ -4,10 +4,10 @@ # Linode Object Storage as Terraform backend # Copy this to your terraform.tfvars or set as environment variables -linode_token = "" -linode_region = "us-east" -environment = "staging" # or "production" -project_name = "hapi" +linode_token = "" +linode_region = "us-east" +environment = "staging" # or "production" +project_name = "hapi" # Object Storage credentials object_storage_access_key = "" diff --git a/infra/backend/main.tf b/infra/backend/main.tf index 6d21788..297e8f9 100644 --- a/infra/backend/main.tf +++ b/infra/backend/main.tf @@ -1,22 +1,35 @@ # Backend configuration for Terraform state in Linode Object 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" { 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 secret_key = var.object_storage_secret_key } locals { backend_config = { - 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 - access_key = var.object_storage_access_key - secret_key = var.object_storage_secret_key + 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 + access_key = var.object_storage_access_key + secret_key = var.object_storage_secret_key skip_region_validation = true } } @@ -29,16 +42,14 @@ output "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 + 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 - } + sensitive = true } diff --git a/infra/backend/terraform.tfvars.example b/infra/backend/terraform.tfvars.example index 9a8065e..25f654b 100644 --- a/infra/backend/terraform.tfvars.example +++ b/infra/backend/terraform.tfvars.example @@ -1,6 +1,7 @@ -# Backend module variables -linode_region = "us-east" +# Backend module example configuration + +linode_region = "us-east" linode_object_storage_cluster = "us-east-1" -object_storage_access_key = "your-access-key" -object_storage_secret_key = "your-secret-key" -environment = "staging" +object_storage_access_key = "your-access-key" +object_storage_secret_key = "your-secret-key" +environment = "staging" diff --git a/infra/main.tf b/infra/main.tf index c7d9886..547cbc6 100644 --- a/infra/main.tf +++ b/infra/main.tf @@ -1,21 +1,62 @@ # 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 "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" + + linode_region = var.linode_region + environment = var.environment } # Backend configuration for Terraform state # State will be stored in Linode Object Storage # 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 +} diff --git a/infra/network/main.tf b/infra/network/main.tf index 2fc9581..6275025 100644 --- a/infra/network/main.tf +++ b/infra/network/main.tf @@ -1,89 +1,97 @@ # 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}"]) + + # Inbound policy + inbound_policy = "DROP" + + # Outbound policy + 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 } } } diff --git a/infra/network/terraform.tfvars.example b/infra/network/terraform.tfvars.example index f3ec4f2..3af7fed 100644 --- a/infra/network/terraform.tfvars.example +++ b/infra/network/terraform.tfvars.example @@ -1,3 +1,5 @@ -# Network module variables -linode_region = "us-east" -environment = "staging" +# Network module example configuration + +linode_region = "us-east" +linode_object_storage_cluster = "us-east-1" +environment = "staging" diff --git a/infra/network/variables.tf b/infra/network/variables.tf index 49c8cf4..8b7663c 100644 --- a/infra/network/variables.tf +++ b/infra/network/variables.tf @@ -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 diff --git a/infra/postgres/main.tf b/infra/postgres/main.tf index 1017fc4..30b7051 100644 --- a/infra/postgres/main.tf +++ b/infra/postgres/main.tf @@ -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" { diff --git a/infra/postgres/random.tf b/infra/postgres/random.tf index c644b89..ecd2cb8 100644 --- a/infra/postgres/random.tf +++ b/infra/postgres/random.tf @@ -4,6 +4,6 @@ resource "random_string" "password" { length = 32 upper = true lower = true - numeric = true - special = false + numeric = true + special = false } diff --git a/infra/postgres/terraform.tfvars.example b/infra/postgres/terraform.tfvars.example index d2e4231..82f7c73 100644 --- a/infra/postgres/terraform.tfvars.example +++ b/infra/postgres/terraform.tfvars.example @@ -1,5 +1,9 @@ -# Postgres module variables -linode_region = "us-east" -linode_lke_version = "1.30" -linode_node_type = "g6-standard-4" +# PostgreSQL module example configuration + +linode_region = "us-east" +linode_lke_version = "1.30" +linode_node_type = "g6-standard-4" linode_object_storage_cluster = "us-east-1" +object_storage_access_key = "your-access-key" +object_storage_secret_key = "your-secret-key" +environment = "staging" diff --git a/infra/postgres/variables.tf b/infra/postgres/variables.tf index f87afcf..6092882 100644 --- a/infra/postgres/variables.tf +++ b/infra/postgres/variables.tf @@ -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'." + } +} diff --git a/infra/providers.tf b/infra/providers.tf index 7b3ffd8..3b7d75b 100644 --- a/infra/providers.tf +++ b/infra/providers.tf @@ -1,8 +1,6 @@ # Provider configurations for HAPI Terraform infrastructure -provider "linode" { - token = var.linode_token - region = var.linode_region -} +# Provider configurations +# These use the sources defined in main.tf terraform block provider "random" {} diff --git a/infra/storage/main.tf b/infra/storage/main.tf index ad22424..ec909ad 100644 --- a/infra/storage/main.tf +++ b/infra/storage/main.tf @@ -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}" } diff --git a/infra/storage/outputs.tf b/infra/storage/outputs.tf new file mode 100644 index 0000000..72668ed --- /dev/null +++ b/infra/storage/outputs.tf @@ -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}" +} diff --git a/infra/storage/terraform.tfvars.example b/infra/storage/terraform.tfvars.example index c303ff7..d389fe4 100644 --- a/infra/storage/terraform.tfvars.example +++ b/infra/storage/terraform.tfvars.example @@ -1,4 +1,5 @@ -# Storage module variables +# Storage module example configuration + linode_region = "us-east" linode_object_storage_cluster = "us-east-1" object_storage_access_key = "your-access-key" diff --git a/infra/variables.tf b/infra/variables.tf index 17593b3..ada8ae1 100644 --- a/infra/variables.tf +++ b/infra/variables.tf @@ -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" +}