@@ -13,47 +13,22 @@ import { WranglerConfig, PackageManagers } from "~/components";
1313Environment variables can be passed into a Container using the ` envVars ` field
1414in the ` Container ` class, or by setting manually when the Container starts.
1515
16- Secrets can be passed into a Container by using [ Worker Secrets] ( /workers/configuration/secrets/ )
17- or the [ Secret Store] ( /secrets-store/integrations/workers/ ) , then passing them into the Container
18- as environment variables.
16+ Secrets can be passed into a Container by using [ Worker Secrets] ( /workers/configuration/secrets/ ) ,
17+ then passing them into the Container as environment variables.
1918
2019These examples show the various ways to pass in secrets and environment variables. In each, we will
2120be passing in:
2221
2322- the variable ` "ACCOUNT_NAME" ` as a hard-coded environment variable
24- - the secret ` "CONTAINER_SECRET_KEY" ` as a secret from Worker Secrets
25- - the secret ` "ACCOUNT_API_KEY" ` as a secret from the Secret Store
26-
27- In practice, you may just use one of the methods for storing secrets, but
28- we will show both for completeness.
23+ - the secret ` "ACCOUNT_API_KEY" ` as a secret from Worker Secrets
2924
3025## Creating secrets
3126
32- First, let's create the ` "CONTAINER_SECRET_KEY" ` secret in Worker Secrets:
33-
34- <PackageManagers
35- type = " exec"
36- pkg = " wrangler"
37- args = " secret put CONTAINER_SECRET_KEY"
38- />
39-
40- Then, let's create a store called "demo" in the Secret Store, and add
41- the ` "ACCOUNT_API_KEY" ` secret to it:
42-
43- <PackageManagers
44- type = " exec"
45- pkg = " wrangler"
46- args = " secrets-store store create demo --remote"
47- />
27+ First, let's create the ` "ACCOUNT_API_KEY" ` secret in Worker Secrets:
4828
49- <PackageManagers
50- type = " exec"
51- pkg = " wrangler"
52- args = " secrets-store secret create demo --name ACCOUNT_API_KEY --scopes workers --remote"
53- />
29+ <PackageManagers type = " exec" pkg = " wrangler" args = " secret put ACCOUNT_API_KEY" />
5430
55- For full details on how to create secrets, see the [ Workers Secrets documentation] ( /workers/configuration/secrets/ )
56- and the [ Secret Store documentation] ( /secrets-store/integrations/workers/ ) .
31+ For full details on how to create secrets, see the [ Workers Secrets documentation] ( /workers/configuration/secrets/ ) .
5732
5833## Adding a secrets binding
5934
@@ -66,15 +41,8 @@ in Wrangler configuration.
6641{
6742 "name" : " my-container-worker" ,
6843 "vars" : {
69- "ACCOUNT_NAME" : " my-account"
70- },
71- "secrets_store_secrets" : [
72- {
73- "binding" : " SECRET_STORE" ,
74- "store_id" : " demo" ,
75- "secret_name" : " ACCOUNT_API_KEY"
76- }
77- ]
44+ "ACCOUNT_API_KEY" : " abcdef12345"
45+ }
7846 // rest of the configuration...
7947}
8048```
@@ -92,13 +60,14 @@ or secrets in the container-related portion of wrangler configuration.
9260Now, let's define a Container using the ` envVars ` field in the ` Container ` class:
9361
9462``` js
63+ import { env } from " cloudflare:workers" ;
64+
9565export class MyContainer extends Container {
96- defaultPort = 8080 ;
97- sleepAfter = ' 10s' ;
98- envVars = {
99- ACCOUNT_NAME : env .ACCOUNT_NAME ,
100- ACCOUNT_API_KEY : env .SECRET_STORE .ACCOUNT_API_KEY ,
101- CONTAINER_SECRET_KEY : env .CONTAINER_SECRET_KEY ,
66+ defaultPort = 8080 ;
67+ sleepAfter = " 10s" ;
68+ envVars = {
69+ ACCOUNT_NAME : env .ACCOUNT_NAME ,
70+ ACCOUNT_API_KEY : env .ACCOUNT_API_KEY ,
10271 };
10372}
10473```
@@ -110,14 +79,13 @@ set as environment variables when it launches.
11079
11180But what if you want to set environment variables on a per-instance basis?
11281
113- In this case, set ` manualStart ` then use the ` start ` method to pass in environment variables for each instance.
82+ In this case, use the ` start ` method to pass in environment variables for each instance.
11483We'll assume that we've set additional secrets in the Secret Store.
11584
11685``` js
11786export class MyContainer extends Container {
11887 defaultPort = 8080 ;
11988 sleepAfter = ' 10s' ;
120- manualStart = true ;
12189}
12290
12391export default {
@@ -134,16 +102,14 @@ export default {
134102 await instanceOne .start ({
135103 envVars: {
136104 ACCOUNT_NAME : env .ACCOUNT_NAME + " -1" ,
137- ACCOUNT_API_KEY : env .SECRET_STORE .ACCOUNT_API_KEY_ONE ,
138- CONTAINER_SECRET_KEY : env .CONTAINER_SECRET_KEY_TWO ,
105+ ACCOUNT_API_KEY : env .ACCOUNT_API_KEY_ONE ,
139106 }
140107 )
141108
142109 await instanceTwo .start ({
143110 envVars: {
144111 ACCOUNT_NAME : env .ACCOUNT_NAME + " -2" ,
145- ACCOUNT_API_KEY : env .SECRET_STORE .ACCOUNT_API_KEY_TWO ,
146- CONTAINER_SECRET_KEY : env .CONTAINER_SECRET_KEY_TWO ,
112+ ACCOUNT_API_KEY : env .ACCOUNT_API_KEY_TWO ,
147113 }
148114 )
149115 return new Response (' Container instances launched' );
0 commit comments