|
| 1 | +--- |
| 2 | +title: New Workers API that allows you to explicitly manage Workers, Versions, and Deployments |
| 3 | +description: New Workers API, SDK methods, and Terraform resources for explicitly managing Workers, Versions, and Deployments |
| 4 | +date: 2025-08-11 |
| 5 | +--- |
| 6 | + |
| 7 | +The current Workers script upload API and its corresponding SDK methods (i.e. [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) and [cloudflare-python](https://github.com/cloudflare/cloudflare-python)) and Terraform resource ([cloudflare_workers_script](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script)) are convenient for a one-shot Workers deployment, but sometimes that does too much. This API unavoidably creates a Version and a Deployment (see [Versions and Deployments](/workers/configuration/versions-and-deployments/)). However, these are separate resources you can individually manage, and sometimes you want more control over how and when these are created. |
| 8 | + |
| 9 | +In [Cloudflare Terraform Provider](https://registry.terraform.io/providers/cloudflare/cloudflare/5.9.0/docs) 5.9.0 and [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) 4.6.0 (TODO double check these versions), we're introducing a clean, separated API that makes Workers, Versions, and Deployments individual "capital R" resources with clear concerns. It's now possible to create a Worker entity--and reference it elsewhere--before uploading code, bindings, or making it routable from the internet. You'll also enjoy a more ergonomic modules upload interface vs the old multipart/form-data API. |
| 10 | + |
| 11 | +This example uses the [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) SDK library. |
| 12 | + |
| 13 | +```javascript |
| 14 | +const scriptContent = ` |
| 15 | + export default { |
| 16 | + async fetch(request, env, ctx) { |
| 17 | + return new Response(env.MESSAGE, { status: 200 }); |
| 18 | + } |
| 19 | + }; |
| 20 | +`; |
| 21 | + |
| 22 | +/** |
| 23 | + * Create a Worker and set non-versioned settings like observability |
| 24 | + */ |
| 25 | +const worker = await client.workers.create("my-worker", { |
| 26 | + account_id: "...", |
| 27 | + observability: { |
| 28 | + enabled: true, |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +/** |
| 33 | + * Create the first version of the Worker |
| 34 | + * This is where code and bindings are defined and can be different between versions |
| 35 | + */ |
| 36 | +const version = await client.workers.versions.create(worker.id, { |
| 37 | + account_id: "...", |
| 38 | + main_module: "my-worker-script", |
| 39 | + compatibility_date: $today, |
| 40 | + bindings: [ /*...*/ ], |
| 41 | + modules: [ |
| 42 | + { |
| 43 | + name: "my-worker-script", |
| 44 | + content_type: "application/javascript+module", |
| 45 | + content_base64: Buffer.from(scriptContent).toString("base64"), |
| 46 | + }, |
| 47 | + ], |
| 48 | +}); |
| 49 | + |
| 50 | +/** |
| 51 | + * Create a deployment and point all traffic to the version we created |
| 52 | + */ |
| 53 | +const deployment = await client.workers.scripts.deployments.create(worker.name, { |
| 54 | + account_id: "...", |
| 55 | + strategy: "percentage", |
| 56 | + versions: [ |
| 57 | + { |
| 58 | + percentage: 100, |
| 59 | + version_id: version.id, |
| 60 | + }, |
| 61 | + ], |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +In Terraform, you now have the option to just manage the Worker entity. You don't need to manage code, config, or deployments in your plan. This gives you flexibility to use Wrangler for build and deploy while maintaining IaC presence. |
| 66 | + |
| 67 | +```tf |
| 68 | +resource "cloudflare_worker" "my_worker" { |
| 69 | + account_id = "..." |
| 70 | + name = "my-important-service" |
| 71 | +} |
| 72 | +
|
| 73 | +# Manage these here or outside of Terraform |
| 74 | +# resource "cloudflare_workers_version" "my_worker_version" {} |
| 75 | +# resource "cloudflare_workers_deployment" "my_worker_deployment" {} |
| 76 | +``` |
| 77 | + |
| 78 | +Check out [Infrastructure as Code (IaC)](/workers/platform/infrastructure-as-code) for more details and examples. |
0 commit comments