Files
code-tools/tf

Terraform Coder Development Environment

A comprehensive development environment deployment using Terraform and Coder, providing isolated workspaces with integrated development tools, databases, and AI-powered coding assistants.

🏗️ Architecture Overview

This configuration deploys self-contained development workspaces using Docker containers orchestrated by Coder. Each workspace includes:

  • Isolated Development Container with VS Code, terminal access, and full development toolchain
  • Database Services (PostgreSQL, Redis, Qdrant) with persistent storage
  • Management Interfaces (pgAdmin, Qdrant Dashboard)
  • AI Development Tools (Claude Code, Cursor, Windsurf support)
  • Reverse Proxy Integration for seamless web access

Network Architecture

┌─────────────────────────────────────────────────────────────┐
│                    Reverse Proxy                            │
│                  http://dev.lab                            │
│                 *.dev.lab wildcard                         │
└─────────────────────────┬───────────────────────────────────┘
                          │
    ┌─────────────────────┼─────────────────────┐
    │     Workspace A     │     Workspace B     │
    │  network-{id-a}     │  network-{id-b}     │
    │                     │                     │
    │ ┌─────────────────┐ │ ┌─────────────────┐ │
    │ │   Dev Container │ │ │   Dev Container │ │
    │ │   VS Code:8080  │ │ │   VS Code:8080  │ │
    │ └─────────────────┘ │ └─────────────────┘ │
    │                     │                     │
    │ ┌─────────────────┐ │ ┌─────────────────┐ │
    │ │ PostgreSQL:5432 │ │ │ PostgreSQL:5432 │ │
    │ │   Redis:6379    │ │ │   Redis:6379    │ │
    │ │  Qdrant:6333    │ │ │  Qdrant:6333    │ │
    │ │ pgAdmin:5050    │ │ │ pgAdmin:5050    │ │
    │ └─────────────────┘ │ └─────────────────┘ │
    └─────────────────────┼─────────────────────┘

Key Benefits:

  • Complete Network Isolation between workspaces
  • No Port Conflicts - same ports used in different networks
  • Scalable - unlimited concurrent workspaces
  • Secure - services only accessible through authenticated Coder session

📁 File Structure

tf/
├── README.md              # This file
├── main.tf                # Provider config, parameters, networks, volumes
├── variables.tf           # All configurable parameters
├── terraform.tfvars       # Variable assignments
├── workspace.tf           # Main development container and Coder agent
├── services.tf            # Database containers (PostgreSQL, Redis, Qdrant)
├── apps.tf                # Coder applications for service access
├── scripts.tf             # AI tools installation and configuration
└── outputs.tf             # Workspace information exports

🛠️ Included Services & Tools

Development Environment

  • Container Base: Microsoft DevContainers Universal image
  • Languages: Node.js 20, Python 3.12, Rust (latest stable)
  • Package Managers: npm, uv (Python), Cargo (Rust)
  • System Tools: make, tree, jq, curl, wget, build-essential

Database Services

  • PostgreSQL 17 with Alpine Linux base
    • Connection pooling and performance optimization
    • pg_stat_statements enabled for query analysis
    • PostgreSQL client tools included in workspace
  • Redis 7 with Alpine Linux base
    • Authentication enabled with configurable password
    • AOF persistence with everysec fsync
    • LRU eviction policy with configurable memory limits
  • Qdrant Vector Database (latest)
    • HTTP API on port 6333, gRPC on port 6334
    • Persistent storage for vector collections
    • Web dashboard for collection management

Management Interfaces

  • pgAdmin 4 - PostgreSQL administration interface
  • Qdrant Dashboard - Vector database management
  • VS Code Server - Browser-based IDE
  • Terminal Access - Full bash shell access

AI Development Tools

  • Claude Code CLI - Anthropic's official CLI
  • Cursor Support - AI-powered code editor integration
  • Windsurf Support - Codeium's development environment

Development Packages

Node.js (Global)

repomix, create-next-app, nodemon, concurrently
@types/node, typescript, eslint, prettier

Python (via uv)

fastapi, uvicorn, requests, pandas, numpy
psycopg2-binary, redis, qdrant-client, python-dotenv

🚀 Quick Start

Prerequisites

  1. Coder Instance running and accessible
  2. Reverse Proxy configured with wildcard subdomain support
  3. Docker daemon accessible to Coder
  4. Terraform >= 1.0 installed

Environment Setup

Set these environment variables in your Coder deployment:

CODER_ACCESS_URL=http://dev.lab
CODER_WILDCARD_ACCESS_URL=*.dev.lab

Deployment Steps

  1. Clone and Navigate

    git clone <your-repo>
    cd tf/
    
  2. Review Configuration

    # Edit terraform.tfvars to match your needs
    vim terraform.tfvars
    
  3. Deploy Infrastructure

    terraform init
    terraform plan
    terraform apply
    
  4. Access Workspace

    • Navigate to your Coder instance
    • Create new workspace using this template
    • Select your preferred Git repository
    • Choose whether to enable database services

⚙️ Configuration

Key Variables (terraform.tfvars)

Resource Limits

workspace_memory_limit = 16384  # 16GB RAM
workspace_cpu_limit    = 4      # 4 CPU cores

Service Configuration

# Database passwords (change in production!)
postgres_password = "devpassword"
redis_password    = "devpassword"

# Database tuning
postgres_max_connections = 100
redis_max_memory        = "512mb"

Feature Toggles

enable_pgadmin    = true   # PostgreSQL admin interface
enable_monitoring = true   # Resource monitoring
enable_jupyter    = false  # Jupyter Lab for data science

Tool Versions

node_version     = "20"      # Node.js LTS
python_version   = "3.12"    # Python latest stable
postgres_version = "17"      # PostgreSQL latest
redis_version    = "7"       # Redis latest stable

Coder Parameters

When creating a workspace, you'll be prompted for:

  1. Git Repository - Select from your available repositories
  2. Enable Services - Toggle database services on/off
  3. Enable AI Tools - Toggle AI development tool installation

🌐 Service Access

Web Applications

All services are accessible through Coder's reverse proxy with subdomain routing:

Service URL Pattern Description
VS Code code-server-{workspace}.dev.lab Browser-based IDE
Terminal Available in Coder dashboard Full bash shell
pgAdmin pgadmin-{workspace}.dev.lab PostgreSQL management
Qdrant qdrant-dashboard-{workspace}.dev.lab Vector DB dashboard
Dev Server nextjs-3000-{workspace}.dev.lab Next.js dev server
API Server api-8000-{workspace}.dev.lab FastAPI/Flask server
Vite Dev vite-5173-{workspace}.dev.lab Vite development

Database Connections

From within your workspace container:

# PostgreSQL
export POSTGRES_URL="postgresql://postgres:devpassword@postgres-{workspace-id}:5432/postgres"
psql $POSTGRES_URL

# Redis
export REDIS_URL="redis://:devpassword@redis-{workspace-id}:6379"
redis-cli -u $REDIS_URL

# Qdrant
export QDRANT_URL="http://qdrant-{workspace-id}:6333"
curl $QDRANT_URL/health

Environment Variables

These are automatically set in your workspace:

NODE_VERSION=20
PYTHON_VERSION=3.12
POSTGRES_URL=postgresql://postgres:***@postgres-{id}:5432/postgres
REDIS_URL=redis://:***@redis-{id}:6379  
QDRANT_URL=http://qdrant-{id}:6333

🔧 Development Workflow

Initial Setup

  1. Access Workspace

    # Run environment info script
    devinfo
    
  2. Verify Services

    # Check PostgreSQL
    pg_isready -h postgres-{workspace-id} -U postgres
    
    # Check Redis  
    redis-cli -h redis-{workspace-id} ping
    
    # Check Qdrant
    curl http://qdrant-{workspace-id}:6333/health
    

Common Tasks

Next.js Project

# Create new Next.js app
npx create-next-app@latest my-app
cd my-app
npm run dev  # Accessible at nextjs-3000-{workspace}.dev.lab

Python FastAPI Project

# Activate Python environment
source /home/coder/.venv/bin/activate

# Create FastAPI app
uv add fastapi uvicorn
# Your app runs on port 8000, accessible via reverse proxy

Database Development

# Connect to PostgreSQL
psql $POSTGRES_URL

# Create tables, run migrations, etc.
# Access pgAdmin for GUI management

🔍 Monitoring & Debugging

Built-in Monitoring

Coder automatically tracks:

  • CPU Usage - Updated every 60 seconds
  • RAM Usage - Updated every 60 seconds
  • Disk Usage - Updated every 5 minutes
  • Git Branch - Updated every 5 minutes

Health Checks

All services include comprehensive health checks:

  • PostgreSQL: pg_isready command
  • Redis: Connection test with redis-cli
  • Qdrant: HTTP health endpoint
  • Web Services: HTTP response verification

Logs and Debugging

# Container logs
docker logs postgres-{workspace-id}
docker logs redis-{workspace-id}
docker logs qdrant-{workspace-id}

# Service status
docker ps --filter label=coder.workspace_id={workspace-id}

# Network inspection  
docker network inspect coder-{workspace-id}

🔐 Security Considerations

Network Security

  • Isolated Networks - Each workspace has its own Docker network
  • No Host Exposure - Services only accessible through authenticated Coder session
  • Internal Communication - Services communicate using internal DNS names

Authentication

  • Database Passwords - Configurable via terraform.tfvars
  • Coder Authentication - All access requires Coder login
  • sudo Access - Granted to coder user for development flexibility

Data Persistence

  • Database Data - Persistent Docker volumes per workspace
  • Workspace Files - Persistent across container restarts
  • User Configuration - Home directory persistence

🚨 Troubleshooting

Common Issues

"Unable to find user coder"

Solution: Container automatically creates coder user during startup. If issues persist, check container logs.

Port Already in Use

Solution: This configuration uses no host port mappings. All routing is handled internally.

Services Not Accessible

Solutions:

  1. Verify reverse proxy wildcard configuration
  2. Check Coder environment variables:
    echo $CODER_ACCESS_URL
    echo $CODER_WILDCARD_ACCESS_URL  
    
  3. Confirm service health via Coder dashboard

Database Connection Issues

Solutions:

  1. Verify service is enabled in workspace parameters
  2. Check container status: docker ps
  3. Test internal connectivity: curl http://postgres-{id}:5432

Debug Commands

# Environment information
devinfo

# Network connectivity  
docker network ls | grep coder
docker network inspect coder-{workspace-id}

# Service health
curl http://qdrant-{workspace-id}:6333/health
pg_isready -h postgres-{workspace-id} -U postgres
redis-cli -h redis-{workspace-id} ping

# Container status
docker ps --filter label=coder.workspace_id={workspace-id}

🔄 Updates & Maintenance

Updating Tool Versions

  1. Modify terraform.tfvars

    node_version = "22"      # Update Node.js version
    python_version = "3.13"  # Update Python version
    
  2. Apply Changes

    terraform plan
    terraform apply
    
  3. Restart Workspaces - Changes apply to new workspace instances

Adding New Services

  1. Add to services.tf - Define new container resource
  2. Add to apps.tf - Create Coder app for access
  3. Update variables.tf - Add configuration options
  4. Update startup script in workspace.tf if needed

🤝 Contributing

Adding New Features

  1. Follow the existing file structure and naming conventions
  2. Add proper health checks for new services
  3. Update this README with new service documentation
  4. Test with multiple concurrent workspaces

Best Practices

  • Resource Labels - All resources should include coder.workspace_id label
  • Network Isolation - New services should join workspace network
  • No Host Ports - Use internal networking only
  • Health Checks - All web services need health check endpoints
  • Persistent Data - Use Docker volumes for data that should survive restarts

📚 References

📝 License

This configuration is provided as-is for development purposes. Modify passwords and security settings for production use.


🚀 Happy Coding! Your isolated development environment is ready for productive development with full database support and AI-powered tools.