|
| 1 | +# Infrastructure |
| 2 | + |
| 3 | +Terraform configuration for deploying this application to a cloud provider. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +- `environments/` - Environment-specific configurations (`preview`, `staging`, `prod`) |
| 8 | +- `modules/` - Reusable Terraform modules for database and storage |
| 9 | + |
| 10 | +## Modules |
| 11 | + |
| 12 | +### Database (`modules/db`) |
| 13 | + |
| 14 | +- Creates Cloudflare D1 (or, Neon) database for each environment |
| 15 | +- Names: `{project-name}-{environment}` |
| 16 | + |
| 17 | +### Storage (`modules/storage`) |
| 18 | + |
| 19 | +- Creates R2 buckets for file storage |
| 20 | +- Main bucket: `{project-name}-{environment}` |
| 21 | +- Uploads bucket: `{project-name}-uploads-{environment}` |
| 22 | + |
| 23 | +## Environments |
| 24 | + |
| 25 | +Each environment includes: |
| 26 | + |
| 27 | +- `main.tf` - Module instantiation |
| 28 | +- `variables.tf` - Input variables |
| 29 | +- `outputs.tf` - Output values |
| 30 | +- `provider.tf` - Provider configuration |
| 31 | +- `backend.tf` - Remote state configuration |
| 32 | +- `terraform.tfvars.example` - Example variables |
| 33 | +- `terraform.tfvars` - Environment-specific variables |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +```bash |
| 38 | +# Navigate to environment |
| 39 | +cd environments/preview |
| 40 | + |
| 41 | +# Copy and configure variables |
| 42 | +cp terraform.tfvars.example terraform.tfvars |
| 43 | +# Edit terraform.tfvars with your values |
| 44 | + |
| 45 | +# Initialize and apply |
| 46 | +terraform init |
| 47 | +terraform plan |
| 48 | +terraform apply |
| 49 | +``` |
| 50 | + |
| 51 | +## Requirements |
| 52 | + |
| 53 | +- Terraform >= 1.12 |
| 54 | +- Cloudflare account with API token |
| 55 | +- Required variables: `project_name` (lowercase, hyphens only), `cloudflare_account_id` |
| 56 | + |
| 57 | +## Development Setup |
| 58 | + |
| 59 | +For the best development experience with Terraform files: |
| 60 | + |
| 61 | +- **VS Code**: Install the HashiCorp Terraform extension (included in project recommendations) |
| 62 | +- **Formatting**: Run `terraform fmt -recursive` to format all .tf files |
| 63 | +- **Validation**: Use `terraform validate` to check syntax before applying |
| 64 | + |
| 65 | +## Security |
| 66 | + |
| 67 | +### API Token Permissions |
| 68 | + |
| 69 | +Your Cloudflare API token needs the following permissions: |
| 70 | + |
| 71 | +- **Zone:Zone:Read** (for domain management) |
| 72 | +- **Zone:Zone Settings:Edit** (for configuration) |
| 73 | +- **Account:Cloudflare D1:Edit** (for database creation) |
| 74 | +- **Account:Cloudflare R2:Edit** (for storage buckets) |
| 75 | + |
| 76 | +### Secrets Management |
| 77 | + |
| 78 | +- Keep `terraform.tfvars` files secure and never commit them to version control |
| 79 | +- The `.gitignore` should include `*.tfvars` (except `.example` files) |
| 80 | +- Store sensitive values in environment variables when possible |
| 81 | + |
| 82 | +## State Management |
| 83 | + |
| 84 | +This configuration uses remote state storage for team collaboration: |
| 85 | + |
| 86 | +- State files are stored in Cloudflare R2 (configured in `backend.tf`) |
| 87 | +- Each environment maintains separate state files |
| 88 | +- Initialize with `terraform init` to download remote state |
| 89 | +- State locking prevents concurrent modifications |
| 90 | + |
| 91 | +## Outputs |
| 92 | + |
| 93 | +After successful deployment, use outputs to configure your application: |
| 94 | + |
| 95 | +```bash |
| 96 | +# Get database ID for wrangler.jsonc |
| 97 | +terraform output database_id |
| 98 | + |
| 99 | +# Get R2 bucket names for environment variables |
| 100 | +terraform output storage_bucket_name |
| 101 | +terraform output uploads_bucket_name |
| 102 | +``` |
| 103 | + |
| 104 | +Add these values to your application's environment configuration. |
| 105 | + |
| 106 | +## Troubleshooting |
| 107 | + |
| 108 | +### Common Issues |
| 109 | + |
| 110 | +**Authentication Error** |
| 111 | + |
| 112 | +``` |
| 113 | +Error: Authentication error (10000) |
| 114 | +``` |
| 115 | + |
| 116 | +- Verify your Cloudflare API token has correct permissions |
| 117 | +- Check `CLOUDFLARE_API_TOKEN` environment variable is set |
| 118 | + |
| 119 | +**Resource Already Exists** |
| 120 | + |
| 121 | +``` |
| 122 | +Error: resource already exists |
| 123 | +``` |
| 124 | + |
| 125 | +- Check if resources exist in Cloudflare dashboard |
| 126 | +- Import existing resources: `terraform import <resource> <id>` |
| 127 | + |
| 128 | +**State Lock Error** |
| 129 | + |
| 130 | +``` |
| 131 | +Error: state locked |
| 132 | +``` |
| 133 | + |
| 134 | +- Another user may be running terraform |
| 135 | +- Force unlock (use carefully): `terraform force-unlock <lock-id>` |
| 136 | + |
| 137 | +**Invalid Project Name** |
| 138 | + |
| 139 | +``` |
| 140 | +Error: invalid project name |
| 141 | +``` |
| 142 | + |
| 143 | +- Use only lowercase letters, numbers, and hyphens |
| 144 | +- Must start with a letter, max 63 characters |
0 commit comments