
A practical guide to automating vector search infrastructure with Terraform and Makefiles, streamlining the deployment of PGVector on Cloud SQL and letting you focus on building powerful semantic applications.
๐ GitHub Repository: View on GitHub
๐ Why Infrastructure as Code May Help with Vector Search
Manual cloud setup can sometimes be time-consuming and prone to inconsistenciesโa missed configuration step might lead to unexpected behavior or security concerns. When building a vector search application, a RAG system, or a semantic recommendation engine for production, having a reliable infrastructure approach alongside your application code could be beneficial.
Many of us have experienced the initial satisfaction of setting up resources through the Google Cloud Console, only to later struggle when trying to recreate that environment or share the exact setup with teammates. Without version control or documentation, reproducing environments consistently can be challenging. While manual setup works well for quick experiments, teams often look for more systematic approaches when moving toward production.
We've found that combining Terraform for infrastructure definition with Makefiles for operation simplification might help address some common challenges. This approach allows you to define your cloud environment as code and potentially automate deployments, which could help maintain consistency across environments and reduce time spent on manual configuration.
By turning your vector database stack into code, you get:
Command Interface] Makefile --> |Executes| TerraformCommands[โ๏ธ Terraform Operations] subgraph "Repository Structure" Makefile TerraformDir[๐ terraform-sql
Infrastructure Config] EnvDir[๐ environments
Environment Configs] ModulesDir[๐งฉ modules
Reusable Components] TutorialsDir[๐ tutorials
Example Apps] TerraformDir --> EnvDir TerraformDir --> ModulesDir end subgraph "Environment Configurations" EnvDir --> DevEnv[๐งช dev
Development] EnvDir --> PreprodEnv[๐ preprod
Pre-production] EnvDir --> ProdEnv[๐ญ prod
Production] end subgraph "Terraform Modules" ModulesDir --> ProjectModule[๐๏ธ project
GCP Setup] ModulesDir --> VPCModule[๐ vpc
Network Config] ModulesDir --> SQLModule[๐๏ธ sql
Database Config] end subgraph "Terraform Commands" TerraformCommands --> TFInit[๐ terraform init] TerraformCommands --> TFPlan[๐ terraform plan] TerraformCommands --> TFApply[โ terraform apply] TerraformCommands --> TFDestroy[๐ฅ terraform destroy] end subgraph "Database Operations" Makefile --> DBCommands[๐๏ธ Database Commands] DBCommands --> SetupDB[๐ง Setup Database
Schema & Extensions] DBCommands --> SampleData[๐ Load Sample Data] DBCommands --> CheckDB[โ Verify Setup] DBCommands --> RunDemo[๐ Run Demo Queries] end subgraph "GCP Resources" ProjectModule --> GCPProject[โ๏ธ GCP Project] GCPProject --> APIs[๐ Enabled APIs
SQL, IAM, Compute] GCPProject --> ServiceAccts[๐ Service Accounts] VPCModule --> VPCNetwork[๐ VPC Network] VPCModule --> FirewallRules[๐ฅ Firewall Rules] VPCModule --> PrivateAccess[๐ Private Access] SQLModule --> CloudSQL[๐๏ธ PostgreSQL Instance] CloudSQL --> PGVector[๐ง pgvector Extension] PGVector --> VectorDB[๐ Vector Database
with Indexes] end DevEnv --> |Configures| DevResources[๐งช Dev Config
Public IP, Open Firewall
Lower Specs, ZONAL] PreprodEnv --> |Configures| PreprodResources[๐ Preprod Config
Private IP, VPC
Medium Specs, ZONAL] ProdEnv --> |Configures| ProdResources[๐ญ Prod Config
Private IP, Strict VPC
High Specs, REGIONAL] DevResources --> GCPProject PreprodResources --> GCPProject ProdResources --> GCPProject VectorDB --> Applications[๐ Vector Applications] Applications --> UseCases[๐ฏ Use Cases
Recommendations, RAG
Semantic Search] style User fill:#E8F4FD,stroke:#2C5AA0,stroke-width:2px style Makefile fill:#FFF2CC,stroke:#B7950B,stroke-width:2px style TerraformCommands fill:#E8F6F3,stroke:#1B5E4F,stroke-width:2px style DBCommands fill:#F4ECF7,stroke:#7D3C98,stroke-width:2px style GCPProject fill:#FADBD8,stroke:#A93226,stroke-width:2px style VectorDB fill:#E1F5FE,stroke:#1976D2,stroke-width:2px style Applications fill:#EAEDED,stroke:#566573,stroke-width:2px
Comprehensive architecture showing repository structure, workflow, and GCP resources
Consistency
Every environment is identical, every time
Speed
Deploy or tear down resources in minutes, not hours
Version Control
Track changes, roll back, and collaborate with your team
Scalability
Grow your infrastructure as your needs evolve
Reproducibility
Create identical environments across dev, pre-production, and production
Documentation
Your code serves as living documentation of your architecture
Cost Control
Easily destroy resources when not in use, reducing cloud expenses
โจ Key Features
Our PGVector on CloudSQL solution provides several key advantages:
๐๏ธ Infrastructure as Code
Terraform-based provisioning of Google Cloud SQL PostgreSQL instances with pgvector extension
๐ Simplified Operations
All database operations managed directly through intuitive Makefile commands
๐ Multi-Environment Support
Separate configurations for development, pre-production, and production environments
๐ Quick Setup
Easy-to-follow setup process with comprehensive documentation and examples
๐๏ธ Terraform Architecture for PostgreSQL Vector Search
Ready to build something cool? Let's get our hands dirty with a complete PostgreSQL setup that has pgvector extensions baked in. No more manual steps or forgotten configurationsโjust pure, repeatable infrastructure.
The repository is organized using a modular approach with separate environments, making it enterprise-ready from day one:
.
โโโ ๐ Makefile # Main command interface for all operations
โโโ ๐ terraform-sql/ # Terraform configuration for Cloud SQL
โ โโโ ๐ environments/ # Environment-specific configurations
โ โ โโโ ๐ dev/ # Development environment
โ โ โโโ ๐ preprod/ # Pre-production environment
โ โ โโโ ๐ prod/ # Production environment
โ โโโ ๐ modules/ # Reusable Terraform modules
โ โโโ ๐ project/ # GCP project configuration
โ โโโ ๐ sql/ # Cloud SQL instance configuration
โ โโโ ๐ vpc/ # Network configuration
โโโ ๐ how_to_tutorials/ # Example applications and demos
โโโ ๐ semantic_demo/ # Semantic search demo for product recommendations
Visual overview of the repository structure and modular organization for Terraform-based PGVector deployment.
The architecture aims to be straightforward while addressing common infrastructure needs:
- ๐๏ธ A dedicated GCP project created by Terraform providing some isolation between projects
- ๐ Custom VPC with private network connectivity for enhanced security
- ๐ Cloud SQL PostgreSQL instance with configurable access options (public access limited to dev environments)
- ๐งฉ Vector database configuration for storing embeddings
- ๐ฆ Environment separation for dev, preprod, and prod following common DevOps practices
๐ What Does the Terraform Setup Do?
The modular configuration in terraform-sql
automates the following:
- Initializes your Google Cloud project and authenticates Terraform โ๏ธ
- Activates essential APIs: Cloud SQL, networking, IAM, Compute, and more ๐
- Creates service accounts with proper permissions ๐
- Creates a secure VPC network with private subnet ranges ๐ก๏ธ
- Configures firewall rules for secure communication ๐ฅ
- Sets up Private Service Access for Cloud SQL connections ๐
- Provisions a Cloud SQL PostgreSQL instance with proper sizing based on environment ๐
- Configures it for the
pgvector
extension, enabling vector similarity search ๐ง - Sets up database users with secure passwords ๐ค
- Configures automated backups and high availability options ๐พ
- Separate configurations for dev, preprod, and prod environments ๐งช
- Parameterization through variables for project name, region, database size, and more ๐งฎ
- Uses Terraform workspaces to isolate state between environments ๐
- Provides connection details and resource IDs for use in your application or CI/CD pipelines ๐
- Outputs important information like database IP, connection names, and project details ๐
๐ Security Considerations
The repository implements different security configurations for each environment. Let's look at the networking module which includes thoughtful firewall rules:
# Allow PostgreSQL access from authorized sources
resource "google_compute_firewall" "allow_postgres" {
name = "${var.name_prefix}-allow-postgres"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "tcp"
ports = ["5432"]
}
# In development, this might be more permissive
# In production, this should be limited to specific sources
source_ranges = var.authorized_networks
target_tags = ["cloudsql"]
description = "Allow PostgreSQL access from authorized networks"
}
# Allow internal traffic within the VPC
resource "google_compute_firewall" "allow_internal" {
name = "${var.name_prefix}-allow-internal"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "all"
}
source_ranges = [var.subnet_cidr]
description = "Allow all internal traffic within the VPC"
}
๐ Key Security Features
- ๐ Environment-specific security settings (dev is more open, prod is locked down)
- ๐ Parameterized authorized networks for database access, not hard-coded
- ๐ก๏ธ Private Service Access for Cloud SQL, ensuring database isn't publicly exposed in production
- ๐ฎโโ๏ธ IAM role separation with principle of least privilege
- ๐งฉ Separate service accounts for different components
- ๐ฏ Set
authorized_networks
to specific IP ranges in prod environment - ๐ค Enable Cloud SQL IAM authentication for user access
- ๐๏ธ Store secrets in Secret Manager, not in Terraform variables
- ๐ Enable audit logging and monitoring
- ๐ Implement database encryption at rest and in transit
The repository suggests a progressive security model that allows starting with more permissive settings in development environments while implementing stricter controls in production. We recommend carefully reviewing security settings before deploying to productionโsecurity considerations are an essential part of any deployment strategy.
๐ฆ Simplified Deployment: Using the Makefile
One of the standout features of this project is its streamlined approach using Makefiles. You can deploy and manage your entire vector search infrastructure with simple commands:
# Copy the example environment file
cp .env.example .env
# Edit with your credentials
# Copy the example variables file
cp terraform-sql/environments/dev/terraform.tfvars.example terraform-sql/environments/dev/terraform.tfvars
# Edit terraform.tfvars with your specific configuration values
# Initialize Terraform for your chosen environment
make tf-init ENV=dev
# Review what will be created
make tf-plan ENV=dev
# Apply the changes
make tf-apply ENV=dev
# Create database schema with pgvector extension
make setup-db ENV=dev
# Add sample product data
make add-sample-data ENV=dev
# Verify database setup
make check-db ENV=dev
# Run simple demo queries
make run-simple-demo ENV=dev
# Check current environment settings
make env
# Connect directly to the database
make connect-db ENV=dev
# Clean up when done
make tf-destroy ENV=dev
This Makefile approach attempts to simplify complex commands, which might help team members collaborate more effectively without needing to memorize every technical detail.
๐งโ๐ป Post-Deployment: Setting Up pgvector
After your infrastructure is deployed, you need to set up the vector extension. Traditionally this would require multiple manual steps, but our Makefile simplifies this:
make setup-db ENV=dev
This single command connects to your database and runs:
CREATE EXTENSION IF NOT EXISTS vector;
-- Create a database for our application
CREATE DATABASE vector_search;
\c vector_search
-- Create a table for semantic product data
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
category TEXT,
price DECIMAL(10, 2),
embedding VECTOR(1536) -- For OpenAI embeddings, adjust as needed
);
-- Create an index for fast similarity search
CREATE INDEX ON products USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
๐ค Potential Use Cases
This infrastructure can support various vector-based applications:
Use Case | Description | Implementation Benefits |
---|---|---|
๐ Product Recommendations | Deliver personalized suggestions using semantic similarity | Higher conversion rates, improved UX |
๐ค Retrieval-Augmented Generation (RAG) | Enhance chatbots and LLMs with context-aware retrieval | More accurate responses, reduced hallucinations |
๐ Semantic Search | Go beyond keywords to find relevant documents, products, or answers | Better search relevance, natural language queries |
๐งน Content Deduplication | Identify near-duplicate items in large datasets | Data quality improvement, storage optimization |
๐จ Anomaly Detection | Spot unusual patterns by comparing vector representations | Early threat detection, quality assurance |
๐ฏ Personalization Engines | Tailor user experiences based on semantic profiles | Increased engagement, user satisfaction |
๐ Success Story: E-commerce Recommendation Engine
Context: A mid-sized e-commerce platform implemented vector-based product recommendations using similar infrastructure, resulting in a 35% increase in click-through rates and 20% boost in sales conversion.
- Semantic Understanding: Products were matched based on meaning, not just keywords
- Real-time Performance: Sub-100ms query responses enabled dynamic recommendations
- Scalable Infrastructure: Terraform automation allowed rapid scaling during peak seasons
- A/B Testing: Multiple environments enabled safe experimentation
๐งฌ The Power of Reproducibility
One potential benefit of Infrastructure as Code is improved reproducibility. Many of us have faced challenges with environment inconsistencies or struggled to remember exact setup steps.
With the Terraform and Makefile approach described here, you might find it easier to:
-
โฑ๏ธ
Create similar testing environments more efficiently
Spin up identical environments in minutes rather than hours of manual configuration
-
๐
Deploy to different regions using the same configuration base
Multi-region deployment becomes a simple parameter change
-
๐
Recover from accidental resource deletions by reapplying your configuration
Disaster recovery becomes a matter of running terraform apply
-
๐
Onboard new team members by sharing code rather than lengthy setup instructions
New developers can be productive in hours, not days
This approach could potentially reduce documentation overhead and make collaboration more straightforward, though every team's experience will vary based on their specific needs and existing workflows.
๐ Production-Ready Deployment Tips
Ready to go beyond the demo and into production? The repository is already structured to support this journey, with separate environments for dev, preprod, and prod. Here are some hard-learned lessons to save you some late-night troubleshooting sessions:
Environment Differences
Environment | Access | Network | Resources | Availability | Protection |
---|---|---|---|---|---|
๐งช dev | Public IP | Open firewall | Lower specs | ZONAL | No deletion protection |
๐ preprod | Private IP only | VPC network | Medium specs | ZONAL | No deletion protection |
๐ญ prod | Private IP only | Strict VPC | Higher specs | REGIONAL | Deletion protection + backups |
Deployment Steps
- Environment progression (follow the path to production):
- Start with
dev
environment to test your configuration ๐งช - Validate in
preprod
with production-like settings ๐ - Apply strict security rules only in
prod
๐ก๏ธ
- Start with
- Secure your database (because security matters):
- In the
prod
environment variables, restrictauthorized_networks
to specific IP ranges ๐ - Enable Private Service Connect for production deployments ๐
- Enable Cloud SQL IAM authentication by setting
enable_iam_auth = true
in your tfvars ๐ค
- In the
- Optimize for performance (vector search gets hungry):
- The repository supports different instance sizes per environment:
db-f1-micro
for dev testingdb-custom-2-7680
for preproddb-custom-4-15360
for production workloads ๐๏ธ
- Vector search loves RAMโuse the high-memory options in the variables ๐ง
- Benchmark with real data in preprod before going to production โณ
- The repository supports different instance sizes per environment:
- Manage secrets properly (no passwords in GitHub, please):
- The repository includes
.gitignore
for.tfvars
files ๐ซ - Use environment variables for sensitive data in CI/CD pipelines ๐
- Consider using the
google_secret_manager_secret
resource ๐๏ธ - Use Terraform's
sensitive = true
marking for outputs ๐
- The repository includes
- Use the environment-specific Makefiles:
- Follow the principle of "make it easy to do the right thing" ๐ฏ
- Take advantage of the input validation built into the modules ๐
- Monitoring saves lives (or at least your weekend):
- Set up Cloud Monitoring alerts before you need them ๐จ
- Enable the automated backups using the
backup_configuration
in the SQL module ๐พ - Consider read replicas for high-traffic applications by setting
read_replica_size
๐
๐ Conclusion
By automating your vector search infrastructure with Terraform and Makefiles, you might find your workflow becoming more streamlined and reproducible. Rather than clicking through console interfaces and trying to remember all the steps, this approach offers a way to document and version your infrastructure setup.
When your database configuration is defined in code, it becomes something you can collaboratively work on, review, and improve over time โ a shared resource for your team.
๐ฏ Key Project Features
The pgvector_cloudsql_gcp repository attempts to implement this approach with features like:
- ๐ Environment Separation: Different configurations for dev, preprod, and prod environments
- ๐งฉ Modular Design: Reusable modules for various infrastructure components
- ๐ก๏ธ Progressive Security: Security controls that adapt to each environment
- โ๏ธ Operation Helpers: Makefile commands to assist with common tasks
- ๐ Organized Structure: A folder organization that aims to be logical and maintainable
Infrastructure as code can be a valuable approach that helps bridge the gap between experimental demos and production-ready systems. For vector search applications where configuration details can significantly impact performance, this approach might save you considerable time and reduce potential errors.
๐ Additional Resources
- Official PGVector Documentation
- Cloud SQL Documentation
- Terraform Documentation
- View Complete Project Repository
We'd welcome your feedback, suggestions, or contributions to help improve this project.