This directory contains Terraform configuration for managing SRA infrastructure on Vercel.
-
Terraform >= 1.0
# Install Terraform # Windows (Chocolatey): choco install terraform # macOS (Homebrew): brew install terraform # Or download from: https://www.terraform.io/downloads
-
Vercel API Token
- Get from: https://vercel.com/account/tokens
- Scope: "Read and Write"
- Expiration: No expiration (with 90-day rotation policy)
# 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!Edit terraform.tfvars with your actual values:
vercel_api_token- From Vercel dashboarddatabase_url- From Supabasegemini_api_key- From Google AI Studio- All other secrets from your
.envfiles
cd terraform
terraform initThis downloads the Vercel provider and sets up the backend.
# Preview what will be created/changed
terraform plan# Apply changes to create/update infrastructure
terraform apply
# Auto-approve (use with caution)
terraform apply -auto-approve# Show current infrastructure
terraform show
# List all resources
terraform state list# Remove all managed infrastructure
terraform destroy
# WARNING: This will delete your Vercel projects!
# Only use for testing or decommissioningterraform/
├── 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
Ensure these files are in .gitignore:
terraform.tfvarsterraform.tfstateterraform.tfstate.backup.terraform/
Alternative to terraform.tfvars:
export TF_VAR_vercel_api_token="your-token"
export TF_VAR_database_url="your-db-url"
# ... etc
terraform applyFor 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"
}
}
}# 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# 1. Update configuration files
# 2. Plan changes
terraform plan -out=tfplan
# 3. Review plan
# 4. Apply if looks good
terraform apply tfplan# 1. Ensure terraform.tfvars has all secrets
# 2. Initialize Terraform
terraform init
# 3. Recreate infrastructure
terraform applyCause: Missing api_token in provider configuration or empty vercel_api_token variable.
Solution:
- Check that
terraform.tfvarshas the correct token values. - Ensure
main.tflinks the variable:provider "vercel" { api_token = var.vercel_api_token }
Cause: Trying to create a project that already exists
Solution:
# Import existing project
terraform import vercel_project.sra_frontend <project-id>Cause: Invalid or expired Vercel token
Solution:
- Generate new token at vercel.com/account/tokens
- Update
terraform.tfvarsor environment variable
- Generate new Vercel API token
- Update
terraform.tfvarsor environment variable - Test with
terraform plan - Document rotation in
OPERATIONS.md
# 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)For issues or questions:
- Check OPERATIONS.md for operational procedures
- Review ARCHITECTURE.md for infrastructure design
- Contact: aniketsahaworkspace@gmail.com