|
| 1 | +--- |
| 2 | +title: Improvements to the Workers Terraform resources and SDKs |
| 3 | +description: Multiple improvements to the Workers Terraform resources and SDKs including fixes for plan diffs, Durable Objects migrations, file uploads, and new Python Workers support. |
| 4 | +products: |
| 5 | + - workers |
| 6 | +date: 2025-07-28 |
| 7 | +--- |
| 8 | + |
| 9 | +The recent [Cloudflare Terraform Provider](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script) and SDK releases (such as [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript)) bring significant improvements to the Workers developer experience. These updates focus on reliability, performance, and adding Python Workers support. |
| 10 | + |
| 11 | +## Terraform Improvements |
| 12 | + |
| 13 | +### Fixed Unwarranted Plan Diffs |
| 14 | + |
| 15 | +Resolved several issues with the `cloudflare_workers_script` resource that resulted in unwarranted plan diffs, including: |
| 16 | + |
| 17 | +- Using Durable Objects migrations |
| 18 | +- Using some bindings such as `secret_text` |
| 19 | +- Using smart placement |
| 20 | +- When the API returned array attribute results in different order |
| 21 | + |
| 22 | +A resource should never show a plan diff if there wasn't an actual change. This fix reduces distracting noise in your Terraform plan. |
| 23 | + |
| 24 | +Available in Cloudflare Terraform Provider 5.8.0. |
| 25 | + |
| 26 | +### Fixed Durable Objects Migration Issues |
| 27 | + |
| 28 | +Resolved an issue in `cloudflare_workers_script` when using Durable Objects migrations failed on the second terraform apply. Configurations like this would work on the first Terraform apply, but fail on subsequent ones: |
| 29 | + |
| 30 | +```tf |
| 31 | +resource "cloudflare_workers_script" "my_worker" { |
| 32 | + account_id = "123456789" |
| 33 | + script_name = "my_worker" |
| 34 | + # ... |
| 35 | + bindings = [ |
| 36 | + { |
| 37 | + name = "MY_DO" |
| 38 | + type = "durable_object_namespace" |
| 39 | + class_name = "MyDOClass" |
| 40 | + } |
| 41 | + ] |
| 42 | + |
| 43 | + migrations = { |
| 44 | + new_tag = "v1" |
| 45 | + new_sqlite_classes = ["MyDOClass"] |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +This is now fixed and available in Cloudflare Terraform Provider 5.8.0. |
| 51 | + |
| 52 | +### Improved File Management |
| 53 | + |
| 54 | +You can now specify `content_file = "worker.js"` and `content_sha256 = filesha256("worker.js")` instead of `content = file("worker.js")`. This prevents the Workers script content from being stored in the state file, which greatly reduces plan diff size and noise. If your workflow synced plans remotely, this will happen much faster. This is available in Cloudflare Terraform Provider 5.7.0. |
| 55 | + |
| 56 | +### Assets Headers and Redirects Support |
| 57 | + |
| 58 | +Fixed the `cloudflare_workers_script` resource to properly support headers and redirects for Assets: |
| 59 | + |
| 60 | +```tf |
| 61 | +resource "cloudflare_workers_script" "my_worker" { |
| 62 | + account_id = "123456789" |
| 63 | + script_name = "my_worker" |
| 64 | + content_file = "worker.js" |
| 65 | + content_sha256 = filesha256("worker.js") |
| 66 | + assets = { |
| 67 | + config = { |
| 68 | + headers = file("headers") |
| 69 | + redirects = file("redirects") |
| 70 | + } |
| 71 | + # Completion jwt from: |
| 72 | + # https://developers.cloudflare.com/api/resources/workers/subresources/assets/subresources/upload/ |
| 73 | + jwt = "jwt" |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Available in Cloudflare Terraform Provider 5.8.0. |
| 79 | + |
| 80 | +### Python Workers Support |
| 81 | + |
| 82 | +Added support for uploading [Python Workers](/workers/languages/python/) (beta) in Terraform. You can now deploy Python Workers with: |
| 83 | + |
| 84 | +```tf |
| 85 | +resource "cloudflare_workers_script" "my_worker" { |
| 86 | + account_id = "123456789" |
| 87 | + script_name = "my_worker" |
| 88 | + content_file = "worker.py" |
| 89 | + content_sha256 = filesha256("worker.py") |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Available in Cloudflare Terraform Provider 5.8.0. |
| 94 | + |
| 95 | +## SDK Enhancements |
| 96 | + |
| 97 | +### Improved File Upload API |
| 98 | + |
| 99 | +Fixed an issue where Workers script versions in the SDK did not allow uploading files. The SDK now provides an improved Workers files upload API: |
| 100 | + |
| 101 | +```js |
| 102 | +const scriptContent = ` |
| 103 | + export default { |
| 104 | + async fetch(request, env, ctx) { |
| 105 | + return new Response('Hello World!', { status: 200 }); |
| 106 | + } |
| 107 | + }; |
| 108 | +`; |
| 109 | + |
| 110 | +const script = await client.workers.scripts.update('my-worker', { |
| 111 | + account_id: '123456789', |
| 112 | + metadata: { |
| 113 | + main_module: 'my-script', |
| 114 | + }, |
| 115 | + files: [ |
| 116 | + await toFile(Buffer.from(scriptContent), 'my-script', { |
| 117 | + type: 'application/javascript+module', |
| 118 | + }), |
| 119 | + ], |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +Available in cloudflare-typescript 4.6.0. |
| 124 | + |
| 125 | +### Fixed updating KV values |
| 126 | + |
| 127 | +Previously when create a KV value like this: |
| 128 | + |
| 129 | +```js |
| 130 | +await cf.kv.namespaces.values.update("my-kv-namespace", "key1", { |
| 131 | + account_id: "123456789", |
| 132 | + metadata: "my metadata", |
| 133 | + value: JSON.stringify({ |
| 134 | + hello: "world" |
| 135 | + }) |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +And recalled it in your Worker like this: |
| 140 | +```ts |
| 141 | +const value = await c.env.KV.get<{hello: string}>("key1", "json"); |
| 142 | +``` |
| 143 | + |
| 144 | +You'd get back this: `{metadata:'my metadata', value:"{'hello':'world'}"}` instead of the correct value of `{hello: 'world'}` |
| 145 | + |
| 146 | +This is fixed in cloudflare-typescript 4.5.0. |
0 commit comments