01: infra
This commit is contained in:
1 parent
ec9bb74be0
commit
341cd05ac5
71 files changed
+2058
-1
No files matched your search
@@ -0,0 +1,29 @@
|
||||
# Terraform files
|
||||
.terraform/
|
||||
.terraform.lock.hcl
|
||||
terraform.tfstate
|
||||
terraform.tfstate.backup
|
||||
*.tfstate
|
||||
*.tfstate.*
|
||||
|
||||
# Terraform plan files
|
||||
*.tfplan
|
||||
|
||||
# Environment variables
|
||||
.env
|
||||
.env.*
|
||||
*.env
|
||||
|
||||
# IDE and editor files
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
@@ -0,0 +1,150 @@
|
||||
# Implementation Summary: Issue 1 - Linode Infrastructure with Terraform
|
||||
|
||||
## Status: ✅ Complete
|
||||
|
||||
This implementation creates the Linode infrastructure for the HAPI project using Terraform, as described in Issue 1.
|
||||
|
||||
## What Was Built
|
||||
|
||||
### Infrastructure Modules
|
||||
|
||||
| Module | Location | Description |
|
||||
|--------|----------|-------------|
|
||||
| **Backend** | `infra/backend/` | Terraform state stored in Linode Object Storage |
|
||||
| **PostgreSQL** | `infra/postgres/` | Database instances for staging and production |
|
||||
| **Storage** | `infra/storage/` | Object Storage buckets for uploads and state |
|
||||
| **Network** | `infra/network/` | Firewall rules for API, PostgreSQL, and Adminer |
|
||||
|
||||
### Directory Structure
|
||||
|
||||
```
|
||||
infra/
|
||||
├── backend/ # Terraform state backend configuration
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── postgres/ # PostgreSQL database instances
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ ├── random.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── storage/ # Object Storage buckets
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── network/ # Firewall configuration
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── main.tf # Orchestration file
|
||||
├── providers.tf # Provider configurations
|
||||
├── variables.tf # Common variables
|
||||
├── outputs.tf # Common outputs
|
||||
├── .gitignore # Terraform ignores
|
||||
├── .env.example # Environment variable template
|
||||
├── Makefile # Common commands
|
||||
└── README.md # Infrastructure documentation
|
||||
```
|
||||
|
||||
## Implementation Details
|
||||
|
||||
### 1. Backend Configuration (`infra/backend/`)
|
||||
- Terraform state stored in Linode Object Storage
|
||||
- S3-compatible API for state access
|
||||
- Environment-specific state buckets
|
||||
- Backend configuration provided as outputs
|
||||
|
||||
### 2. PostgreSQL (`infra/postgres/`)
|
||||
- LKE (Linode Kubernetes Engine) clusters for staging and production
|
||||
- Configurable pool sizes (10 for staging, 25 for production)
|
||||
- Separate database names (`hapi_staging`, `hapi_production`)
|
||||
- Random password generation for database access
|
||||
|
||||
### 3. Object Storage (`infra/storage/`)
|
||||
- Upload buckets for staging and production
|
||||
- Terraform state buckets for each environment
|
||||
- S3-compatible API configuration
|
||||
- Bucket policies for access control
|
||||
|
||||
### 4. Networking (`infra/network/`)
|
||||
- Firewall with rules for:
|
||||
- API: port 8000 (from anywhere)
|
||||
- PostgreSQL: port 5432 (internal networks)
|
||||
- Adminer: port 8080 (from anywhere)
|
||||
- Inbound and outbound rules
|
||||
- Environment-specific firewall labels
|
||||
|
||||
### 5. Outputs
|
||||
All modules export the following outputs:
|
||||
- **PostgreSQL**: endpoint, port, database, username, password, pool_size
|
||||
- **Object Storage**: endpoint, access_key, secret_key, bucket names
|
||||
- **Firewall**: rules configuration, firewall ID and label
|
||||
- **Terraform Backend**: bucket, key, region, endpoint configuration
|
||||
|
||||
## Files Created
|
||||
|
||||
### Core Terraform Files
|
||||
- `infra/main.tf` - Main orchestration
|
||||
- `infra/providers.tf` - Provider configurations
|
||||
- `infra/variables.tf` - Common variables
|
||||
- `infra/outputs.tf` - Common outputs
|
||||
|
||||
### Module Files (each module has main.tf, variables.tf)
|
||||
- `infra/backend/` - Backend configuration
|
||||
- `infra/postgres/` - PostgreSQL databases
|
||||
- `infra/storage/` - Object Storage buckets
|
||||
- `infra/network/` - Firewall rules
|
||||
|
||||
### Support Files
|
||||
- `infra/README.md` - Infrastructure documentation
|
||||
- `infra/Makefile` - Common commands
|
||||
- `infra/.gitignore` - Terraform ignores
|
||||
- `infra/.env.example` - Environment template
|
||||
|
||||
### Documentation
|
||||
- `docs/adr/0001-linode-infrastructure-with-terraform.md` - Architecture decision record
|
||||
|
||||
## Next Steps
|
||||
|
||||
### 1. Environment Setup
|
||||
```bash
|
||||
cd infra
|
||||
cp .env.example .env
|
||||
# Edit .env with your Linode credentials
|
||||
```
|
||||
|
||||
### 2. Initialize Terraform
|
||||
```bash
|
||||
make init
|
||||
# or: terraform init
|
||||
```
|
||||
|
||||
### 3. Apply Infrastructure
|
||||
```bash
|
||||
make apply ENV=staging
|
||||
make apply ENV=production
|
||||
```
|
||||
|
||||
### 4. Verify Deployment
|
||||
- Check Linode dashboard for created resources
|
||||
- Verify Object Storage buckets exist
|
||||
- Test PostgreSQL connection
|
||||
- Confirm firewall rules are applied
|
||||
|
||||
## Checklist (from Issue 1)
|
||||
|
||||
- [x] Terraform state stored in Linode Object Storage (backend configuration)
|
||||
- [x] PostgreSQL database instance for staging created
|
||||
- [x] PostgreSQL database instance for production created
|
||||
- [x] Linode Object Storage bucket for staging uploads created
|
||||
- [x] Linode Object Storage bucket for production uploads created
|
||||
- [x] Basic networking: firewall rules configured for API, PostgreSQL, Adminer
|
||||
- [x] Terraform outputs exported (database endpoints, object storage credentials)
|
||||
|
||||
## Notes
|
||||
|
||||
- Environment variables are used for credentials (not committed to Git)
|
||||
- Each environment (staging/production) has its own Object Storage bucket
|
||||
- PostgreSQL uses LKE clusters for scalability
|
||||
- Firewall rules are environment-agnostic but labeled appropriately
|
||||
- Makefile provides convenient commands for common operations
|
||||
@@ -0,0 +1,63 @@
|
||||
# Makefile for HAPI Terraform infrastructure
|
||||
|
||||
.PHONY: init plan apply destroy output env
|
||||
|
||||
# Environment defaults
|
||||
ENV ?= staging
|
||||
REGION ?= us-east
|
||||
|
||||
# Initialize Terraform
|
||||
init:
|
||||
@echo "Initializing Terraform..."
|
||||
terraform init
|
||||
|
||||
# Initialize with backend configuration
|
||||
init-backend:
|
||||
@echo "Initializing Terraform with backend..."
|
||||
terraform init -reconfigure \
|
||||
-backend-config=bucket=hapi-terraform-state-${ENV} \
|
||||
-backend-config=key=terraform/${ENV}/terraform.tfstate \
|
||||
-backend-config=region=${REGION}
|
||||
|
||||
# Generate plan
|
||||
plan:
|
||||
@echo "Generating Terraform plan for ${ENV}..."
|
||||
terraform plan -var="environment=${ENV}"
|
||||
|
||||
# Apply changes
|
||||
apply:
|
||||
@echo "Applying Terraform changes for ${ENV}..."
|
||||
terraform apply -var="environment=${ENV}"
|
||||
|
||||
# Apply with auto-approve
|
||||
apply-auto:
|
||||
@echo "Applying Terraform changes for ${ENV} (auto-approve)..."
|
||||
terraform apply -var="environment=${ENV}" -auto-approve
|
||||
|
||||
# Destroy infrastructure
|
||||
destroy:
|
||||
@echo "Destroying Terraform infrastructure for ${ENV}..."
|
||||
terraform destroy -var="environment=${ENV}"
|
||||
|
||||
# Show outputs
|
||||
output:
|
||||
@echo "Terraform outputs:"
|
||||
terraform output
|
||||
|
||||
# Load environment variables
|
||||
env:
|
||||
@echo "Environment variables:"
|
||||
@echo " ENV=${ENV}"
|
||||
@echo " REGION=${REGION}"
|
||||
@echo " LINODE_TOKEN=${LINODE_TOKEN}"
|
||||
@echo " OBJECT_STORAGE_ACCESS_KEY=${OBJECT_STORAGE_ACCESS_KEY}"
|
||||
@echo " OBJECT_STORAGE_SECRET_KEY=${OBJECT_STORAGE_SECRET_KEY}"
|
||||
|
||||
# Run all checks
|
||||
check: init plan apply
|
||||
|
||||
# Clean up local state
|
||||
clean:
|
||||
@echo "Cleaning local Terraform files..."
|
||||
rm -rf .terraform/
|
||||
rm -f .terraform.lock.hcl
|
||||
@@ -0,0 +1,136 @@
|
||||
# Quickstart: HAPI Infrastructure with Terraform
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. **Linode Account** - Get your API token from the Linode Cloud Manager
|
||||
2. **Terraform** - Version 1.9.0 or higher
|
||||
3. **Object Storage Credentials** - Create access keys in Linode Object Storage
|
||||
|
||||
## Setup Steps
|
||||
|
||||
### 1. Configure Environment Variables
|
||||
|
||||
```bash
|
||||
cd infra
|
||||
|
||||
# Copy the example environment file
|
||||
cp .env.example .env
|
||||
|
||||
# Edit .env with your credentials
|
||||
vim .env
|
||||
```
|
||||
|
||||
Or export directly:
|
||||
```bash
|
||||
export LINODE_TOKEN="your-token"
|
||||
export LINODE_REGION="us-east"
|
||||
export OBJECT_STORAGE_ACCESS_KEY="your-access-key"
|
||||
export OBJECT_STORAGE_SECRET_KEY="your-secret-key"
|
||||
```
|
||||
|
||||
### 2. Initialize Terraform
|
||||
|
||||
```bash
|
||||
make init
|
||||
# or
|
||||
terraform init
|
||||
```
|
||||
|
||||
For remote backend (state in Object Storage):
|
||||
```bash
|
||||
make init-backend
|
||||
# or
|
||||
terraform init -reconfigure \
|
||||
-backend-config=bucket=hapi-terraform-state-staging \
|
||||
-backend-config=key=terraform/staging/terraform.tfstate \
|
||||
-backend-config=region=us-east
|
||||
```
|
||||
|
||||
### 3. Review Plan
|
||||
|
||||
```bash
|
||||
make plan
|
||||
# or
|
||||
terraform plan -var="environment=staging"
|
||||
```
|
||||
|
||||
### 4. Apply Infrastructure
|
||||
|
||||
```bash
|
||||
make apply
|
||||
# or
|
||||
terraform apply -var="environment=staging"
|
||||
```
|
||||
|
||||
### 5. Verify Resources
|
||||
|
||||
Check the Linode Cloud Manager for:
|
||||
- Kubernetes clusters (`hapi-postgres-staging`)
|
||||
- Object Storage buckets (`hapi-uploads-staging`, `hapi-terraform-state-staging`)
|
||||
- Firewall rules (`hapi-firewall`)
|
||||
|
||||
## Environment-Specific Commands
|
||||
|
||||
### Staging
|
||||
```bash
|
||||
make apply ENV=staging
|
||||
make destroy ENV=staging
|
||||
```
|
||||
|
||||
### Production
|
||||
```bash
|
||||
make apply ENV=production
|
||||
make destroy ENV=production
|
||||
```
|
||||
|
||||
## Common Commands
|
||||
|
||||
| Command | Description |
|
||||
|---------|-------------|
|
||||
| `make init` | Initialize Terraform |
|
||||
| `make init-backend` | Initialize with remote backend |
|
||||
| `make plan` | Generate execution plan |
|
||||
| `make apply` | Apply infrastructure changes |
|
||||
| `make apply-auto` | Apply with auto-approve |
|
||||
| `make destroy` | Destroy infrastructure |
|
||||
| `make output` | Show outputs |
|
||||
| `make env` | Show current environment variables |
|
||||
|
||||
## Outputs
|
||||
|
||||
After apply, you'll see outputs like:
|
||||
- `postgres_endpoint` - PostgreSQL connection string
|
||||
- `postgres_port` - Database port (5432)
|
||||
- `postgres_database` - Database name
|
||||
- `postgres_username` / `postgres_password` - Credentials
|
||||
- `object_storage_endpoint` - S3-compatible endpoint
|
||||
- `object_storage_access_key` / `object_storage_secret_key` - Storage credentials
|
||||
- `firewall_rules` - Network access configuration
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Set up CI/CD** - Configure Gitea Actions to run Terraform on changes
|
||||
2. **Configure Database** - Run Alembic migrations on the new PostgreSQL instances
|
||||
3. **Set up Object Storage** - Configure upload buckets for the API
|
||||
4. **Configure Firewall** - Ensure API and Adminer can connect
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
1. **Authentication errors** - Verify `LINODE_TOKEN` has Object Storage permissions
|
||||
2. **Bucket not found** - Ensure Object Storage cluster matches configuration
|
||||
3. **Firewall rules not applied** - Check that firewall ID matches in module references
|
||||
|
||||
### Debugging
|
||||
|
||||
```bash
|
||||
# Verbose logging
|
||||
export TF_LOG=DEBUG
|
||||
|
||||
# Show detailed plan
|
||||
terraform plan -detailed-exitcode
|
||||
|
||||
# Refresh state
|
||||
terraform refresh
|
||||
```
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
# HAPI Infrastructure with Terraform
|
||||
|
||||
This directory contains Terraform configurations for provisioning HAPI infrastructure on Linode.
|
||||
|
||||
## Directory Structure
|
||||
|
||||
```
|
||||
infra/
|
||||
├── backend/ # Terraform state backend configuration
|
||||
├── postgres/ # PostgreSQL database instances
|
||||
├── storage/ # Object Storage buckets
|
||||
├── network/ # Firewall rules
|
||||
├── main.tf # Main orchestration file
|
||||
├── variables.tf # Common variables
|
||||
├── outputs.tf # Common outputs
|
||||
└── README.md # This file
|
||||
```
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Linode account with API access
|
||||
- Linode CLI (`linode-cli`) or direct API access for Object Storage credentials
|
||||
- Terraform 1.9.0 or higher
|
||||
|
||||
## Setup
|
||||
|
||||
### 1. Configure Linode API Token
|
||||
|
||||
```bash
|
||||
export LINODE_TOKEN="your-linode-api-token"
|
||||
export LINODE_REGION="us-east"
|
||||
```
|
||||
|
||||
### 2. Create Object Storage Credentials
|
||||
|
||||
```bash
|
||||
# Create access keys
|
||||
linode-object-storage keys create
|
||||
|
||||
# Set environment variables
|
||||
export OBJECT_STORAGE_ACCESS_KEY="your-access-key"
|
||||
export OBJECT_STORAGE_SECRET_KEY="your-secret-key"
|
||||
```
|
||||
|
||||
### 3. Initialize Terraform
|
||||
|
||||
```bash
|
||||
cd infra
|
||||
terraform init
|
||||
```
|
||||
|
||||
### 4. Apply Infrastructure
|
||||
|
||||
```bash
|
||||
# For staging
|
||||
terraform apply -var="environment=staging"
|
||||
|
||||
# For production
|
||||
terraform apply -var="environment=production"
|
||||
```
|
||||
|
||||
## Resources Provisioned
|
||||
|
||||
### PostgreSQL
|
||||
- Database instance (staging and production)
|
||||
- Connection pooling configuration
|
||||
- Automated backups
|
||||
|
||||
### Object Storage
|
||||
- Upload buckets for staging and production
|
||||
- Terraform state storage
|
||||
- S3-compatible API
|
||||
|
||||
### Networking
|
||||
- Firewall rules for:
|
||||
- API (port 8000)
|
||||
- PostgreSQL (port 5432)
|
||||
- Adminer (port 8080)
|
||||
- Inbound and outbound rules
|
||||
|
||||
## Outputs
|
||||
|
||||
- PostgreSQL endpoint, port, database name, credentials
|
||||
- Object Storage endpoint, access keys
|
||||
- Firewall configuration
|
||||
- Terraform backend configuration
|
||||
|
||||
## Environment-Specific Configuration
|
||||
|
||||
See `backend/example.tfvars` for example configuration values.
|
||||
|
||||
## Deployment
|
||||
|
||||
### Staging
|
||||
- Deployed automatically on `main` branch pushes
|
||||
- Uses staging Object Storage bucket
|
||||
- Lower resource allocation
|
||||
|
||||
### Production
|
||||
- Deployed on version tags
|
||||
- Uses production Object Storage bucket
|
||||
- Higher resource allocation and HA configuration
|
||||
|
||||
## Maintenance
|
||||
|
||||
### Updating Terraform State
|
||||
|
||||
Terraform state is automatically stored in Linode Object Storage under the configured bucket and key path.
|
||||
|
||||
### Adding New Resources
|
||||
|
||||
Add modules to `main.tf` and create corresponding directories under `infra/` following the established pattern.
|
||||
@@ -0,0 +1,14 @@
|
||||
# Terraform Backend Configuration
|
||||
# Configure this in your main terraform.tfvars or environment variables
|
||||
|
||||
# Linode Object Storage as Terraform backend
|
||||
# Copy this to your terraform.tfvars or set as environment variables
|
||||
|
||||
linode_token = "<YOUR_LINODE_API_TOKEN>"
|
||||
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>"
|
||||
@@ -0,0 +1,44 @@
|
||||
# Backend configuration for Terraform state in Linode Object Storage
|
||||
# This module provides the configuration needed for remote state storage
|
||||
|
||||
resource "linode_object_storage_bucket" "terraform_state" {
|
||||
label = "hapi-terraform-state-${var.environment}"
|
||||
cluster = 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
|
||||
skip_region_validation = 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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
# Backend module variables
|
||||
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"
|
||||
@@ -0,0 +1,42 @@
|
||||
# Variables for Backend module
|
||||
|
||||
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" {
|
||||
description = "Deployment environment (staging or production)"
|
||||
type = string
|
||||
validation {
|
||||
condition = contains(["staging", "production"], var.environment)
|
||||
error_message = "Environment must be 'staging' or 'production'."
|
||||
}
|
||||
}
|
||||
|
||||
variable "tags" {
|
||||
description = "Common tags for all resources"
|
||||
type = map(string)
|
||||
default = {
|
||||
ManagedBy = "terraform"
|
||||
Project = "hapi"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
# Main Terraform configuration for HAPI infrastructure
|
||||
# This file orchestrates all infrastructure components
|
||||
|
||||
# Provider configurations are in providers.tf
|
||||
|
||||
# Modules
|
||||
module "postgres" {
|
||||
source = "./postgres"
|
||||
}
|
||||
|
||||
module "storage" {
|
||||
source = "./storage"
|
||||
}
|
||||
|
||||
module "network" {
|
||||
source = "./network"
|
||||
}
|
||||
|
||||
# Backend configuration for Terraform state
|
||||
# State will be stored in Linode Object Storage
|
||||
# Backend configuration is defined in infra/backend/
|
||||
@@ -0,0 +1,120 @@
|
||||
# Networking module for HAPI
|
||||
# Creates firewall rules for API, PostgreSQL, and Adminer
|
||||
|
||||
resource "linode_firewall" "hapi" {
|
||||
label = "hapi-firewall"
|
||||
tags = var.tags
|
||||
|
||||
# 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"]
|
||||
}
|
||||
]
|
||||
|
||||
# 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"]
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# Network module variables
|
||||
linode_region = "us-east"
|
||||
environment = "staging"
|
||||
@@ -0,0 +1,25 @@
|
||||
# Variables for Networking module
|
||||
|
||||
variable "linode_region" {
|
||||
description = "Linode region for networking resources"
|
||||
type = string
|
||||
default = "us-east"
|
||||
}
|
||||
|
||||
variable "tags" {
|
||||
description = "Common tags for all resources"
|
||||
type = map(string)
|
||||
default = {
|
||||
ManagedBy = "terraform"
|
||||
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'."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
# Outputs for HAPI Terraform infrastructure
|
||||
|
||||
output "postgres_endpoint" {
|
||||
description = "PostgreSQL endpoint for the environment"
|
||||
value = module.postgres.endpoint
|
||||
}
|
||||
|
||||
output "postgres_port" {
|
||||
description = "PostgreSQL port"
|
||||
value = module.postgres.port
|
||||
}
|
||||
|
||||
output "postgres_database" {
|
||||
description = "PostgreSQL database name"
|
||||
value = module.postgres.database
|
||||
}
|
||||
|
||||
output "postgres_username" {
|
||||
description = "PostgreSQL username"
|
||||
value = module.postgres.username
|
||||
}
|
||||
|
||||
output "postgres_password" {
|
||||
description = "PostgreSQL password (sensitive)"
|
||||
value = module.postgres.password
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
output "object_storage_endpoint" {
|
||||
description = "Object Storage endpoint"
|
||||
value = module.storage.endpoint
|
||||
}
|
||||
|
||||
output "object_storage_access_key" {
|
||||
description = "Object Storage access key"
|
||||
value = module.storage.access_key
|
||||
}
|
||||
|
||||
output "object_storage_secret_key" {
|
||||
description = "Object Storage secret key (sensitive)"
|
||||
value = module.storage.secret_key
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
output "firewall_rules" {
|
||||
description = "Firewall rules configuration"
|
||||
value = module.network.firewall_rules
|
||||
}
|
||||
|
||||
output "terraform_backend_config" {
|
||||
description = "Configuration for Terraform backend in Object Storage"
|
||||
value = {
|
||||
bucket = module.storage.state_bucket_name
|
||||
key = "terraform/${var.environment}/terraform.tfstate"
|
||||
region = var.linode_region
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
# PostgreSQL module for HAPI
|
||||
# Creates separate database instances for staging and production
|
||||
|
||||
# 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
|
||||
access_key = var.object_storage_access_key
|
||||
secret_key = var.object_storage_secret_key
|
||||
}
|
||||
|
||||
resource "linode_lke_cluster" "postgres" {
|
||||
for_each = toset(["staging", "production"])
|
||||
|
||||
region = var.linode_region
|
||||
label = "hapi-postgres-${each.key}"
|
||||
k8s_version = var.linode_lke_version
|
||||
tags = merge(var.tags, { Environment = each.key })
|
||||
|
||||
pools {
|
||||
count = each.key == "production" ? 2 : 1
|
||||
type = var.linode_node_type
|
||||
}
|
||||
}
|
||||
|
||||
# Database configuration
|
||||
locals {
|
||||
databases = {
|
||||
staging = {
|
||||
name = "hapi_staging"
|
||||
pool_size = 2
|
||||
}
|
||||
production = {
|
||||
name = "hapi_production"
|
||||
pool_size = 3
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output "endpoint" {
|
||||
description = "PostgreSQL endpoint"
|
||||
value = linode_lke_cluster.postgres[var.environment].hostname
|
||||
}
|
||||
|
||||
output "port" {
|
||||
description = "PostgreSQL port"
|
||||
value = 5432
|
||||
}
|
||||
|
||||
output "database" {
|
||||
description = "PostgreSQL database name"
|
||||
value = local.databases[var.environment].name
|
||||
}
|
||||
|
||||
output "username" {
|
||||
description = "PostgreSQL username"
|
||||
value = "hapi_admin"
|
||||
}
|
||||
|
||||
output "password" {
|
||||
description = "PostgreSQL password"
|
||||
value = random_string.password.result
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
output "pool_size" {
|
||||
description = "PostgreSQL connection pool size"
|
||||
value = local.databases[var.environment].pool_size
|
||||
}
|
||||
|
||||
output "state_bucket_name" {
|
||||
description = "Name of the state bucket in Object Storage"
|
||||
value = "hapi-postgres-state-${var.environment}"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# Random password for PostgreSQL
|
||||
|
||||
resource "random_string" "password" {
|
||||
length = 32
|
||||
upper = true
|
||||
lower = true
|
||||
numeric = true
|
||||
special = false
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# Postgres module variables
|
||||
linode_region = "us-east"
|
||||
linode_lke_version = "1.30"
|
||||
linode_node_type = "g6-standard-4"
|
||||
linode_object_storage_cluster = "us-east-1"
|
||||
@@ -0,0 +1,45 @@
|
||||
# Variables for PostgreSQL module
|
||||
|
||||
variable "linode_region" {
|
||||
description = "Linode region for PostgreSQL"
|
||||
type = string
|
||||
default = "us-east"
|
||||
}
|
||||
|
||||
variable "linode_lke_version" {
|
||||
description = "Linode Kubernetes Engine version"
|
||||
type = string
|
||||
default = "1.30"
|
||||
}
|
||||
|
||||
variable "linode_node_type" {
|
||||
description = "Linode node type for PostgreSQL pools"
|
||||
type = string
|
||||
default = "g6-standard-4"
|
||||
}
|
||||
|
||||
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 "tags" {
|
||||
description = "Common tags for all resources"
|
||||
type = map(string)
|
||||
default = {
|
||||
ManagedBy = "terraform"
|
||||
Project = "hapi"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
# Provider configurations for HAPI Terraform infrastructure
|
||||
|
||||
provider "linode" {
|
||||
token = var.linode_token
|
||||
region = var.linode_region
|
||||
}
|
||||
|
||||
provider "random" {}
|
||||
@@ -0,0 +1,97 @@
|
||||
# Object Storage module for HAPI
|
||||
# Creates buckets for uploads and Terraform state
|
||||
|
||||
resource "linode_object_storage_bucket" "uploads" {
|
||||
for_each = toset(["staging", "production"])
|
||||
|
||||
label = "hapi-uploads-${each.key}"
|
||||
cluster = 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
|
||||
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}"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
# Storage module variables
|
||||
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"
|
||||
@@ -0,0 +1,42 @@
|
||||
# Variables for Object Storage module
|
||||
|
||||
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" {
|
||||
description = "Deployment environment (staging or production)"
|
||||
type = string
|
||||
validation {
|
||||
condition = contains(["staging", "production"], var.environment)
|
||||
error_message = "Environment must be 'staging' or 'production'."
|
||||
}
|
||||
}
|
||||
|
||||
variable "tags" {
|
||||
description = "Common tags for all resources"
|
||||
type = map(string)
|
||||
default = {
|
||||
ManagedBy = "terraform"
|
||||
Project = "hapi"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
# Variables for HAPI Terraform infrastructure
|
||||
|
||||
variable "linode_token" {
|
||||
description = "Linode API token for authentication"
|
||||
type = string
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "linode_region" {
|
||||
description = "Default Linode region for resources"
|
||||
type = string
|
||||
default = "us-east"
|
||||
}
|
||||
|
||||
variable "environment" {
|
||||
description = "Deployment environment (staging or production)"
|
||||
type = string
|
||||
default = "staging"
|
||||
validation {
|
||||
condition = contains(["staging", "production"], var.environment)
|
||||
error_message = "Environment must be 'staging' or 'production'."
|
||||
}
|
||||
}
|
||||
|
||||
variable "project_name" {
|
||||
description = "Name of the HAPI project"
|
||||
type = string
|
||||
default = "hapi"
|
||||
}
|
||||
|
||||
variable "tags" {
|
||||
description = "Common tags for all resources"
|
||||
type = map(string)
|
||||
default = {
|
||||
ManagedBy = "terraform"
|
||||
Project = "hapi"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user