Rebuild & Deployment Instructions
Rebuild & Deployment Instructions
Follow these instructions to provision the Cloudflare resources, initialize the D1 database schema, and test/deploy the edge API.
1. Prerequisite Environment Variables
Create a local .env or set these in your terminal. Do not commit these variables to public source control.
export TF_VAR_cloudflare_api_token="your-cloudflare-token"export TF_VAR_cloudflare_account_id="your-cloudflare-account-id"2. Infrastructure Provisioning (Terraform)
-
Navigate to the
infra/directory:Terminal window cd infra -
Initialize Terraform and fetch the Cloudflare provider:
Terminal window terraform init -
Generate a plan to review resource allocations:
Terminal window terraform plan -
Deploy the D1 Database and Worker placeholder:
Terminal window terraform apply -auto-approveNote: Terraform will output the generated D1 Database ID (UUID).
3. Database Schema Initialization (Wrangler)
-
Navigate to the
api/directory:Terminal window cd ../api -
Update the
database_idvalue under[[d1_databases]]in your wrangler.toml with the database UUID outputted by Terraform. -
Apply the schema migration to the local D1 instance (for local dev tests):
Terminal window npx wrangler d1 execute event-route-optimiser-db --local --file=schema.sql -
Apply the schema migration to the production D1 database in Cloudflare:
Terminal window npx wrangler d1 execute event-route-optimiser-db --remote --file=schema.sql
4. Run & Deploy Worker
-
Run the local dev server:
Terminal window npx wrangler dev -
Deploy the Worker script to the Cloudflare edge network:
Terminal window npx wrangler deploy