{"id":50664759,"url":"https://github.com/marcuwynu23/terraform-cloudflare-d1","last_synced_at":"2026-06-08T05:03:57.411Z","repository":{"id":357492116,"uuid":"1237204035","full_name":"marcuwynu23/terraform-cloudflare-d1","owner":"marcuwynu23","description":"This Terraform project provisions a Cloudflare D1 serverless SQL database using the official Cloudflare Terraform provider.","archived":false,"fork":false,"pushed_at":"2026-05-13T01:39:23.000Z","size":14,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-13T03:24:19.276Z","etag":null,"topics":["cloudflare-d1","cloudflare-provisioning","d1","d1-database","example","provisioning","serverless-database","sql-database","terraform","terraform-cloudflare","terraform-d1-provisioning","terraform-project"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcuwynu23.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-13T01:17:50.000Z","updated_at":"2026-05-13T03:03:24.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/marcuwynu23/terraform-cloudflare-d1","commit_stats":null,"previous_names":["marcuwynu23/terraform-cloudflare-d1"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/marcuwynu23/terraform-cloudflare-d1","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuwynu23%2Fterraform-cloudflare-d1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuwynu23%2Fterraform-cloudflare-d1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuwynu23%2Fterraform-cloudflare-d1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuwynu23%2Fterraform-cloudflare-d1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcuwynu23","download_url":"https://codeload.github.com/marcuwynu23/terraform-cloudflare-d1/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuwynu23%2Fterraform-cloudflare-d1/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34048707,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare-d1","cloudflare-provisioning","d1","d1-database","example","provisioning","serverless-database","sql-database","terraform","terraform-cloudflare","terraform-d1-provisioning","terraform-project"],"created_at":"2026-06-08T05:03:56.993Z","updated_at":"2026-06-08T05:03:57.398Z","avatar_url":"https://github.com/marcuwynu23.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# terraform-cloudflare-d1\n\nThis Terraform project provisions a Cloudflare D1 serverless SQL database using the official Cloudflare Terraform provider.\n\n## Features\n\n- Provisions a Cloudflare D1 database via Infrastructure as Code\n- Configurable database name, jurisdiction, and primary location hint\n- Sensitive API token handling through Terraform variables\n- Outputs database ID and name for downstream use (e.g., Worker bindings)\n\n## Prerequisites\n\n- [Terraform](https://developer.hashicorp.com/terraform/downloads) `\u003e= 1.0`\n- A [Cloudflare account](https://dash.cloudflare.com/sign-up)\n- A Cloudflare API Token with **D1 Edit** permissions\n- Your Cloudflare **Account ID**\n\n### How to get your Cloudflare API Token\n\n1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com/).\n2. Go to **My Profile** → **API Tokens**, or open [this direct link](https://dash.cloudflare.com/profile/api-tokens).\n3. Click **Create Token**.\n4. Under **Custom token**, click **Get started**.\n5. Give the token a descriptive name (e.g., `terraform-d1`).\n6. Under **Permissions**, add:\n   - `Account` → `D1` → `Edit`\n7. Under **Account Resources**, select the account you want this token scoped to (e.g., `Include` → `\u003cYour Account\u003e`).\n8. (Optional) Set **TTL** and **IP Address Filtering** as needed.\n9. Click **Continue to summary** → **Create Token**.\n10. Copy the generated token immediately and store it securely — it won't be shown again.\n\n\u003e Use a scoped API token (D1 Edit only) rather than your Global API Key for least-privilege access.\n\n### How to get your Cloudflare Account ID\n\n**Option A — From the dashboard URL**\n\n1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com/).\n2. Select any domain/account from your home page.\n3. Look at the URL — it will be in the form:\n   `https://dash.cloudflare.com/\u003cACCOUNT_ID\u003e/...`\n4. Copy the `\u003cACCOUNT_ID\u003e` segment.\n\n**Option B — From the Account home page**\n\n1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com/).\n2. Click **Workers \u0026 Pages** (or any domain overview).\n3. On the right sidebar, find **Account ID** and click the copy icon.\n\n**Option C — Via the API**\n\n```bash\ncurl -X GET \"https://api.cloudflare.com/client/v4/accounts\" \\\n  -H \"Authorization: Bearer \u003cYOUR_API_TOKEN\u003e\" \\\n  -H \"Content-Type: application/json\"\n```\n\nThe `result[].id` field in the JSON response is your Account ID.\n\n## Usage\n\n### 1. Clone and configure\n\n```bash\ngit clone https://github.com/marcuwynu23/terraform-cloudflare-d1.git\ncd terraform-cloudflare-d1\ncp terraform.tfvars.example terraform.tfvars\n```\n\n### 2. Set your values in `terraform.tfvars`\n\n```hcl\ncloudflare_api_token  = \"your-api-token-here\"\ncloudflare_account_id = \"your-account-id-here\"\ndatabase_name         = \"my-database\"\n```\n\n### 3. Initialize Terraform\n\n```bash\nterraform init\n```\n\n### 4. Preview changes\n\n```bash\nterraform plan\n```\n\n### 5. Apply\n\n```bash\nterraform apply\n```\n\n### 6. Destroy (when no longer needed)\n\n```bash\nterraform destroy\n```\n\n## Variables\n\n| Name                    | Description                              | Type     | Default       | Required |\n| ----------------------- | ---------------------------------------- | -------- | ------------- | :------: |\n| `cloudflare_api_token`  | Cloudflare API Token with D1 permissions | `string` | n/a           |   yes    |\n| `cloudflare_account_id` | Cloudflare Account ID                    | `string` | n/a           |   yes    |\n| `database_name`         | Name of the D1 database                  | `string` | `my-database` |    no    |\n\n## Outputs\n\n| Name               | Description             |\n| ------------------ | ----------------------- |\n| `d1_database_id`   | UUID of the D1 database |\n| `d1_database_name` | Name of the D1 database |\n\n## Optional Configuration\n\nThe `cloudflare_d1_database` resource supports two optional arguments (commented in `main.tf`):\n\n- `jurisdiction` — restrict data location. Valid values: `eu`, `fedramp`\n- `primary_location_hint` — preferred region. Valid values: `wnam`, `enam`, `weur`, `eeur`, `apac`, `oc`\n\nUncomment and set them in `main.tf` as needed.\n\n## Binding D1 to a Worker\n\nAfter applying, use the output `d1_database_id` in your `wrangler.toml`:\n\n```toml\n[[d1_databases]]\nbinding = \"DB\"\ndatabase_name = \"my-database\"\ndatabase_id = \"\u003cd1_database_id from terraform output\u003e\"\n```\n\n## Testing with the `examples/` folder\n\nThe [`examples/`](./examples) folder contains a Node.js script (`index.js`) that verifies the provisioned D1 database by running full CRUD operations through the Cloudflare REST API using the built-in `fetch`. Environment variables are loaded from `examples/.env` via [`dotenv`](https://www.npmjs.com/package/dotenv).\n\n### Requirements\n\n- Node.js `\u003e= 18` (for native `fetch`)\n\n### 1. Get the D1 database ID from Terraform\n\nAfter `terraform apply`, capture the database UUID:\n\n```bash\nterraform output d1_database_id\n```\n\n### 2. Configure environment variables\n\n```bash\ncd examples\ncp .env.example .env\n```\n\nEdit `examples/.env` and fill in the values:\n\n```dotenv\nCLOUDFLARE_API_TOKEN=your-api-token-here\nCLOUDFLARE_ACCOUNT_ID=your-account-id-here\nCLOUDFLARE_D1_DATABASE_ID=your-d1-database-id-here\n```\n\n\u003e `examples/.env` is gitignored — `.env.example` is committed as a template.\n\n### 3. Install dependencies and run\n\n```bash\ncd examples\nnpm install\nnode index.js\n```\n\nOr using the npm script:\n\n```bash\nnpm start\n```\n\n### What the script does\n\n1. Creates a `users` table if it does not exist\n2. Inserts a demo user\n3. Fetches the user by id\n4. Updates the user\n5. Lists all users\n6. Deletes the demo user\n\nExpected output (abbreviated):\n\n```\n→ Creating table 'users' if not exists...\n→ Creating user: Alice \u003cdemo+...@example.com\u003e\nCreated: { id: 1, name: 'Alice', ... }\n→ Fetching user id=1\nFetched: { id: 1, ... }\n→ Updating user id=1\nUpdated: { id: 1, name: 'Alice Updated', ... }\n→ Listing users\nAll users: [ { ... } ]\n→ Deleting user id=1\nDeleted rows: 1\n```\n\n## Security Notes\n\n- `terraform.tfvars` and `*.tfstate` files are gitignored — never commit secrets\n- The API token variable is marked `sensitive` to prevent accidental log exposure\n- Use a scoped API token (D1 Edit only) rather than a Global API Key\n\n## References\n\n- [Cloudflare D1 documentation](https://developers.cloudflare.com/d1/)\n- [Cloudflare Terraform provider](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs)\n- [cloudflare_d1_database resource](https://developers.cloudflare.com/api/terraform/resources/d1/subresources/database/)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcuwynu23%2Fterraform-cloudflare-d1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcuwynu23%2Fterraform-cloudflare-d1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcuwynu23%2Fterraform-cloudflare-d1/lists"}