@@ -16,17 +16,21 @@ Secrets can be passed into a Container by using [Worker Secrets](/workers/config
16
16
or the [ Secret Store] ( /secrets-store/integrations/workers/ ) , then passing them into the Container
17
17
as environment variables.
18
18
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
20
23
be passing in:
21
24
22
25
- the variable ` "ENV_VAR" ` as a hard-coded environment variable
23
26
- the secret ` "WORKER_SECRET" ` as a secret from Worker Secrets
24
27
- the secret ` "SECRET_STORE_SECRET" ` as a secret from the Secret Store
28
+ - the value ` "KV_VALUE" ` as a value from Workers KV
25
29
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.
28
32
29
- ## Creating secrets
33
+ ## Creating secrets and KV data
30
34
31
35
First, let's create the ` "WORKER_SECRET" ` secret in Worker Secrets:
32
36
@@ -47,12 +51,26 @@ the `"SECRET_STORE_SECRET"` secret to it:
47
51
args = " secrets-store secret create demo --name SECRET_STORE_SECRET --scopes workers --remote"
48
52
/>
49
53
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
+
50
68
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/ ) .
52
70
53
- ## Adding a secrets binding
71
+ ## Adding bindings
54
72
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
56
74
in Wrangler configuration.
57
75
58
76
<WranglerConfig >
@@ -69,6 +87,12 @@ in Wrangler configuration.
69
87
"store_id" : " demo" ,
70
88
"secret_name" : " SECRET_STORE_SECRET"
71
89
}
90
+ ],
91
+ "kv_namespaces" : [
92
+ {
93
+ "binding" : " DEMO_KV" ,
94
+ "id" : " <your-kv-namespace-id>"
95
+ }
72
96
]
73
97
// rest of the configuration...
74
98
}
@@ -79,8 +103,8 @@ in Wrangler configuration.
79
103
Note that ` "WORKER_SECRET" ` does not need to be specified in the Wrangler config file, as it is automatically
80
104
added to ` env ` .
81
105
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.
84
108
85
109
## Using ` envVars ` on the Container class
86
110
@@ -95,7 +119,7 @@ export class MyContainer extends Container {
95
119
envVars = {
96
120
WORKER_SECRET : env .WORKER_SECRET ,
97
121
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
99
123
};
100
124
}
101
125
```
@@ -129,16 +153,20 @@ export default {
129
153
ENV_VAR : env .ENV_VAR + " foo" ,
130
154
WORKER_SECRET : env .WORKER_SECRET ,
131
155
SECRET_STORE_SECRET : await env .SECRET_STORE .get (),
156
+ KV_VALUE : await env .DEMO_KV .get (" KV_VALUE" ),
132
157
},
133
158
},
134
159
});
135
160
136
161
await instanceTwo .startAndWaitForPorts ({
137
162
startOptions: {
138
163
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" ),
142
170
},
143
171
},
144
172
});
@@ -150,6 +178,70 @@ export default {
150
178
};
151
179
```
152
180
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
+
153
245
## Build-time environment variables
154
246
155
247
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