Skip to content

Latest commit

 

History

History
266 lines (193 loc) · 5.34 KB

File metadata and controls

266 lines (193 loc) · 5.34 KB

SRA Infrastructure as Code (Terraform)

This directory contains Terraform configuration for managing SRA infrastructure on Vercel.

Prerequisites

  1. Terraform >= 1.0

    # Install Terraform
    # Windows (Chocolatey):
    choco install terraform
    
    # macOS (Homebrew):
    brew install terraform
    
    # Or download from: https://www.terraform.io/downloads
  2. Vercel API Token

Setup

1. Create Configuration File

# Copy example file
# Mac/Linux:
cp terraform.tfvars.example terraform.tfvars

# Windows (PowerShell):
Copy-Item terraform.tfvars.example -Destination terraform.tfvars
# OR Windows (CMD):
copy terraform.tfvars.example terraform.tfvars

# Edit with your values
# IMPORTANT: Never commit terraform.tfvars to git!

2. Fill in Required Values

Edit terraform.tfvars with your actual values:

  • vercel_api_token - From Vercel dashboard
  • database_url - From Supabase
  • gemini_api_key - From Google AI Studio
  • All other secrets from your .env files

Usage

Initialize Terraform

cd terraform
terraform init

This downloads the Vercel provider and sets up the backend.

Plan Changes

# Preview what will be created/changed
terraform plan

Apply Configuration

# Apply changes to create/update infrastructure
terraform apply

# Auto-approve (use with caution)
terraform apply -auto-approve

View Current State

# Show current infrastructure
terraform show

# List all resources
terraform state list

Destroy Infrastructure

# Remove all managed infrastructure
terraform destroy

# WARNING: This will delete your Vercel projects!
# Only use for testing or decommissioning

File Structure

terraform/
├── main.tf                    # Provider and backend configuration
├── variables.tf               # Variable definitions
├── vercel.tf                  # Vercel project resources
├── outputs.tf                 # Output values
├── terraform.tfvars.example   # Example configuration
├── terraform.tfvars           # Your actual values (gitignored)
└── README.md                  # This file

Security Best Practices

1. Never Commit Secrets

Ensure these files are in .gitignore:

  • terraform.tfvars
  • terraform.tfstate
  • terraform.tfstate.backup
  • .terraform/

2. Use Environment Variables

Alternative to terraform.tfvars:

export TF_VAR_vercel_api_token="your-token"
export TF_VAR_database_url="your-db-url"
# ... etc

terraform apply

3. Remote State (Production)

For production, use remote state backend:

# main.tf
terraform {
  backend "s3" {
    bucket = "sra-terraform-state"
    key    = "production/terraform.tfstate"
    region = "us-east-1"
  }
}

Or use Terraform Cloud:

terraform {
  cloud {
    organization = "your-org"
    workspaces {
      name = "sra-production"
    }
  }
}

Workflow

Initial Setup (One-time)

# 1. Initialize
terraform init

# 2. Import existing Vercel projects (if any)
terraform import vercel_project.sra_frontend <project-id>
terraform import vercel_project.sra_backend <project-id>

# 3. Plan and apply
terraform plan
terraform apply

Regular Updates

# 1. Update configuration files
# 2. Plan changes
terraform plan -out=tfplan

# 3. Review plan
# 4. Apply if looks good
terraform apply tfplan

Disaster Recovery

# 1. Ensure terraform.tfvars has all secrets
# 2. Initialize Terraform
terraform init

# 3. Recreate infrastructure
terraform apply

Troubleshooting

"Error: Unable to find api_token"

Cause: Missing api_token in provider configuration or empty vercel_api_token variable.

Solution:

  1. Check that terraform.tfvars has the correct token values.
  2. Ensure main.tf links the variable:
    provider "vercel" {
      api_token = var.vercel_api_token
    }

"Error: Project already exists"

Cause: Trying to create a project that already exists

Solution:

# Import existing project
terraform import vercel_project.sra_frontend <project-id>

"Error: Unauthorized"

Cause: Invalid or expired Vercel token

Solution:

  1. Generate new token at vercel.com/account/tokens
  2. Update terraform.tfvars or environment variable

Maintenance

Token Rotation (Every 90 Days)

  1. Generate new Vercel API token
  2. Update terraform.tfvars or environment variable
  3. Test with terraform plan
  4. Document rotation in OPERATIONS.md

State Backup

# Backup current state
# Bash/Mac/Linux
cp terraform.tfstate terraform.tfstate.backup-$(date +%Y%m%d)

# Windows (PowerShell)
Copy-Item terraform.tfstate -Destination ("terraform.tfstate.backup-" + (Get-Date -Format "yyyyMMdd"))

# Store securely (encrypted)

Resources

Support

For issues or questions: