Skip to content

Commit ddb8f33

Browse files
committed
[Workers] Add getting started guide for API and IaC
1 parent 031f2c8 commit ddb8f33

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Workers
4+
external_link: /workers/platform/infrastructure-as-code/
5+
sidebar:
6+
order: 12
7+
---

src/content/docs/workers/configuration/multipart-upload-metadata.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ If you're using the [Workers Script Upload API](/api/resources/workers/subresour
2424
}
2525
```
2626

27+
:::note
28+
29+
See examples of metadata being used with the Workers Script Upload API [here](/workers/platform/infrastructure-as-code).
30+
:::
31+
2732
## Attributes
2833

2934
The following attributes are configurable at the top-level.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: Infrastructure as Code (IaC)
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 11
6+
---
7+
8+
import { GitHubCode } from "~/components";
9+
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](https://developers.cloudflare.com/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.
11+
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](https://developers.cloudflare.com/api/resources/workers/subresources/scripts/methods/update/).
13+
14+
All of these examples need an [account id](/fundamentals/setup/find-account-and-zone-ids) and [API token](/fundamentals/api/get-started/create-token) (not Global API key) to work.
15+
16+
## Terraform
17+
18+
In this example, you need a local file named `my-hello-world-script.mjs` with script content similar to the above examples. Replace `account_id` with your own. Learn more about the Cloudflare Terraform Provider [here](/terraform), and see an example with all the Workers script resource settings [here](https://github.com/cloudflare/terraform-provider-cloudflare/blob/main/examples/resources/cloudflare_workers_script/resource.tf).
19+
20+
```tf
21+
terraform {
22+
required_providers {
23+
cloudflare = {
24+
source = "cloudflare/cloudflare"
25+
version = "~> 5"
26+
}
27+
}
28+
}
29+
30+
resource "cloudflare_workers_script" "my-hello-world-script" {
31+
account_id = "<replace_me>"
32+
script_name = "my-hello-world-script"
33+
main_module = "my-hello-world-script.mjs"
34+
content = trimspace(file("my-hello-world-script.mjs"))
35+
bindings = [{
36+
name = "MESSAGE"
37+
type = "plain_text"
38+
text = "Hello World!"
39+
}]
40+
}
41+
```
42+
43+
:::note
44+
45+
- `trimspace()` removes empty lines in the file
46+
- The Workers Script resource does not have a `metadata` property like in the other examples. All of the properties found in `metadata` are instead at the top-level of the resource class, such as `bindings` or `compatibility_date`. Please see the [cloudflare_workers_script (Resource) docs](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script).
47+
:::
48+
49+
## Cloudflare API Libraries
50+
51+
### JavaScript/TypeScript
52+
53+
This example uses the [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) library which provides convenient access to the Cloudflare REST API from server-side JavaScript or TypeScript.
54+
55+
<GitHubCode
56+
repo="cloudflare/cloudflare-typescript"
57+
file="examples/workers/script-upload.ts"
58+
commit="f30592e4d10fd9090999bc63b36ca7e4eaf66f74"
59+
lang="ts"
60+
code={{
61+
collapse: "1-13",
62+
}}
63+
/>
64+
65+
### Python
66+
67+
This example uses the [cloudflare-python](https://github.com/cloudflare/cloudflare-python) library.
68+
69+
<GitHubCode
70+
repo="cloudflare/cloudflare-python"
71+
file="examples/workers/script-upload.py"
72+
commit="8845a22af23075ae88496bc7187471485b944048"
73+
lang="py"
74+
code={{
75+
collapse: "1-12",
76+
}}
77+
/>
78+
79+
## Cloudflare REST API
80+
81+
Open a terminal or create a shell script to upload a Worker easily with curl. For this example, replace `<account_id>` and `<api_token>` with your own. What's notable about interacting with the Workers Script Upload API directly is that it uses [multipart/form-data](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/POST) for uploading metadata, multiple JavaScript modules, source maps, and more. This is abstracted away in Terraform and the API libraries.
82+
83+
```bash
84+
curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/scripts/my-hello-world-script \
85+
-X PUT \
86+
-H 'Authorization: Bearer <api_token>' \
87+
-F 'metadata={
88+
"main_module": "my-hello-world-script.mjs",
89+
"bindings": [
90+
{
91+
"type": "plain_text",
92+
"name": "MESSAGE",
93+
"text": "Hello World!"
94+
}
95+
],
96+
"compatibility_date": "2025-03-11"
97+
};type=application/json' \
98+
-F 'my-hello-world-script.mjs=@-;filename=my-hello-world-script.mjs;type=application/javascript+module' <<EOF
99+
export default {
100+
async fetch(request, env, ctx) {
101+
return new Response(env.MESSAGE, { status: 200 });
102+
}
103+
};
104+
EOF
105+
```
106+
107+
:::note
108+
109+
Change `my-hello-world-script.mjs=@-;` to `my-hello-world-script.mjs=@./my-hello-world-script.mjs;` and remove everything after and including `<<EOF` to upload a local file named `my-hello-world-script.mjs` instead.
110+
:::

0 commit comments

Comments
 (0)