Skip to content

Commit 5c69bbd

Browse files
author
Chris Thorwarth
committed
chore: updating worker secrets tf docs
1 parent 7f208ab commit 5c69bbd

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/content/docs/workers/configuration/secrets.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { Render,DashButton } from "~/components";
1111

1212
Secrets are a type of binding that allow you to attach encrypted text values to your Worker. Secrets are used for storing sensitive information like API keys and auth tokens. Secrets are programmatically available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/), and may also be accessible via [`process.env`](/workers/configuration/environment-variables) in Workers that support Node.js compatibility.
1313

14+
:::note[Secrets are bindings, not separate resources]
15+
16+
Secrets are not managed as standalone resources. Instead, they are implemented as bindings attached to your Worker or Worker version. When using tools like Wrangler, the CLI handles this binding automatically. When using Infrastructure as Code tools like [Terraform](/workers/platform/infrastructure-as-code/) or the [Cloudflare API](/api/resources/workers/) directly, secrets must be explicitly included as `secret_text` bindings on your `cloudflare_workers_script` or `cloudflare_worker_version` resources.
17+
18+
:::
19+
1420
## Access your secrets with Workers
1521

1622
Secrets can be accessed from Workers as you would any other [environment variables](/workers/configuration/environment-variables/). For instance, given a `DB_CONNECTION_STRING` secret, you can access it in your Worker code:

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,113 @@ resource "cloudflare_workers_deployment" "my_worker_deployment" {
417417
```
418418

419419
To make this succeed, you first have to comment out the `durable_object` binding block, apply the plan, uncomment it, comment out the `migrations` block, then apply again. This time the plan will succeed. This also applies to the API or SDKs. This is an example where it makes sense to just manage the `cloudflare_worker` and/or `cloudflare_workers_deployment` resources while using Wrangler for build and Version management.
420+
421+
## Considerations with Secrets
422+
423+
[Secrets](/workers/configuration/secrets/) are not managed as standalone resources in Terraform or the API. Instead, they must be included as bindings on your Worker script or version.
424+
425+
### Secret Text Binding
426+
427+
When using Terraform or the API, use a `secret_text` binding to attach encrypted secrets to your Worker:
428+
429+
<Tabs syncKey="terraform-worker-resource">
430+
<TabItem label="cloudflare_workers_script">
431+
432+
```tf
433+
variable "account_id" {
434+
type = string
435+
}
436+
437+
variable "api_key" {
438+
type = string
439+
sensitive = true
440+
}
441+
442+
resource "cloudflare_workers_script" "example_script" {
443+
account_id = var.account_id
444+
name = "my-script"
445+
content = file("worker.js")
446+
447+
bindings = [{
448+
type = "secret_text"
449+
name = "API_KEY"
450+
text = var.api_key
451+
}]
452+
}
453+
```
454+
455+
</TabItem>
456+
<TabItem label="cloudflare_worker_version">
457+
458+
```tf
459+
variable "account_id" {
460+
type = string
461+
}
462+
463+
variable "api_key" {
464+
type = string
465+
sensitive = true
466+
}
467+
468+
resource "cloudflare_worker" "example" {
469+
account_id = var.account_id
470+
name = "my-worker"
471+
}
472+
473+
resource "cloudflare_worker_version" "example_version" {
474+
account_id = var.account_id
475+
worker_id = cloudflare_worker.example.id
476+
compatibility_date = "2025-08-06"
477+
main_module = "worker.js"
478+
479+
modules = [{
480+
name = "worker.js"
481+
content_type = "application/javascript+module"
482+
content_file = "worker.js"
483+
}]
484+
485+
bindings = [{
486+
type = "secret_text"
487+
name = "API_KEY"
488+
text = var.api_key
489+
}]
490+
}
491+
```
492+
493+
</TabItem>
494+
</Tabs>
495+
496+
The secret will be encrypted and accessible in your Worker via `env.API_KEY`.
497+
498+
### Secrets Store
499+
500+
For centralized secrets management across multiple Workers, [Secrets Store](/secrets-store/) (beta) provides account-level secrets. While Secrets Store can currently be accessed from Workers using `secrets_store_secret` bindings, a dedicated Terraform resource for managing Secrets Store is planned for GA, tentatively targeting Q1 2026.
501+
502+
In the meantime, you can reference an existing Secrets Store managed outside of Terraform:
503+
504+
```tf
505+
variable "store_id" {
506+
type = string
507+
description = "ID of your Secrets Store resource"
508+
}
509+
510+
resource "cloudflare_worker_version" "example_version" {
511+
account_id = var.account_id
512+
worker_id = cloudflare_worker.example.id
513+
compatibility_date = "2025-08-06"
514+
main_module = "worker.js"
515+
516+
modules = [{
517+
name = "worker.js"
518+
content_type = "application/javascript+module"
519+
content_file = "worker.js"
520+
}]
521+
522+
bindings = [{
523+
type = "secrets_store_secret"
524+
name = "MY_SECRET"
525+
secret_name = "my-secret"
526+
store_id = var.store_id
527+
}]
528+
}
529+
```

0 commit comments

Comments
 (0)