Skip to content

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.

bash
export TF_VAR_cloudflare_api_token="your-cloudflare-token"
export TF_VAR_cloudflare_account_id="your-cloudflare-account-id"

2. Infrastructure Provisioning (Terraform)

  1. Navigate to the infra/ directory:

    bash
    cd infra
  2. Initialize Terraform and fetch the Cloudflare provider:

    bash
    terraform init
  3. Generate a plan to review resource allocations:

    bash
    terraform plan
  4. Deploy the D1 Database and Worker placeholder:

    bash
    terraform apply -auto-approve

    Note: Terraform will output the generated D1 Database ID (UUID).

3. Database Schema Initialization (Wrangler)

  1. Navigate to the api/ directory:

    bash
    cd ../api
  2. Update the database_id value under [[d1_databases]] in your wrangler.toml with the database UUID outputted by Terraform.

  3. Apply the schema migration to the local D1 instance (for local dev tests):

    bash
    npx wrangler d1 execute event-route-optimiser-db --local --file=schema.sql
  4. Apply the schema migration to the production D1 database in Cloudflare:

    bash
    npx wrangler d1 execute event-route-optimiser-db --remote --file=schema.sql

4. Run & Deploy Worker

  1. Run the local dev server:

    bash
    npx wrangler dev
  2. Deploy the Worker script to the Cloudflare edge network:

    bash
    npx wrangler deploy