Skip to main content

Overview

MCP Server uses a multi-environment Terraform strategy to maintain consistent infrastructure across development, staging, and production while optimizing costs and managing risk.

Development

Cost-optimized for testing

Staging

Production-like validation

Production

HA, resilient, monitored

Environment Philosophy

1

Consistent Modules

Same Terraform modules across all environments
  • Reduces “works in dev, breaks in prod” issues
  • Ensures feature parity
  • Simplifies testing
2

Parameter-Driven Differences

Use variables to adjust sizing, HA, and features
  • Dev: Single zone, smaller instances
  • Staging: Regional, production-like
  • Prod: Regional, HA, monitoring
3

Isolated State

Separate Terraform state per environment
  • Prevents accidental cross-environment changes
  • Allows independent lifecycle management
  • Enables parallel deployments
4

Progressive Rollout

Test changes in dev → staging → prod
  • Validate in dev
  • Load test in staging
  • Deploy to prod with confidence

Environment Comparison

Configuration Matrix


Directory Structure

Key principle: main.tf, variables.tf, outputs.tf are identical across environments. Only terraform.tfvars differs.

Development Environment

Purpose

  • Rapid iteration for engineers
  • Cost optimization (minimal resources)
  • No SLA requirements

Configuration

Tradeoffs

No HA: Single zone means cluster downtime during zone failures (rare but possible).
Acceptable for dev: Downtime doesn’t impact customers, cost savings are significant.

Staging Environment

Purpose

  • Pre-production validation
  • Load testing with production-like config
  • Integration testing across services
  • Security scanning before prod deployment

Configuration

Key Differences from Production

Production: Has Cloud SQL read replicas for read scaling.Staging: No read replicas (cost savings, same testing value).
Production: Binary Authorization enforcing (blocks unsigned images).Staging: Audit mode (logs denials, doesn’t block). Allows testing image signing without risk.
Production: db-custom-4-15360 (4 vCPU).Staging: db-custom-2-7680 (2 vCPU). Sufficient for load testing without production traffic.

Production Environment

Purpose

  • Live customer traffic
  • 99.9% uptime SLA
  • Security compliance (Binary Auth, audit logs)
  • Full observability (SLI/SLO, alerting)

Configuration

Production-Only Features

Blocks deployment of unsigned or unverified container images.
Requires image signing in CI/CD pipeline.
Recover database to any second within retention period.
Essential for disaster recovery.
Scale read traffic without impacting primary database.
Improves performance and availability.
Automated cluster backups for disaster recovery.
Enables cluster restoration after catastrophic failure.
Service-level objectives with error budgets.
  • 99.9% availability SLO
  • P95 latency < 2s
  • P99 latency < 5s
Alerts when error budget is depleted.

Deployment Strategy

Progressive Rollout

1

1. Deploy to Development

Validation:
  • Terraform apply succeeds
  • Cluster is accessible
  • Basic smoke tests pass
2

2. Deploy to Staging

Validation:
  • Integration tests pass
  • Load tests show acceptable performance
  • Security scans complete (no critical issues)
3

3. Deploy to Production (with approval)

Validation:
  • Canary deployment (10% traffic)
  • Monitor SLI/SLO metrics
  • Full rollout after 24 hours with no issues

Rollback Strategy

If infrastructure change causes issues:

State Management

Separate State Per Environment

terraform/environments/gcp-dev/backend.tf
Best practice: Same bucket, different prefixes (cost-effective and easy to manage).

Variable Management

Shared Variables (variables.tf)

Variables defined in variables.tf are identical across environments:

Environment-Specific Values (terraform.tfvars)

Only terraform.tfvars differs across environments:

Testing Strategy

1

Unit Tests (Terraform Validate)

Validates HCL syntax and module compatibility.
2

Integration Tests (Terraform Plan)

Exit codes:
  • 0 = No changes
  • 1 = Error
  • 2 = Changes present (expected)
3

Compliance Scans

Scans for:
  • CIS GKE Benchmark compliance
  • Terraform security (Trivy, tfsec, Checkov)
  • Secrets in code (Gitleaks)
4

Load Testing (Staging)

Validates performance at scale.

Cost Optimization by Environment

Development

Auto-shutdown dev cluster after hours
Savings: $50-70/month (60-70%)

Staging

Right-size based on actual load test results
enable_committed_use_discounts = true committed_use_term = “3_YEAR” # 52% discount
cd terraform/environments/gcp-dev terraform init && terraform apply
cd terraform/environments/gcp-staging terraform init && terraform apply