You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`vars` is a non-inheritable key. [Non-inheritable keys](/workers/wrangler/configuration/#non-inheritable-keys) are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
50
+
### Configuring different environments in Wrangler
51
+
52
+
[Environments in Wrangler](/workers/wrangler/environments) let you specify different configurations for the same Worker, including different values for `vars` in each environment.
53
+
As `vars` is a [non-inheritable key](/workers/wrangler/configuration/#non-inheritable-keys), they are not inherited by environments and must be specified for each environment.
52
54
53
-
To define environment variables for different environments, refer to the example below:
55
+
The example below sets up two environments, `staging` and `production`, with different values for `API_HOST`.
For local development with `wrangler dev`, variables in the [Wrangler configuration file](/workers/wrangler/configuration/) are automatically overridden by any values defined in a `.dev.vars` file located in the root directory of your worker. This is useful for providing values you do not want to check in to source control.
75
+
To run Wrangler commands in specific environments, you can pass in the `--env` or `-e` flag. For example, you can develop the Worker in an environment called `staging` by running `npx wrangler dev --env staging`, and deploy it with `npx wrangler deploy --env staging`.
74
76
75
-
```shell
76
-
API_HOST = "localhost:4000"
77
-
API_ACCOUNT_ID = "local_example_user"
78
-
```
79
-
80
-
Alternatively, you can specify per-environment values in the [Wrangler configuration file](/workers/wrangler/configuration/) and provide an `environment` value via the `env` flag when developing locally like so `wrangler dev --env=local`.
77
+
Learn about [environments in Wrangler](/workers/wrangler/environments).
81
78
82
79
## Add environment variables via the dashboard
83
80
@@ -94,14 +91,14 @@ To add environment variables via the dashboard:
94
91
95
92
:::caution[Plaintext strings and secrets]
96
93
97
-
98
94
Select the **Secret** type if your environment variable is a [secret](/workers/configuration/secrets/).
99
95
100
-
101
96
:::
102
97
103
98
<Renderfile="env_and_secrets" />
104
99
100
+
<Renderfile="secrets-in-dev" />
101
+
105
102
## Related resources
106
103
107
-
* Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
104
+
- Migrating environment variables from [Service Worker format to ES modules syntax](/workers/reference/migrate-to-module-workers/#environment-variables).
Copy file name to clipboardExpand all lines: src/content/docs/workers/wrangler/configuration.mdx
+13-12Lines changed: 13 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,10 +52,10 @@ kv_namespaces = [
52
52
53
53
## Environments
54
54
55
-
The configuration for a Worker can become complex when you define different [environments](/workers/wrangler/environments/), and each environment has its own configuration.
56
-
There is a default (top-level) environment and named environments that provide environment-specific configuration.
55
+
You can define different configurations for a Worker using Wrangler [environments](/workers/wrangler/environments/).
56
+
There is a default (top-level) environment and you can create named environments that provide environment-specific configuration.
57
57
58
-
These are defined under `[env.name]` keys, such as `[env.staging]` which you can then preview or deploy with the `-e` / `--env` flag in the `wrangler` commands like `npx wrangler deploy --env staging`.
58
+
These are defined under `[env.<name>]` keys, such as `[env.staging]` which you can then preview or deploy with the `-e` / `--env` flag in the `wrangler` commands like `npx wrangler deploy --env staging`.
59
59
60
60
The majority of keys are inheritable, meaning that top-level configuration can be used in environments. [Bindings](/workers/runtime-apis/bindings/), such as `vars` or `kv_namespaces`, are not inheritable and need to be defined explicitly.
61
61
@@ -646,7 +646,7 @@ id = "<ID>"
646
646
647
647
</WranglerConfig>
648
648
649
-
### Images
649
+
### Images
650
650
651
651
[Cloudflare Images](/images/transform-images/transform-via-workers/) lets you make transformation requests to optimize, resize, and manipulate images stored in remote sources.
652
652
@@ -867,6 +867,7 @@ To bind other Workers to your Worker, assign an array of the below object to the
- To bind to a Worker in a specific [environment](/workers/wrangler/environments), you need to append the environment name to the Worker name. This should be in the format `<worker-name>-<environment-name>`. For example, to bind to a Worker called `worker-name` in its `staging` environment, `service` should be set to `worker-name-staging`.
Copy file name to clipboardExpand all lines: src/content/docs/workers/wrangler/environments.mdx
+61-34Lines changed: 61 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,38 +2,37 @@
2
2
pcx_content_type: concept
3
3
title: Environments
4
4
head: []
5
-
description: Deploy the same Worker application with different configuration for
6
-
each environment.
5
+
description: Use environments to create different configurations for the same Worker application.
7
6
---
8
7
9
8
import { WranglerConfig } from"~/components";
10
9
11
-
## Background
12
-
13
-
Wrangler allows you to deploy the same Worker application with different configuration for each environment. You must configure environments in your Worker application's Wrangler file.
10
+
Wrangler allows you to use environments to create different configurations for the same Worker application. Environments are configured in the Worker's [Wrangler configuration file](/workers/wrangler/configuration/).
11
+
There is a default (top-level) environment and you can create named environments that provide environment-specific configuration.
14
12
15
13
Review the following environments flow:
16
14
17
-
1. You have created a Worker application named `my-worker`.
18
-
2. You create an environment, for example, `dev`, in the Worker's [Wrangler configuration file](/workers/wrangler/configuration/).
19
-
3. In the Wrangler configuration file, you configure the `dev` environment by [adding bindings](/workers/runtime-apis/bindings/) and/or [routes](/workers/configuration/routing/routes/).
20
-
4. You deploy the Worker using `npx wrangler deploy -e dev`.
21
-
5. In the background, Wrangler creates a new Worker named `my-worker-dev`.
22
-
6. You can now change your `my-worker` Worker code and configuration, and choose which environment to deploy your changes to.
23
-
24
-
Environments are used with the `--env` or `-e` flag on `wrangler dev`, `npx wrangler deploy`, and `wrangler secret`.
15
+
1. Create a Worker, named `my-worker` for example.
16
+
2. Create an environment, for example `dev`, in the Worker's [Wrangler configuration file](/workers/wrangler/configuration/), by adding a `[env.<ENV_NAME>]` section.
25
17
26
-
## Configuration
18
+
<WranglerConfig>
27
19
28
-
To create an environment:
20
+
```json
21
+
{
22
+
"name": "my-worker",
23
+
"env": {
24
+
"<ENV_NAME>": {
25
+
// environment-specific configuration goes here
26
+
}
27
+
}
28
+
}
29
+
```
29
30
30
-
1. Open your Worker's Wrangler file.
31
-
2. Add `[env.<NAME>]` and change `<NAME>` to the desired name of your environment.
32
-
3. Repeat step 2 to create multiple environments.
31
+
</WranglerConfig>
33
32
34
-
Be careful when naming your environments that they do not contain sensitive information, such as, `migrating-service-from-company1-to-company2` or `company1-acquisition-load-test`.
33
+
3. You can configure the `dev` environment with different values to the top-level environment. Refer [here](/workers/wrangler/configuration/#environments) for how different options are inherited - or not inherited - between environments.
35
34
36
-
Review the layout of an example `[env.dev]` environment that sets up a custom `dev.example.com` route:
35
+
For example, to set a different route for a Worker in the `dev` environment:
37
36
38
37
<WranglerConfig>
39
38
@@ -46,23 +45,21 @@ route = "dev.example.com"
46
45
```
47
46
48
47
</WranglerConfig>
48
+
4. Environments are used with the `--env` or `-e` flag on Wrangler commands. For example, you can develop the Worker in the `dev` environment by running `npx wrangler dev -e=dev`, and deploy it with `npx wrangler deploy -e=dev`.
49
49
50
-
You cannot specify multiple environments with the same name.
51
-
52
-
Wrangler appends the environment name to the top-level name to deploy a Worker. For example, a Worker project named `my-worker` with an environment `[env.dev]` would deploy a Worker named `my-worker-dev`.
50
+
:::note
53
51
54
-
After you have configured your environment, run `npx wrangler deploy` in your Worker project directory for the changes to take effect.
52
+
Cloudflare effectively creates a new Worker for you when you create a Worker with an environment. Wrangler appends the environment name to the top-level name. For example, a Worker project named `my-worker` with an environment `dev` would deploy a Worker named `my-worker-dev`. You must use this name when referencing a Worker in a particular environment, for example in a [service binding](/workers/wrangler/configuration/#service-bindings).
53
+
:::
55
54
56
55
## Non-inheritable keys and environments
57
56
58
57
[Non-inheritable keys](/workers/wrangler/configuration/#non-inheritable-keys) are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
59
58
60
-
[Bindings](/workers/runtime-apis/bindings/) and [environment variables](/workers/configuration/environment-variables/) must be specified per each[environment](/workers/wrangler/environments/) in your [Wrangler configuration file](/workers/wrangler/configuration/).
59
+
For example, [bindings](/workers/runtime-apis/bindings/) and [environment variables](/workers/configuration/environment-variables/)are non-inheritable, and must be specified per [environment](/workers/wrangler/environments/) in your [Wrangler configuration file](/workers/wrangler/configuration/).
61
60
62
61
Review the following example Wrangler file:
63
62
64
-
65
-
66
63
<WranglerConfig>
67
64
68
65
```toml
@@ -85,7 +82,41 @@ kv_namespaces = [
85
82
86
83
</WranglerConfig>
87
84
88
-
You may assign environment-specific [secrets](/workers/configuration/secrets/) by running the command [`wrangler secret put <KEY> -env`](/workers/wrangler/commands/#put).
85
+
### Service bindings
86
+
87
+
To use a [service binding](/workers/wrangler/configuration/#service-bindings) that targets a Worker in a specific environment, you need to append the environment name to the target Worker name in the `service` field. This should be in the format `<worker-name>-<environment-name>`.
88
+
In the example below, we have two Workers, both with a `staging` environment. `worker-b` has a service binding to `worker-a`. Note how the `service` field in the `staging` environment points to `worker-a-staging`, whereas the top-level service binding points to `worker-a`.
89
+
90
+
<WranglerConfig>
91
+
92
+
```toml
93
+
name = "worker-a"
94
+
95
+
vars = { FOO = "<top-level-var>" }
96
+
[env.staging.vars]
97
+
FOO = "<staging-var>"
98
+
```
99
+
100
+
</WranglerConfig>
101
+
102
+
<WranglerConfig>
103
+
104
+
```toml
105
+
name = "worker-b"
106
+
107
+
services = { binding = "<BINDING_NAME>", service = "worker-a" }
108
+
109
+
# Note how `service = "worker-a-staging"`
110
+
env.staging.service ={ binding = "<BINDING_NAME>", service = "worker-a-staging" }
111
+
```
112
+
113
+
</WranglerConfig>
114
+
115
+
### Secrets
116
+
117
+
You may assign environment-specific [secrets](/workers/configuration/secrets/) by running the command [`wrangler secret put <KEY> -env`](/workers/wrangler/commands/#put). You can also create `dotenv` type files named `.dev.vars.<environment-name>`.
118
+
119
+
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
89
120
90
121
---
91
122
@@ -95,8 +126,6 @@ You may assign environment-specific [secrets](/workers/configuration/secrets/) b
95
126
96
127
The following Wrangler file adds two environments, `[env.staging]` and `[env.production]`, to the Wrangler file. If you are deploying to a [Custom Domain](/workers/configuration/routing/custom-domains/) or [route](/workers/configuration/routing/routes/), you must provide a [`route` or `routes` key](/workers/wrangler/configuration/) for each environment.
97
128
98
-
99
-
100
129
<WranglerConfig>
101
130
102
131
```toml
@@ -118,7 +147,7 @@ routes = [
118
147
119
148
</WranglerConfig>
120
149
121
-
In order to use environments with this configuration, you can pass the name of the environment via the `--env` flag.
150
+
You can pass the name of the environment via the `--env` flag to run commands in a specific environment.
122
151
123
152
With this configuration, Wrangler will behave in the following manner:
124
153
@@ -168,8 +197,6 @@ if (ENVIRONMENT === "staging") {
168
197
169
198
To deploy your code to your `*.workers.dev` subdomain, include `workers_dev = true` in the desired environment. Your Wrangler file may look like this:
170
199
171
-
172
-
173
200
<WranglerConfig>
174
201
175
202
```toml
@@ -206,6 +233,6 @@ Published my-worker
206
233
207
234
:::caution
208
235
209
-
When you create a Worker via an environment, Cloudflare automatically creates an SSL certification for it. SSL certifications are discoverable and a matter of public record.
236
+
When you create a Worker via an environment, Cloudflare automatically creates an SSL certification for it. SSL certifications are discoverable and a matter of public record. Be careful when naming your environments that they do not contain sensitive information, such as, `migrating-service-from-company1-to-company2` or `company1-acquisition-load-test`.
Copy file name to clipboardExpand all lines: src/content/partials/workers/secrets-in-dev.mdx
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,5 @@
1
1
---
2
2
{}
3
-
4
3
---
5
4
6
5
When developing your Worker or Pages Function, create a `.dev.vars` file in the root of your project to define secrets that will be used when running `wrangler dev` or `wrangler pages dev`, as opposed to using environment variables in the [Wrangler configuration file](/workers/configuration/environment-variables/#compare-secrets-and-environment-variables). This works both in local and remote development modes.
@@ -12,6 +11,6 @@ SECRET_KEY="value"
12
11
API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
13
12
```
14
13
15
-
To set different secrets for each environment, create files named `.dev.vars.<environment-name>`. When you use `wrangler <command> --env <environment-name>`, the corresponding environment-specific file will be loaded instead of the `.dev.vars` file, so the two files are not merged.
14
+
To set different secrets for each environment, create files named `.dev.vars.<environment-name>`. When you use `wrangler <command> --env <environment-name>`, the corresponding environment-specific file will be loaded instead of the `.dev.vars` file.
16
15
17
16
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
0 commit comments