Terraform configuration for deploying this application to a cloud provider.
environments/- Environment-specific configurations (preview,staging,prod)modules/- Reusable Terraform modules for database connectivity
- Creates Cloudflare Hyperdrive configurations for Neon PostgreSQL connectivity
- Provides connection pooling and edge optimization for database access
- Configurations: direct (no-cache) and cached (60s TTL) variants
Each environment includes:
main.tf- Module instantiationvariables.tf- Input variablesoutputs.tf- Output valuesprovider.tf- Provider configurationbackend.tf- Remote state configurationterraform.tfvars.example- Example variablesterraform.tfvars- Environment-specific variables
# Navigate to environment
cd environments/preview
# Copy and configure variables
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your values
# Initialize and apply
terraform init
terraform plan
terraform apply- Terraform >= 1.12
- Cloudflare account with API token
- Required variables:
project_name(lowercase, hyphens only),cloudflare_account_id
For the best development experience with Terraform files:
- VS Code: Install the HashiCorp Terraform extension (included in project recommendations)
- Formatting: Run
terraform fmt -recursiveto format all .tf files - Validation: Use
terraform validateto check syntax before applying
Your Cloudflare API token needs the following permissions:
- Zone:DNS:Edit (for DNS management)
- Zone:Zone:Read (for domain management)
- Zone:Zone Settings:Edit (for configuration)
- Account:Cloudflare Hyperdrive:Edit (for database connection pooling)
- Keep
terraform.tfvarsfiles secure and never commit them to version control - The
.gitignoreshould include*.tfvars(except.examplefiles) - Store sensitive values in environment variables when possible
This configuration uses remote state storage for team collaboration:
- State files are stored in Cloudflare R2 (configured in
backend.tf) - Each environment maintains separate state files
- Initialize with
terraform initto download remote state - State locking prevents concurrent modifications
After successful deployment, use outputs to configure your application:
# Get Hyperdrive configuration IDs for apps/edge/wrangler.jsonc
terraform output hyperdrive_direct_id
terraform output hyperdrive_cached_idAdd these values to your application's environment configuration.
Authentication Error
Error: Authentication error (10000)
- Verify your Cloudflare API token has correct permissions
- Check
CLOUDFLARE_API_TOKENenvironment variable is set
Resource Already Exists
Error: resource already exists
- Check if resources exist in Cloudflare dashboard
- Import existing resources:
terraform import <resource> <id>
State Lock Error
Error: state locked
- Another user may be running terraform
- Force unlock (use carefully):
terraform force-unlock <lock-id>
Invalid Project Name
Error: invalid project name
- Use only lowercase letters, numbers, and hyphens
- Must start with a letter, max 63 characters