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
15 files changed
+240 -158

No files matched your search

+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..."
+15 -4
View File
@@ -1,9 +1,22 @@
# 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
}
@@ -38,7 +51,5 @@ output "backend_s3_config" {
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
}
+2 -1
View File
@@ -1,4 +1,5 @@
# Backend module variables
# Backend module example configuration
linode_region = "us-east"
linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key"
+41
View File
@@ -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
}
+64 -56
View File
@@ -1,75 +1,83 @@
# 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"
inbound {
action = "ACCEPT"
label = "allow-api-${var.environment}"
protocol = "TCP"
ports = "8000"
cidrs = ["0.0.0.0/0", "::/0"]
},
{
label = "allow-postgres"
protocol = "tcp"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
inbound {
action = "ACCEPT"
label = "allow-postgres-${var.environment}"
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"
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"
cidrs = ["0.0.0.0/0", "::/0"]
},
{
label = "allow-https"
protocol = "tcp"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
inbound {
action = "ACCEPT"
label = "allow-https-${var.environment}"
protocol = "TCP"
ports = "443"
cidrs = ["0.0.0.0/0", "::/0"]
},
{
label = "allow-http"
protocol = "tcp"
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
inbound {
action = "ACCEPT"
label = "allow-http-${var.environment}"
protocol = "TCP"
ports = "80"
cidrs = ["0.0.0.0/0", "::/0"]
ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
}
]
# Outbound rules
outbound = [
{
label = "allow-all-outbound"
protocol = "tcp"
outbound {
action = "ACCEPT"
label = "allow-all-outbound-${var.environment}"
protocol = "TCP"
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
+3 -1
View File
@@ -1,3 +1,5 @@
# Network module variables
# Network module example configuration
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" {
description = "Deployment environment (staging or production)"
type = string
+18 -5
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
}
@@ -17,9 +30,9 @@ resource "linode_lke_cluster" "postgres" {
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
}
@@ -40,8 +53,8 @@ locals {
}
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" {
+5 -1
View File
@@ -1,5 +1,9 @@
# Postgres module variables
# 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"
+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'."
}
}
+2 -4
View File
@@ -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" {}
+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}"
}
+2 -1
View File
@@ -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"
+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"
}