Skip to content

Commit d38cec4

Browse files
authored
docs: add KV values support to containers env-vars-and-secrets example (#25406)
- Add KV namespace creation instructions alongside secrets setup - Update Wrangler configuration to include kv_namespaces binding - Enhance Container class examples to demonstrate KV value usage - Add dedicated section with KV usage patterns (configuration data, feature flags) - Update per-instance examples to show reading different KV keys - Maintain consistent documentation style and formatting
1 parent 582f2c0 commit d38cec4

File tree

1 file changed

+105
-13
lines changed

1 file changed

+105
-13
lines changed

src/content/docs/containers/examples/env-vars-and-secrets.mdx

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ Secrets can be passed into a Container by using [Worker Secrets](/workers/config
1616
or the [Secret Store](/secrets-store/integrations/workers/), then passing them into the Container
1717
as environment variables.
1818

19-
These examples show the various ways to pass in secrets and environment variables. In each, we will
19+
KV values can be passed into a Container by using [Workers KV](/kv/), then reading
20+
the values and passing them into the Container as environment variables.
21+
22+
These examples show the various ways to pass in secrets, KV values, and environment variables. In each, we will
2023
be passing in:
2124

2225
- the variable `"ENV_VAR"` as a hard-coded environment variable
2326
- the secret `"WORKER_SECRET"` as a secret from Worker Secrets
2427
- the secret `"SECRET_STORE_SECRET"` as a secret from the Secret Store
28+
- the value `"KV_VALUE"` as a value from Workers KV
2529

26-
In practice, you may just use one of the methods for storing secrets, but
27-
we will show both for completeness.
30+
In practice, you may just use one of the methods for storing secrets and data, but
31+
we will show all methods for completeness.
2832

29-
## Creating secrets
33+
## Creating secrets and KV data
3034

3135
First, let's create the `"WORKER_SECRET"` secret in Worker Secrets:
3236

@@ -47,12 +51,26 @@ the `"SECRET_STORE_SECRET"` secret to it:
4751
args="secrets-store secret create demo --name SECRET_STORE_SECRET --scopes workers --remote"
4852
/>
4953

54+
Next, let's create a KV namespace called `DEMO_KV` and add a key-value pair:
55+
56+
<PackageManagers
57+
type="exec"
58+
pkg="wrangler"
59+
args="kv namespace create DEMO_KV"
60+
/>
61+
62+
<PackageManagers
63+
type="exec"
64+
pkg="wrangler"
65+
args="kv key put --binding DEMO_KV KV_VALUE 'Hello from KV!'"
66+
/>
67+
5068
For full details on how to create secrets, see the [Workers Secrets documentation](/workers/configuration/secrets/)
51-
and the [Secret Store documentation](/secrets-store/integrations/workers/).
69+
and the [Secret Store documentation](/secrets-store/integrations/workers/). For KV setup, see the [Workers KV documentation](/kv/).
5270

53-
## Adding a secrets binding
71+
## Adding bindings
5472

55-
Next, we need to add bindings to access our secrets and environment variables
73+
Next, we need to add bindings to access our secrets, KV values, and environment variables
5674
in Wrangler configuration.
5775

5876
<WranglerConfig>
@@ -69,6 +87,12 @@ in Wrangler configuration.
6987
"store_id": "demo",
7088
"secret_name": "SECRET_STORE_SECRET"
7189
}
90+
],
91+
"kv_namespaces": [
92+
{
93+
"binding": "DEMO_KV",
94+
"id": "<your-kv-namespace-id>"
95+
}
7296
]
7397
// rest of the configuration...
7498
}
@@ -79,8 +103,8 @@ in Wrangler configuration.
79103
Note that `"WORKER_SECRET"` does not need to be specified in the Wrangler config file, as it is automatically
80104
added to `env`.
81105

82-
Also note that we did not configure anything specific for environment variables
83-
or secrets in the _container-related_ portion of the Wrangler configuration file.
106+
Also note that we did not configure anything specific for environment variables,
107+
secrets, or KV values in the _container-related_ portion of the Wrangler configuration file.
84108

85109
## Using `envVars` on the Container class
86110

@@ -95,7 +119,7 @@ export class MyContainer extends Container {
95119
envVars = {
96120
WORKER_SECRET: env.WORKER_SECRET,
97121
ENV_VAR: env.ENV_VAR,
98-
// we can't set the secret store binding as a default here, as getting the secret value is asynchronous
122+
// we can't set the secret store binding or KV values as defaults here, as getting their values is asynchronous
99123
};
100124
}
101125
```
@@ -129,16 +153,20 @@ export default {
129153
ENV_VAR: env.ENV_VAR + "foo",
130154
WORKER_SECRET: env.WORKER_SECRET,
131155
SECRET_STORE_SECRET: await env.SECRET_STORE.get(),
156+
KV_VALUE: await env.DEMO_KV.get("KV_VALUE"),
132157
},
133158
},
134159
});
135160

136161
await instanceTwo.startAndWaitForPorts({
137162
startOptions: {
138163
envVars: {
139-
ENV_VAR: env.ENV_VAR + "foo",
140-
WORKER_SECRET: env.ANOTHER_WORKER_SECRET,
141-
SECRET_STORE_SECRET: await env.OTHER_SECRET_STORE.get(),
164+
ENV_VAR: env.ENV_VAR + "bar",
165+
WORKER_SECRET: env.WORKER_SECRET,
166+
SECRET_STORE_SECRET: await env.SECRET_STORE.get(),
167+
KV_VALUE: await env.DEMO_KV.get("KV_VALUE"),
168+
// You can also read different KV keys for different instances
169+
INSTANCE_CONFIG: await env.DEMO_KV.get("instance-bar-config"),
142170
},
143171
},
144172
});
@@ -150,6 +178,70 @@ export default {
150178
};
151179
```
152180

181+
## Reading KV values in containers
182+
183+
KV values are particularly useful for configuration data that changes infrequently but needs to be accessible to your containers. Since KV operations are asynchronous, you must read the values at runtime when starting containers.
184+
185+
Here are common patterns for using KV with containers:
186+
187+
### Configuration data
188+
189+
```js
190+
export default {
191+
async fetch(request, env) {
192+
if (new URL(request.url).pathname === "/configure-container") {
193+
// Read configuration from KV
194+
const config = await env.DEMO_KV.get("container-config", "json");
195+
const apiUrl = await env.DEMO_KV.get("api-endpoint");
196+
197+
let container = env.MY_CONTAINER.getByName("configured");
198+
199+
await container.startAndWaitForPorts({
200+
startOptions: {
201+
envVars: {
202+
CONFIG_JSON: JSON.stringify(config),
203+
API_ENDPOINT: apiUrl,
204+
DEPLOYMENT_ENV: await env.DEMO_KV.get("deployment-env"),
205+
},
206+
},
207+
});
208+
209+
return new Response("Container configured and launched");
210+
}
211+
},
212+
};
213+
```
214+
215+
### Feature flags
216+
217+
```js
218+
export default {
219+
async fetch(request, env) {
220+
if (new URL(request.url).pathname === "/launch-with-features") {
221+
// Read feature flags from KV
222+
const featureFlags = {
223+
ENABLE_FEATURE_A: await env.DEMO_KV.get("feature-a-enabled"),
224+
ENABLE_FEATURE_B: await env.DEMO_KV.get("feature-b-enabled"),
225+
DEBUG_MODE: await env.DEMO_KV.get("debug-enabled"),
226+
};
227+
228+
let container = env.MY_CONTAINER.getByName("features");
229+
230+
await container.startAndWaitForPorts({
231+
startOptions: {
232+
envVars: {
233+
...featureFlags,
234+
CONTAINER_VERSION: "1.2.3",
235+
},
236+
},
237+
});
238+
239+
return new Response("Container launched with feature flags");
240+
}
241+
},
242+
};
243+
```
244+
153245
## Build-time environment variables
154246

155247
Finally, you can also set build-time environment variables that are only available when building the container image via the `image_vars` field in the Wrangler configuration.

0 commit comments

Comments
 (0)