|
| 1 | +--- |
| 2 | +title: You can now deploy full-stack apps on Workers using Terraform |
| 3 | +description: The Cloudflare Terraform provider v5.11.0 now lets you define a directory of static assets, and handles the heavy lifting of uploading them and detecting changes. |
| 4 | +products: |
| 5 | + - workers |
| 6 | +date: 2025-10-09 |
| 7 | +--- |
| 8 | +import { Code } from 'astro-expressive-code/components'; |
| 9 | + |
| 10 | +You can now upload Workers with [static assets](/workers/static-assets/) (like HTML, CSS, JavaScript, images) with the [Cloudflare Terraform provider v5.11.0](https://registry.terraform.io/providers/cloudflare/cloudflare/5.11.0/docs), making it even easier to deploy and manage full-stack apps with IaC. |
| 11 | + |
| 12 | +**Previously**, you couldn't use Terraform to upload static assets without writing custom scripts to handle generating an [asset manifest](/workers/static-assets/direct-upload/#upload-manifest), calling the [Cloudflare API to upload assets in chunks](/workers/static-assets/direct-upload/#upload-static-assets), and handling change detection. |
| 13 | + |
| 14 | +**Now**, you simply define the directory where your assets are built, and we handle the rest. Check out the [examples](/changelog/#examples) for what this looks like in Terraform configuration. |
| 15 | + |
| 16 | +You can get started today with [the Cloudflare Terraform provider (v5.11.0)](https://registry.terraform.io/providers/cloudflare/cloudflare/5.11.0/docs), using either the existing [`cloudflare_workers_script` resource](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script), or the beta [`cloudflare_worker_version` resource](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/worker_version). |
| 17 | + |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +#### With `cloudflare_workers_script` |
| 22 | + |
| 23 | +Here's how you can use the existing [`cloudflare_workers_script`](https://registry.terraform.io/providers/cloudflare/cloudflare/5.11.0/docs/resources/workers_script) resource to upload your Worker code and assets in one shot. |
| 24 | + |
| 25 | + |
| 26 | +```hcl |
| 27 | +resource "cloudflare_workers_script" "my_app" { |
| 28 | + account_id = var.account_id |
| 29 | + script_name = "my-app" |
| 30 | +
|
| 31 | + content_file = "./dist/worker/index.js" |
| 32 | + content_sha256 = filesha256("./dist/worker/index.js") |
| 33 | + main_module = "index.js" |
| 34 | +
|
| 35 | + # Just point to your assets directory - that's it! |
| 36 | + assets = { |
| 37 | + directory = "./dist/static" |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +#### With `cloudflare_worker`, `cloudflare_worker_version`, and `cloudflare_workers_deployment` |
| 43 | + |
| 44 | +And here's an example using the beta [`cloudflare_worker_version`](https://registry.terraform.io/providers/cloudflare/cloudflare/5.11.0/docs/resources/worker_version) resource, alongside the [`cloudflare_worker`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/worker) and [`cloudflare_workers_deployment`](https://registry.terraform.io/providers/cloudflare/cloudflare/5.11.0/docs/resources/workers_deployment) resources: |
| 45 | + |
| 46 | +```hcl |
| 47 | +
|
| 48 | +# This tracks the existence of your Worker, so that you |
| 49 | +# can upload code and assets separately from tracking Worker state. |
| 50 | +
|
| 51 | +resource "cloudflare_worker" "my_app" { |
| 52 | + account_id = var.account_id |
| 53 | + name = "my-app" |
| 54 | +} |
| 55 | +
|
| 56 | +resource "cloudflare_worker_version" "my_app_version" { |
| 57 | + account_id = var.account_id |
| 58 | + worker_id = cloudflare_worker.my_app.id |
| 59 | +
|
| 60 | + # Just point to your assets directory - that's it! |
| 61 | + assets = { |
| 62 | + directory = "./dist/static" |
| 63 | + } |
| 64 | +
|
| 65 | + modules = [{ |
| 66 | + name = "index.js" |
| 67 | + content_file = "./dist/worker/index.js" |
| 68 | + content_type = "application/javascript+module" |
| 69 | + }] |
| 70 | +} |
| 71 | +
|
| 72 | +resource "cloudflare_workers_deployment" "my_app_deployment" { |
| 73 | + account_id = var.account_id |
| 74 | + script_name = cloudflare_worker.my_app.name |
| 75 | +
|
| 76 | + strategy = "percentage" |
| 77 | + versions = [{ |
| 78 | + version_id = cloudflare_worker_version.my_app_version.id |
| 79 | + percentage = 100 |
| 80 | + }] |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## What's changed |
| 85 | +Under the hood, the Cloudflare Terraform provider now handles the same logic that Wrangler uses for static asset uploads. This includes scanning your assets directory, computing hashes for each file, generating a manifest with file metadata, and calling the Cloudflare API to upload any missing files in chunks. We support large directories with parallel uploads and chunking, and when the asset manifest hash changes, we detect what's changed and trigger an upload for *only* those changed files. |
| 86 | + |
| 87 | + |
| 88 | +## Try it out |
| 89 | +- Get started with [the Cloudflare Terraform provider (v5.11.0)](https://registry.terraform.io/providers/cloudflare/cloudflare/5.11.0/docs) |
| 90 | +- You can use either the existing [`cloudflare_workers_script` resource](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script) to upload your Worker code and assets in one resource. |
| 91 | +- Or you can use the new beta [`cloudflare_worker_version` resource](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/worker_version) (along with the [`cloudflare_worker`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/worker) and [`cloudflare_workers_deployment`](https://registry.terraform.io/providers/cloudflare/cloudflare/5.11.0/docs/resources/workers_deployment)) resources to more granularly control the lifecycle of each Worker resource. |
0 commit comments