--- # quotesdb-3euj title: Define Cloudflare D1 database resource and document binding name for the Worker status: completed type: task priority: high created_at: 2026-03-10T23:32:10Z updated_at: 2026-03-10T23:32:19Z blocked_by: - quotesdb-zzm3 - quotesdb-4tec --- Infrastructure is managed with OpenTofu using the Cloudflare provider. Configuration lives in `infra/`. The D1 database must be provisioned before the Worker can bind to it — OpenTofu handles this automatically via the attribute reference in the Worker resource (see triage 07cafb). Triage 5c0c64 resolved: schema is applied via `wrangler d1 execute quotesdb --file infra/schema.sql --remote` as a separate step after `tofu apply`. Do NOT use `null_resource` local-exec. Define the Cloudflare D1 database resource in `infra/d1.tf`. ```hcl # infra/d1.tf # Cloudflare D1 database for the quotesdb application. # SQLite-compatible, bound to the API Worker under the binding name "DB". resource "cloudflare_d1_database" "db" { account_id = var.cloudflare_account_id name = "quotesdb" } # Export the D1 database ID so it can be referenced in worker.tf and # used as an argument to `wrangler d1 execute` for schema migrations. output "d1_database_id" { description = "D1 database ID — referenced by the Worker binding and schema migration commands." value = cloudflare_d1_database.db.id } ``` - `cloudflare_d1_database` outputs `id` (String) — the identifier used in Worker bindings. - The binding name `"DB"` must match what the workers-rs code uses to access the database (set in the API source, not here). - After `tofu apply`, apply the schema: `wrangler d1 execute quotesdb --file infra/schema.sql --remote` (see ticket bb1514 for schema.sql). - The D1 ID showing as `(known after apply)` in `tofu plan` is expected; the Worker binding resolves it at apply time automatically. Run from the `infra/` directory: ```sh tofu validate tofu plan ``` `feat(quotesdb): define Cloudflare D1 database resource in OpenTofu`