Skip to content

Commit 5517573

Browse files
markjmiller1000hzkorinne
authored
docs: add info about workers bundling (cloudflare#23126)
Co-authored-by: Cina Saffary <[email protected]> Co-authored-by: Korinne Alpers <[email protected]>
1 parent 4bd4b2f commit 5517573

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/content/docs/workers/platform/infrastructure-as-code.mdx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@ sidebar:
77

88
import { GitHubCode } from "~/components";
99

10-
Uploading and managing a Worker is easy with [Wrangler](/workers/wrangler/configuration), but sometimes you need to do it more programmatically. You might do this with IaC ("Infrastructure as Code") tools or by calling the [Cloudflare API](/api) directly. Use cases for the API include build and deploy scripts, CI/CD pipelines, custom dev tools, or testing. We provide API SDK libraries for common languages that make interacting with the API easier, such as [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) or [cloudflare-python](https://github.com/cloudflare/cloudflare-python). For IaC, a common tool is HashiCorp's Terraform where you can use the [Cloudflare Provider](/terraform) to create and manage Workers resources.
10+
Uploading and managing Workers is easy with [Wrangler](/workers/wrangler/configuration), but sometimes you need to do it more programmatically. You might do this with IaC ("Infrastructure as Code") tools or by calling the [Cloudflare API](/api) directly. Use cases for the API include build and deploy scripts, CI/CD pipelines, custom dev tools, and testing. We provide API SDK libraries for common languages that make interacting with the API easier, such as [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) and [cloudflare-python](https://github.com/cloudflare/cloudflare-python). For IaC, a common tool is HashiCorp's Terraform. You can use the [Cloudflare Terraform Provider](/terraform) to create and manage Workers resources.
1111

12-
Here are examples of deploying a Worker with common tools and languages. In particular, they highlight how to upload script content and metadata which is different with each approach. Reference the Upload Worker Module API docs [here](/api/resources/workers/subresources/scripts/methods/update).
12+
Here are examples of deploying a Worker with common tools and languages, and considerations for successfully managing Workers with IaC. In particular, the examples highlight how to upload script content and metadata which is different with each approach. Reference the Upload Worker Module API docs [here](/api/resources/workers/subresources/scripts/methods/update) for an exact definition of how script upload works.
1313

14-
All of these examples need an [account id](/fundamentals/account/find-account-and-zone-ids) and [API token](/fundamentals/api/get-started/create-token) (not Global API key) to work.
14+
All of these examples need an [account ID](/fundamentals/account/find-account-and-zone-ids) and [API token](/fundamentals/api/get-started/create-token) (not Global API key) to work.
15+
16+
## Workers Bundling
17+
18+
None of the examples below do [Workers Bundling](/workers/wrangler/bundling) which is usually the function of a tool like Wrangler or [esbuild](https://esbuild.github.io). Generally, you'd run this bundling step before applying your Terraform plan or using the API for script upload:
19+
20+
```bash
21+
wrangler deploy --dry-run -outdir build
22+
```
23+
24+
Then you'd reference the bundled script like `build/index.js`.
25+
26+
:::note
27+
28+
Depending on your Wrangler project and `-outdir`, the name and location of your bundled script might vary.
29+
:::
30+
31+
Make sure to copy all of your config from `wrangler.json` into your Terraform config or API request. This is especially important for compatibility date or compatibility flags your script relies on.
1532

1633
## Terraform
1734

@@ -32,6 +49,7 @@ resource "cloudflare_workers_script" "my-hello-world-script" {
3249
script_name = "my-hello-world-script"
3350
main_module = "my-hello-world-script.mjs"
3451
content = trimspace(file("my-hello-world-script.mjs"))
52+
compatibility_date = "$today"
3553
bindings = [{
3654
name = "MESSAGE"
3755
type = "plain_text"
@@ -88,7 +106,7 @@ curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/scripts/
88106
"text": "Hello World!"
89107
}
90108
],
91-
"compatibility_date": "2025-03-11"
109+
"compatibility_date": "$today"
92110
};type=application/json' \
93111
-F 'my-hello-world-script.mjs=@-;filename=my-hello-world-script.mjs;type=application/javascript+module' <<EOF
94112
export default {
@@ -106,13 +124,13 @@ Change `my-hello-world-script.mjs=@-;` to `my-hello-world-script.mjs=@./my-hello
106124

107125
### Workers for Platforms
108126

109-
With [Workers for Platforms](/cloudflare-for-platforms/workers-for-platforms), you can upload [User Workers](/cloudflare-for-platforms/workers-for-platforms/get-started/user-workers) in a [dispatch namespace](/cloudflare-for-platforms/workers-for-platforms/reference/how-workers-for-platforms-works/#dispatch-namespace). You can use the exact same example above for that if you simply change the url to:
127+
With [Workers for Platforms](/cloudflare-for-platforms/workers-for-platforms), you can upload [User Workers](/cloudflare-for-platforms/workers-for-platforms/get-started/user-workers) in a [dispatch namespace](/cloudflare-for-platforms/workers-for-platforms/reference/how-workers-for-platforms-works/#dispatch-namespace). You can use the exact same example. Simply change the url to:
110128

111129
```bash
112130
curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/dispatch/namespaces/<dispatch_namespace>/scripts/my-hello-world-script
113131
```
114132

115-
For this to work, you will first need to configure [Workers for Platforms](/cloudflare-for-platforms/workers-for-platforms/get-started/configuration), create a dispatch namespace, and replace `<dispatch_namespace>` with your own.
133+
For this to work, you first need to configure [Workers for Platforms](/cloudflare-for-platforms/workers-for-platforms/get-started/configuration), create a dispatch namespace, and replace `<dispatch_namespace>` with your own.
116134

117135
### Python Workers
118136

@@ -131,7 +149,7 @@ curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/scripts/
131149
"text": "Hello World!"
132150
}
133151
],
134-
"compatibility_date": "2025-03-11",
152+
"compatibility_date": "$today",
135153
"compatibility_flags": [
136154
"python_workers"
137155
]

0 commit comments

Comments
 (0)