@@ -19,25 +19,21 @@ as environment variables.
1919These examples show the various ways to pass in secrets and environment variables. In each, we will
2020be passing in:
2121
22- - the variable ` "ACCOUNT_NAME " ` as a hard-coded environment variable
23- - the secret ` "CONTAINER_SECRET_KEY " ` as a secret from Worker Secrets
24- - the secret ` "ACCOUNT_API_KEY " ` as a secret from the Secret Store
22+ - the variable ` "ENV_VAR " ` as a hard-coded environment variable
23+ - the secret ` "WORKER_SECRET " ` as a secret from Worker Secrets
24+ - the secret ` "SECRET_STORE_SECRET " ` as a secret from the Secret Store
2525
2626In practice, you may just use one of the methods for storing secrets, but
2727we will show both for completeness.
2828
2929## Creating secrets
3030
31- First, let's create the ` "CONTAINER_SECRET_KEY " ` secret in Worker Secrets:
31+ First, let's create the ` "WORKER_SECRET " ` secret in Worker Secrets:
3232
33- <PackageManagers
34- type = " exec"
35- pkg = " wrangler"
36- args = " secret put CONTAINER_SECRET_KEY"
37- />
33+ <PackageManagers type = " exec" pkg = " wrangler" args = " secret put WORKER_SECRET" />
3834
3935Then, let's create a store called "demo" in the Secret Store, and add
40- the ` "ACCOUNT_API_KEY " ` secret to it:
36+ the ` "SECRET_STORE_SECRET " ` secret to it:
4137
4238<PackageManagers
4339 type = " exec"
@@ -48,7 +44,7 @@ the `"ACCOUNT_API_KEY"` secret to it:
4844<PackageManagers
4945 type = " exec"
5046 pkg = " wrangler"
51- args = " secrets-store secret create demo --name ACCOUNT_API_KEY --scopes workers --remote"
47+ args = " secrets-store secret create demo --name SECRET_STORE_SECRET --scopes workers --remote"
5248/>
5349
5450For full details on how to create secrets, see the [ Workers Secrets documentation] ( /workers/configuration/secrets/ )
@@ -65,13 +61,13 @@ in Wrangler configuration.
6561{
6662 "name" : " my-container-worker" ,
6763 "vars" : {
68- "ACCOUNT_NAME " : " my-account "
64+ "ENV_VAR " : " my-env-var "
6965 },
7066 "secrets_store_secrets" : [
7167 {
7268 "binding" : " SECRET_STORE" ,
7369 "store_id" : " demo" ,
74- "secret_name" : " ACCOUNT_API_KEY "
70+ "secret_name" : " SECRET_STORE_SECRET "
7571 }
7672 ]
7773 // rest of the configuration...
@@ -80,11 +76,11 @@ in Wrangler configuration.
8076
8177</WranglerConfig >
8278
83- Note that ` "CONTAINER_SECRET_KEY " ` does not need to be set, at it is automatically
79+ Note that ` "WORKER_SECRET " ` does not need to be specified in the Wrangler config file, as it is automatically
8480added to ` env ` .
8581
8682Also note that we did not configure anything specific for environment variables
87- or secrets in the container-related portion of wrangler configuration.
83+ or secrets in the _ container-related _ portion of the Wrangler configuration file .
8884
8985## Using ` envVars ` on the Container class
9086
@@ -97,9 +93,9 @@ export class MyContainer extends Container {
9793 defaultPort = 8080 ;
9894 sleepAfter = " 10s" ;
9995 envVars = {
100- ACCOUNT_NAME : env .ACCOUNT_NAME ,
101- ACCOUNT_API_KEY : env .SECRET_STORE . ACCOUNT_API_KEY ,
102- CONTAINER_SECRET_KEY : env . CONTAINER_SECRET_KEY ,
96+ WORKER_SECRET : env .WORKER_SECRET ,
97+ 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
10399 };
104100}
105101```
@@ -112,7 +108,6 @@ set as environment variables when it launches.
112108But what if you want to set environment variables on a per-instance basis?
113109
114110In this case, use the ` startAndWaitForPorts() ` method to pass in environment variables for each instance.
115- We'll assume that we've set additional secrets in the Secret Store.
116111
117112``` js
118113export class MyContainer extends Container {
@@ -124,26 +119,26 @@ export default {
124119 async fetch (request , env ) {
125120 if (new URL (request .url ).pathname === " /launch-instances" ) {
126121 let instanceOne = env .MY_CONTAINER .getByName (" foo" );
127- let instanceTwo = env .MY_CONTAINER .getByName (" foo " );
122+ let instanceTwo = env .MY_CONTAINER .getByName (" bar " );
128123
129124 // Each instance gets a different set of environment variables
130125
131126 await instanceOne .startAndWaitForPorts ({
132127 startOptions: {
133128 envVars: {
134- ACCOUNT_NAME : env .ACCOUNT_NAME + " -1 " ,
135- ACCOUNT_API_KEY : env .SECRET_STORE . ACCOUNT_API_KEY_ONE ,
136- CONTAINER_SECRET_KEY : env .CONTAINER_SECRET_KEY_TWO ,
129+ ENV_VAR : env .ENV_VAR + " foo " ,
130+ WORKER_SECRET : env .WORKER_SECRET ,
131+ SECRET_STORE_SECRET : await env .SECRET_STORE . get () ,
137132 },
138133 },
139134 });
140135
141136 await instanceTwo .startAndWaitForPorts ({
142137 startOptions: {
143138 envVars: {
144- 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 ,
139+ ENV_VAR : env .ENV_VAR + " foo " ,
140+ WORKER_SECRET : env .ANOTHER_WORKER_SECRET ,
141+ SECRET_STORE_SECRET : await env .OTHER_SECRET_STORE . get () ,
147142 },
148143 },
149144 });
0 commit comments