You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.0 KiB

+++ title = "Define Cloudflare D1 database resource and document binding name for the Worker" priority = 7 status = "todo" ticket_type = "task" dependencies = ["2d1371", "5c0c64"] +++

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 }

</implementation>

<notes>
- `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.
</notes>

<validation>
Run from the `infra/` directory:

```sh
tofu validate
tofu plan
`feat(quotesdb): define Cloudflare D1 database resource in OpenTofu`