-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Updates to environment variables docs (managing multiple environments) #20728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: production
Are you sure you want to change the base?
Changes from 2 commits
4a30651
6fb549f
abc2294
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,16 +3,15 @@ pcx_content_type: configuration | |
| title: Environment variables | ||
| head: [] | ||
| description: Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker | ||
|
|
||
| --- | ||
|
|
||
| import { Render, TabItem, Tabs, WranglerConfig } from "~/components" | ||
| import { Render, TabItem, Tabs, Aside, WranglerConfig } from "~/components"; | ||
|
|
||
| ## Background | ||
|
|
||
| Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker. Environment variables are available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/). | ||
| Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker. They are pass in via the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) in your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/). | ||
|
|
||
| Text strings and JSON values are not encrypted and are useful for storing application configuration. | ||
| Text strings and JSON values are **not encrypted** and are useful for storing application configuration. If you need to store sensitive information (such as API keys or tokens), use [secrets](/workers/configuration/secrets/). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this "not encrypted" language was here before, but I'd love if we could provide some more details here around encryption behaviour—are these encrypted at rest? |
||
|
|
||
| ## Add environment variables via Wrangler | ||
|
|
||
|
|
@@ -26,31 +25,37 @@ Refer to the following example on how to access the `API_HOST` environment varia | |
|
|
||
| ```js | ||
| export default { | ||
| async fetch(request, env, ctx) { | ||
| return new Response(`API host: ${env.API_HOST}`); | ||
| } | ||
| } | ||
| async fetch(request, env, ctx) { | ||
| return new Response(`API host: ${env.API_HOST}`); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| </TabItem> <TabItem label="TypeScript" icon="seti:typescript"> | ||
|
|
||
| ```ts | ||
| export interface Env { | ||
| API_HOST: string; | ||
| API_HOST: string; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request, env, ctx): Promise<Response> { | ||
| return new Response(`API host: ${env.API_HOST}`); | ||
| } | ||
| async fetch(request, env, ctx): Promise<Response> { | ||
| return new Response(`API host: ${env.API_HOST}`); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
|
|
||
| </TabItem> </Tabs> | ||
|
|
||
| `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. | ||
| <Aside type="note"> | ||
|
|
||
| **`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. | ||
|
|
||
| </Aside> | ||
|
|
||
| ## Managing environment variables across multiple environments | ||
|
|
||
| To define environment variables for different environments, refer to the example below: | ||
| Since `vars` is a [non-inheritable key](/workers/wrangler/configuration/#non-inheritable-keys), you need to explicitly define your environment variables for each environment (for example, `staging` and `production`). | ||
|
|
||
| <WranglerConfig> | ||
|
|
||
|
|
@@ -69,15 +74,19 @@ SERVICE_X_DATA = { URL = "service-x-api.prod.example", MY_ID = 456 } | |
| ``` | ||
|
|
||
| </WranglerConfig> | ||
|
|
||
| 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. | ||
| ### Overriding environment variables during local development | ||
| When running [`wrangler dev`](/workers/wrangler/commands/#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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also have per-environment dev.vars files, which might be useful to mention here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There info in the partial |
||
|
|
||
| ```shell | ||
| API_HOST = "localhost:4000" | ||
| API_ACCOUNT_ID = "local_example_user" | ||
| ``` | ||
|
|
||
| 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`. | ||
| 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: | ||
|
|
||
| ```sh | ||
| wrangler dev --env=local | ||
| ``` | ||
|
|
||
| ## Add environment variables via the dashboard | ||
|
|
||
|
|
@@ -89,19 +98,17 @@ To add environment variables via the dashboard: | |
| 4. Select **Settings**. | ||
| 5. Under **Variables and Secrets**, select **Add**. | ||
| 6. Select a **Type**, input a **Variable name**, and input its **Value**. This variable will be made available to your Worker. | ||
| 7. (Optional) To add multiple environment variables, select **Add variable**. | ||
| 7. (_Optional_) To add multiple environment variables, select **Add variable**. | ||
| 8. Select **Deploy** to implement your changes. | ||
|
|
||
| :::caution[Plaintext strings and secrets] | ||
|
|
||
|
|
||
| Select the **Secret** type if your environment variable is a [secret](/workers/configuration/secrets/). | ||
|
|
||
|
|
||
| ::: | ||
|
|
||
| <Render file="env_and_secrets" /> | ||
|
|
||
| ## Related resources | ||
|
|
||
| * Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience. | ||
| - Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,30 @@ | ||
| --- | ||
| {} | ||
|
|
||
| --- | ||
|
|
||
| ## Compare secrets and environment variables | ||
| ## Secrets vs Environment Variables | ||
|
|
||
| :::caution[Use secrets for sensitive information] | ||
|
|
||
| **Never store sensitive information in plaintext environment variables**. Always use [secrets](/workers/configuration/secrets/) for data like passwords or API tokens. | ||
| ::: | ||
|
|
||
| ### When to use [secrets](/workers/configuration/secrets/) | ||
|
|
||
| Do not use plaintext environment variables to store sensitive information. Use [secrets](/workers/configuration/secrets/) instead. | ||
| If your environment variable is a secret (such as a password or API token), select the **Secret** type when adding it via the dashboard or use [Wrangler's built-in](/workers/configuration/secrets/#secrets-on-deployed-workers) command: | ||
|
|
||
| ```sh | ||
| wrangler secret put <KEY> | ||
| ``` | ||
|
|
||
| ::: | ||
| Secrets function similarly to environment variables in a Worker, but with crucial differences: | ||
|
|
||
| - **Visibility:** Once you define a secret, its value is no longer visible in Wrangler or the Cloudflare dashboard. | ||
|
|
||
| - **Security:** Sensitive data, such as passwords and tokens, should always be encrypted to prevent accidental exposure. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "should always be encrypted" is a bit confusing here. That kinda implies that the user needs to encrypt it, but I think you're saying that sensitive data should be a secret because secrets are encrypted? |
||
|
|
||
| To your Worker, **there is no difference between an environment variable and a secret.** The secret's value is passed through as defined. | ||
|
|
||
| ### When to use [plaintext environment variables](/workers/configuration/environment-variables) | ||
|
|
||
| [Secrets](/workers/configuration/secrets/) are [environment variables](/workers/configuration/environment-variables/). The difference is secret values are not visible within Wrangler or Cloudflare dashboard after you define them. This means that sensitive data, including passwords or API tokens, should always be encrypted to prevent data leaks. To your Worker, there is no difference between an environment variable and a secret. The secret's value is passed through as defined. | ||
| Plaintext environment variables are best for non-sensitive configuration details, such as hostnames and IDs. These are values that **do not** require encryption because leaking them does not compromise security or privacy. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know plaintext is a term of art here, but is there for potential for confusion given env vars can also be json, not just text? |
||
Uh oh!
There was an error while loading. Please reload this page.