# 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-${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 { 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 { 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 postgres_port = 5432 adminer_port = 8080 } production = { api_port = 8000 postgres_port = 5432 adminer_port = 8080 } } } output "firewall_rules" { description = "Firewall rules configuration" value = { api = { port = 8000 protocol = "tcp" cidrs = ["0.0.0.0/0", "::/0"] } postgres = { port = 5432 protocol = "tcp" cidrs = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"] } adminer = { port = 8080 protocol = "tcp" cidrs = ["0.0.0.0/0", "::/0"] } } } output "firewall_id" { description = "ID of the main firewall" value = linode_firewall.hapi.id } output "firewall_label" { description = "Label of the main firewall" value = linode_firewall.hapi.label }