|
1 | 1 | --- |
2 | | -title: Fixes to Workers in Terraform, SDKs, and APIs. |
| 2 | +title: Automate Worker deployments with a simplified SDK and more reliable Terraform provider |
3 | 3 | description: Various fixes and improvements have been made to Workers Terraform resources, SDKs, APIs, and API docs. |
4 | 4 | products: |
5 | 5 | - d1 |
6 | 6 | - workers |
7 | 7 | - workers-for-platforms |
8 | | -date: 2025-06-17T00:00:00Z |
| 8 | +date: 2025-06-19T14:00:00Z |
9 | 9 | --- |
10 | 10 |
|
11 | | -Fixes and improvements have been made to Workers Terraform resources, SDKs, APIs, and API docs that unblock users from configuring Workers and resources on the Developer Platform for build and deploy scripts, CI/CD pipelines, custom dev tools, testing, etc. |
| 11 | +import { TypeScriptExample } from "~/components"; |
12 | 12 |
|
13 | | -## New Workers Script Upload Interface in SDKs |
| 13 | +## Simplified Worker Deployments with our SDKs |
14 | 14 |
|
15 | | -In [cloudlfare-typescript](https://github.com/cloudflare/cloudflare-typescript) (4.4.1) and [cloudflare-python](https://github.com/cloudflare/cloudflare-python) (4.3.1), there is a fixed (and improved!) interface for uploading Workers. Check out the examples [here](/workers/platform/infrastructure-as-code). |
| 15 | +We've simplified the programmatic deployment of Workers via our [Cloudflare SDKs](/fundamentals/api/reference/sdks/). This update abstracts away the low-level complexities of the `multipart/form-data` upload process, allowing you to focus on your code while we handle the deployment mechanics. |
16 | 16 |
|
17 | | -## Terraform |
| 17 | +This new interface is available in: |
18 | 18 |
|
19 | | -- Fixed the [cloudflare_workers_script](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script) resource in Terraform producing a diff even when there are no changes. |
20 | | -- Fixed the [cloudflare_workers_for_platforms_dispatch_namespace](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_for_platforms_dispatch_namespace) resource in Terraform always attempting to recreate. |
21 | | -- The [cloudflare_workers_route](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_route) resource now allows for the `script` property to be empty, null, or omitted to indicate that pattern should be negated for all scripts (see routes [docs](/workers/configuration/routing/routes)). |
22 | | -- Using `primary_location_hint` in the [cloudflare_d1_database](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/d1_database) resource will no longer always try to recreate. |
| 19 | +- [cloudlfare-typescript](https://github.com/cloudflare/cloudflare-typescript) (4.4.1) |
| 20 | +- [cloudflare-python](https://github.com/cloudflare/cloudflare-python) (4.3.1) |
23 | 21 |
|
24 | | -## API |
| 22 | +For complete examples, see our guide on [programmatic Worker deployments](/workers/platform/infrastructure-as-code). |
25 | 23 |
|
26 | | -- Added missing [Workers Script And Version Settings](/api/resources/workers/subresources/scripts/subresources/script_and_version_settings) to the OpenAPI docs and API SDKs. |
| 24 | +### The Old way: Manual API calls |
| 25 | + |
| 26 | +Previously, deploying a Worker programmatically required manually constructing a `multipart/form-data` HTTP request, packaging your code and a separate `metadata.json` file. This was more complicated and verbose, and prone to formatting errors. |
| 27 | + |
| 28 | +For example, here's how you would upload a Worker script previously with cURL: |
| 29 | + |
| 30 | +```bash |
| 31 | +curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/scripts/my-hello-world-script \ |
| 32 | + -X PUT \ |
| 33 | + -H 'Authorization: Bearer <api_token>' \ |
| 34 | + -F 'metadata={ |
| 35 | + "main_module": "my-hello-world-script.mjs", |
| 36 | + "bindings": [ |
| 37 | + { |
| 38 | + "type": "plain_text", |
| 39 | + "name": "MESSAGE", |
| 40 | + "text": "Hello World!" |
| 41 | + } |
| 42 | + ], |
| 43 | + "compatibility_date": "$today" |
| 44 | + };type=application/json' \ |
| 45 | + -F 'my-hello-world-script.mjs=@-;filename=my-hello-world-script.mjs;type=application/javascript+module' <<EOF |
| 46 | +export default { |
| 47 | + async fetch(request, env, ctx) { |
| 48 | + return new Response(env.MESSAGE, { status: 200 }); |
| 49 | + } |
| 50 | +}; |
| 51 | +EOF |
| 52 | +``` |
| 53 | + |
| 54 | +### After: SDK interface |
| 55 | + |
| 56 | +With the new SDK interface, you can now define your entire Worker configuration using a single, structured object. |
| 57 | + |
| 58 | +This approach allows you to specify metadata like `main_module`, `bindings`, and `compatibility_date` as top-level properties directly alongside your script content. Our SDK takes this logical object and automatically constructs the complex multipart/form-data API request behind the scenes. |
| 59 | + |
| 60 | +Here's how you can now programmatically deploy a Worker via the [`cloudlfare-typescript` SDK](https://github.com/cloudflare/cloudflare-typescript) |
| 61 | + |
| 62 | +<TypeScriptExample> |
| 63 | +```ts |
| 64 | +const script = await client.workers.scripts.update(scriptName, { |
| 65 | + account_id: accountID, |
| 66 | + main_module: `${scriptName}.mjs`, |
| 67 | + body: scriptContent, |
| 68 | + bindings: [ |
| 69 | + {}, |
| 70 | + ], |
| 71 | +}); |
| 72 | +```` |
| 73 | + |
| 74 | +</TypeScriptExample> |
| 75 | + |
| 76 | +## Terraform provider improvements |
| 77 | + |
| 78 | +We've also made several fixes and enhancements to the [Cloudflare Terraform provider](https://github.com/cloudflare/terraform-provider-cloudflare): |
| 79 | + |
| 80 | +- Fixed the [`cloudflare_workers_script`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script) resource in Terraform, which previously was producing a diff even when there were no changes. Now, your `terraform plan` outputs will be cleaner and more reliable. |
| 81 | +- Fixed the [`cloudflare_workers_for_platforms_dispatch_namespace`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_for_platforms_dispatch_namespace), where the provider would attempt to recreate the namespace on a `terraform apply`. The resource now correctly reads its remote state, ensuring stability for production environments and CI/CD workflows. |
| 82 | +- The [`cloudflare_workers_route`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_route) resource now allows for the `script` property to be empty, null, or omitted to indicate that pattern should be negated for all scripts (see routes [docs](/workers/configuration/routing/routes)). You can now reserve a pattern or temporarily disable a Worker on a route without deleting the route definition itself. |
| 83 | +- Using `primary_location_hint` in the [`cloudflare_d1_database`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/d1_database) resource will no longer always try to recreate. You can now safely change the location hint for a D1 database without causing a destructive operation. |
| 84 | + |
| 85 | +## API improvements |
| 86 | + |
| 87 | +We've also properly documented the [Workers Script And Version Settings](/api/resources/workers/subresources/scripts/subresources/script_and_version_settings) in our public OpenAPI spec and SDKs. |
0 commit comments