node-env-resolver-aws 12.0.1 → 13.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +313 -159
- package/dist/chunk-6KYWBBFW.js +9 -0
- package/dist/chunk-T5KL7JTC.cjs +9 -0
- package/dist/handlers.cjs +1 -0
- package/dist/handlers.d.cts +52 -0
- package/dist/handlers.d.ts +52 -0
- package/dist/handlers.js +1 -0
- package/dist/index.cjs +4 -1
- package/dist/index.d.cts +3 -12
- package/dist/index.d.ts +3 -12
- package/dist/index.js +4 -1
- package/package.json +23 -13
- package/binding.gyp +0 -9
- package/index.js +0 -1
package/README.md
CHANGED
|
@@ -18,20 +18,26 @@ npm install node-env-resolver/aws
|
|
|
18
18
|
import { resolveSsm, resolveSecrets } from 'node-env-resolver-aws';
|
|
19
19
|
|
|
20
20
|
// Resolve from SSM Parameter Store
|
|
21
|
-
const config = await resolveSsm(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
const config = await resolveSsm(
|
|
22
|
+
{
|
|
23
|
+
path: '/myapp/config',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
API_ENDPOINT: string(),
|
|
27
|
+
TIMEOUT: 30,
|
|
28
|
+
}
|
|
29
|
+
);
|
|
27
30
|
|
|
28
31
|
// Resolve from Secrets Manager
|
|
29
|
-
const secrets = await resolveSecrets(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
const secrets = await resolveSecrets(
|
|
33
|
+
{
|
|
34
|
+
secretId: 'myapp/production/secrets',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
DATABASE_URL: string(),
|
|
38
|
+
API_KEY: string(),
|
|
39
|
+
}
|
|
40
|
+
);
|
|
35
41
|
```
|
|
36
42
|
|
|
37
43
|
### Using with resolveAsync() (for combining multiple sources)
|
|
@@ -42,14 +48,20 @@ import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
|
42
48
|
|
|
43
49
|
const config = await resolveAsync({
|
|
44
50
|
resolvers: [
|
|
45
|
-
[
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
[
|
|
52
|
+
awsSecrets({ secretId: 'myapp/production/secrets' }),
|
|
53
|
+
{
|
|
54
|
+
DATABASE_URL: postgres(),
|
|
55
|
+
API_KEY: string(),
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
awsSsm({ path: '/myapp/config' }),
|
|
60
|
+
{
|
|
61
|
+
TIMEOUT: 30,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
],
|
|
53
65
|
});
|
|
54
66
|
```
|
|
55
67
|
|
|
@@ -58,11 +70,97 @@ const config = await resolveAsync({
|
|
|
58
70
|
- One-line convenience functions for quick setup
|
|
59
71
|
- Load secrets from AWS Secrets Manager
|
|
60
72
|
- Load parameters from SSM Parameter Store
|
|
73
|
+
- **Secret reference handlers** for URI-style dereferencing (`aws-sm://`, `aws-ssm://`)
|
|
61
74
|
- Safe (non-throwing) versions of all functions
|
|
62
75
|
- Automatic AWS credential detection (environment variables, IAM roles, ~/.aws/credentials)
|
|
63
76
|
- Optional TTL caching
|
|
64
77
|
- Full TypeScript support
|
|
65
78
|
|
|
79
|
+
## Secret Reference Handlers
|
|
80
|
+
|
|
81
|
+
Use URI-style references in your `.env` files instead of plaintext secrets:
|
|
82
|
+
|
|
83
|
+
```dotenv
|
|
84
|
+
# .env
|
|
85
|
+
DATABASE_URL=aws-sm://prod/database/url
|
|
86
|
+
API_KEY=aws-sm://prod/api/key#token
|
|
87
|
+
REDIS_URL=aws-ssm:///prod/redis/url
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
92
|
+
import { createAwsSecretHandler, createAwsSsmHandler } from 'node-env-resolver-aws';
|
|
93
|
+
|
|
94
|
+
const config = await resolveAsync({
|
|
95
|
+
resolvers: [[dotenv(), schema]],
|
|
96
|
+
references: {
|
|
97
|
+
handlers: {
|
|
98
|
+
'aws-sm': createAwsSecretHandler({ region: 'us-east-1' }),
|
|
99
|
+
'aws-ssm': createAwsSsmHandler({ region: 'us-east-1' }),
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Secret Reference Formats
|
|
106
|
+
|
|
107
|
+
**AWS Secrets Manager:**
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
aws-sm://secret-id # Entire secret as string
|
|
111
|
+
aws-sm://secret-id#key # Extract JSON key from secret
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**SSM Parameter Store:**
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
aws-ssm:///path/to/parameter # Parameter value
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Pre-configured Handlers
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { awsSecretHandler, awsSsmHandler } from 'node-env-resolver-aws';
|
|
124
|
+
|
|
125
|
+
// Uses default AWS credentials chain (env vars, IAM roles, ~/.aws/credentials)
|
|
126
|
+
const config = await resolveAsync({
|
|
127
|
+
resolvers: [[dotenv(), schema]],
|
|
128
|
+
references: {
|
|
129
|
+
handlers: {
|
|
130
|
+
'aws-sm': awsSecretHandler,
|
|
131
|
+
'aws-ssm': awsSsmHandler,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### JSON Key Extraction
|
|
138
|
+
|
|
139
|
+
For secrets stored as JSON objects in AWS Secrets Manager:
|
|
140
|
+
|
|
141
|
+
```dotenv
|
|
142
|
+
# Secret "prod/database" contains: {"host": "db.example.com", "password": "secret123"}
|
|
143
|
+
DB_HOST=aws-sm://prod/database#host
|
|
144
|
+
DB_PASSWORD=aws-sm://prod/database#password
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Handler Options
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
const handler = createAwsSecretHandler({
|
|
151
|
+
region: 'us-east-1', // AWS region (default: process.env.AWS_REGION)
|
|
152
|
+
accessKeyId: 'AKIAIOSF...', // Explicit credentials (optional)
|
|
153
|
+
secretAccessKey: 'wJalrX...', // Explicit credentials (optional)
|
|
154
|
+
sessionToken: '...', // Temporary credentials (optional)
|
|
155
|
+
parseJson: true, // Parse JSON secrets (default: true)
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const ssmHandler = createAwsSsmHandler({
|
|
159
|
+
region: 'us-east-1',
|
|
160
|
+
withDecryption: true, // Decrypt secure strings (default: true)
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
66
164
|
## Performance and caching
|
|
67
165
|
|
|
68
166
|
AWS API calls are slow (~100-200ms) and cost money. Caching is essential for production applications.
|
|
@@ -72,12 +170,15 @@ AWS API calls are slow (~100-200ms) and cost money. Caching is essential for pro
|
|
|
72
170
|
```typescript
|
|
73
171
|
// Every request hits AWS = slow and expensive
|
|
74
172
|
export const handler = async (event) => {
|
|
75
|
-
const config = await resolveSecrets(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
173
|
+
const config = await resolveSecrets(
|
|
174
|
+
{
|
|
175
|
+
secretId: 'myapp/secrets',
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
DATABASE_URL: postgres(),
|
|
179
|
+
API_KEY: string(),
|
|
180
|
+
}
|
|
181
|
+
);
|
|
81
182
|
// 200ms delay on EVERY request + AWS API costs
|
|
82
183
|
};
|
|
83
184
|
```
|
|
@@ -92,18 +193,18 @@ import { awsSecrets } from 'node-env-resolver-aws';
|
|
|
92
193
|
export const getConfig = async () => {
|
|
93
194
|
return await resolveAsync({
|
|
94
195
|
resolvers: [
|
|
95
|
-
[
|
|
96
|
-
awsSecrets({ secretId: 'myapp/secrets' }),
|
|
97
|
-
{
|
|
196
|
+
[
|
|
197
|
+
cached(awsSecrets({ secretId: 'myapp/secrets' }), {
|
|
98
198
|
ttl: TTL.minutes5,
|
|
99
199
|
maxAge: TTL.hour,
|
|
100
|
-
staleWhileRevalidate: true
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
200
|
+
staleWhileRevalidate: true,
|
|
201
|
+
}),
|
|
202
|
+
{
|
|
203
|
+
DATABASE_URL: postgres(),
|
|
204
|
+
API_KEY: string(),
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
],
|
|
107
208
|
});
|
|
108
209
|
};
|
|
109
210
|
|
|
@@ -123,13 +224,16 @@ import { awsSecrets } from 'node-env-resolver-aws';
|
|
|
123
224
|
const getConfig = async () => {
|
|
124
225
|
return await resolveAsync({
|
|
125
226
|
resolvers: [
|
|
126
|
-
[
|
|
127
|
-
awsSecrets({ secretId: 'myapp/lambda' }),
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
227
|
+
[
|
|
228
|
+
cached(awsSecrets({ secretId: 'myapp/lambda' }), {
|
|
229
|
+
ttl: TTL.minutes5,
|
|
230
|
+
staleWhileRevalidate: true,
|
|
231
|
+
}),
|
|
232
|
+
{
|
|
233
|
+
DATABASE_URL: postgres(),
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
],
|
|
133
237
|
});
|
|
134
238
|
};
|
|
135
239
|
|
|
@@ -155,6 +259,7 @@ export const handler = async (event) => {
|
|
|
155
259
|
This package uses the standard AWS SDK credential provider chain. Credentials are automatically detected from:
|
|
156
260
|
|
|
157
261
|
1. **Environment variables** (recommended for local development)
|
|
262
|
+
|
|
158
263
|
```bash
|
|
159
264
|
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
|
|
160
265
|
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
|
|
@@ -167,12 +272,15 @@ This package uses the standard AWS SDK credential provider chain. Credentials ar
|
|
|
167
272
|
|
|
168
273
|
4. **Explicit options** (for special cases):
|
|
169
274
|
```ts
|
|
170
|
-
await resolveSsm(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
275
|
+
await resolveSsm(
|
|
276
|
+
{
|
|
277
|
+
path: '/myapp/config',
|
|
278
|
+
region: 'us-east-1',
|
|
279
|
+
accessKeyId: 'AKIAIOSFODNN7EXAMPLE',
|
|
280
|
+
secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
|
|
281
|
+
},
|
|
282
|
+
{ API_ENDPOINT: string() }
|
|
283
|
+
);
|
|
176
284
|
```
|
|
177
285
|
|
|
178
286
|
**In most cases, you don't need to pass credentials explicitly** - just set the standard AWS environment variables or use IAM roles.
|
|
@@ -188,14 +296,17 @@ Directly resolve environment variables from SSM Parameter Store.
|
|
|
188
296
|
```ts
|
|
189
297
|
import { resolveSsm } from 'node-env-resolver-aws';
|
|
190
298
|
|
|
191
|
-
const config = await resolveSsm(
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
299
|
+
const config = await resolveSsm(
|
|
300
|
+
{
|
|
301
|
+
path: '/myapp/config',
|
|
302
|
+
region: 'us-east-1',
|
|
303
|
+
recursive: true,
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
API_ENDPOINT: string(),
|
|
307
|
+
TIMEOUT: 30,
|
|
308
|
+
}
|
|
309
|
+
);
|
|
199
310
|
```
|
|
200
311
|
|
|
201
312
|
#### `safeResolveSsm(ssmOptions, schema, resolveOptions?)`
|
|
@@ -205,11 +316,14 @@ Safe version that returns a result object instead of throwing.
|
|
|
205
316
|
```ts
|
|
206
317
|
import { safeResolveSsm } from 'node-env-resolver-aws';
|
|
207
318
|
|
|
208
|
-
const result = await safeResolveSsm(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
319
|
+
const result = await safeResolveSsm(
|
|
320
|
+
{
|
|
321
|
+
path: '/myapp/config',
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
API_ENDPOINT: string(),
|
|
325
|
+
}
|
|
326
|
+
);
|
|
213
327
|
|
|
214
328
|
if (result.success) {
|
|
215
329
|
console.log(result.data.API_ENDPOINT);
|
|
@@ -225,13 +339,16 @@ Directly resolve environment variables from Secrets Manager.
|
|
|
225
339
|
```ts
|
|
226
340
|
import { resolveSecrets } from 'node-env-resolver-aws';
|
|
227
341
|
|
|
228
|
-
const config = await resolveSecrets(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
342
|
+
const config = await resolveSecrets(
|
|
343
|
+
{
|
|
344
|
+
secretId: 'myapp/production/secrets',
|
|
345
|
+
region: 'us-east-1',
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
DATABASE_URL: string(),
|
|
349
|
+
API_KEY: string(),
|
|
350
|
+
}
|
|
351
|
+
);
|
|
235
352
|
```
|
|
236
353
|
|
|
237
354
|
#### `safeResolveSecrets(secretsOptions, schema, resolveOptions?)`
|
|
@@ -241,11 +358,14 @@ Safe version that returns a result object instead of throwing.
|
|
|
241
358
|
```ts
|
|
242
359
|
import { safeResolveSecrets } from 'node-env-resolver-aws';
|
|
243
360
|
|
|
244
|
-
const result = await safeResolveSecrets(
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
361
|
+
const result = await safeResolveSecrets(
|
|
362
|
+
{
|
|
363
|
+
secretId: 'myapp/secrets',
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
DATABASE_URL: string(),
|
|
367
|
+
}
|
|
368
|
+
);
|
|
249
369
|
|
|
250
370
|
if (result.success) {
|
|
251
371
|
console.log(result.data.DATABASE_URL);
|
|
@@ -270,11 +390,14 @@ import { awsSecrets } from 'node-env-resolver-aws';
|
|
|
270
390
|
|
|
271
391
|
const config = await resolveAsync({
|
|
272
392
|
resolvers: [
|
|
273
|
-
[
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
393
|
+
[
|
|
394
|
+
awsSecrets({ secretId: 'myapp/secrets' }),
|
|
395
|
+
{
|
|
396
|
+
DATABASE_URL: postgres(),
|
|
397
|
+
API_KEY: string(),
|
|
398
|
+
},
|
|
399
|
+
],
|
|
400
|
+
],
|
|
278
401
|
});
|
|
279
402
|
```
|
|
280
403
|
|
|
@@ -284,8 +407,8 @@ With options:
|
|
|
284
407
|
awsSecrets({
|
|
285
408
|
secretId: 'myapp/production/database',
|
|
286
409
|
region: 'us-east-1',
|
|
287
|
-
parseJson: true
|
|
288
|
-
})
|
|
410
|
+
parseJson: true, // Default: true
|
|
411
|
+
});
|
|
289
412
|
```
|
|
290
413
|
|
|
291
414
|
## SSM Parameter Store
|
|
@@ -298,11 +421,14 @@ import { awsSsm } from 'node-env-resolver-aws';
|
|
|
298
421
|
|
|
299
422
|
const config = await resolveAsync({
|
|
300
423
|
resolvers: [
|
|
301
|
-
[
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
424
|
+
[
|
|
425
|
+
awsSsm({ path: '/myapp/config' }),
|
|
426
|
+
{
|
|
427
|
+
API_ENDPOINT: string(),
|
|
428
|
+
TIMEOUT: 30,
|
|
429
|
+
},
|
|
430
|
+
],
|
|
431
|
+
],
|
|
306
432
|
});
|
|
307
433
|
```
|
|
308
434
|
|
|
@@ -312,8 +438,8 @@ Get all parameters under a path:
|
|
|
312
438
|
awsSsm({
|
|
313
439
|
path: '/myapp/production',
|
|
314
440
|
region: 'us-west-2',
|
|
315
|
-
recursive: true
|
|
316
|
-
})
|
|
441
|
+
recursive: true,
|
|
442
|
+
});
|
|
317
443
|
```
|
|
318
444
|
|
|
319
445
|
## Caching
|
|
@@ -326,17 +452,17 @@ import { awsSecrets } from 'node-env-resolver-aws';
|
|
|
326
452
|
|
|
327
453
|
const config = await resolveAsync({
|
|
328
454
|
resolvers: [
|
|
329
|
-
[
|
|
330
|
-
awsSecrets({ secretId: 'myapp/secrets' }),
|
|
331
|
-
{
|
|
455
|
+
[
|
|
456
|
+
cached(awsSecrets({ secretId: 'myapp/secrets' }), {
|
|
332
457
|
ttl: TTL.minutes5,
|
|
333
458
|
maxAge: TTL.hour,
|
|
334
|
-
staleWhileRevalidate: true
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
459
|
+
staleWhileRevalidate: true,
|
|
460
|
+
}),
|
|
461
|
+
{
|
|
462
|
+
DATABASE_URL: postgres(),
|
|
463
|
+
},
|
|
464
|
+
],
|
|
465
|
+
],
|
|
340
466
|
});
|
|
341
467
|
```
|
|
342
468
|
|
|
@@ -365,11 +491,7 @@ const config = await resolveAsync({
|
|
|
365
491
|
"Statement": [
|
|
366
492
|
{
|
|
367
493
|
"Effect": "Allow",
|
|
368
|
-
"Action": [
|
|
369
|
-
"ssm:GetParameter",
|
|
370
|
-
"ssm:GetParameters",
|
|
371
|
-
"ssm:GetParametersByPath"
|
|
372
|
-
],
|
|
494
|
+
"Action": ["ssm:GetParameter", "ssm:GetParameters", "ssm:GetParametersByPath"],
|
|
373
495
|
"Resource": ["arn:aws:ssm:region:account:parameter/myapp/*"]
|
|
374
496
|
}
|
|
375
497
|
]
|
|
@@ -382,11 +504,12 @@ const config = await resolveAsync({
|
|
|
382
504
|
|
|
383
505
|
```ts
|
|
384
506
|
interface AwsSecretsOptions {
|
|
385
|
-
secretId: string;
|
|
386
|
-
region?: string;
|
|
387
|
-
accessKeyId?: string;
|
|
388
|
-
secretAccessKey?: string;
|
|
389
|
-
|
|
507
|
+
secretId: string; // Secret ID or ARN
|
|
508
|
+
region?: string; // AWS region (overrides profile/env defaults)
|
|
509
|
+
accessKeyId?: string; // AWS access key (optional)
|
|
510
|
+
secretAccessKey?: string; // AWS secret key (optional)
|
|
511
|
+
profile?: string; // AWS shared credentials profile (optional)
|
|
512
|
+
parseJson?: boolean; // Parse JSON secrets (default: true)
|
|
390
513
|
}
|
|
391
514
|
```
|
|
392
515
|
|
|
@@ -394,11 +517,12 @@ interface AwsSecretsOptions {
|
|
|
394
517
|
|
|
395
518
|
```ts
|
|
396
519
|
interface AwsSsmOptions {
|
|
397
|
-
path: string;
|
|
398
|
-
region?: string;
|
|
399
|
-
accessKeyId?: string;
|
|
400
|
-
secretAccessKey?: string;
|
|
401
|
-
|
|
520
|
+
path: string; // Parameter path
|
|
521
|
+
region?: string; // AWS region (overrides profile/env defaults)
|
|
522
|
+
accessKeyId?: string; // AWS access key (optional)
|
|
523
|
+
secretAccessKey?: string; // AWS secret key (optional)
|
|
524
|
+
profile?: string; // AWS shared credentials profile (optional)
|
|
525
|
+
recursive?: boolean; // Get all parameters under path (default: false)
|
|
402
526
|
}
|
|
403
527
|
```
|
|
404
528
|
|
|
@@ -410,21 +534,27 @@ interface AwsSsmOptions {
|
|
|
410
534
|
import { resolveSecrets, resolveSsm } from 'node-env-resolver-aws';
|
|
411
535
|
|
|
412
536
|
// Load secrets from Secrets Manager
|
|
413
|
-
const secrets = await resolveSecrets(
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
537
|
+
const secrets = await resolveSecrets(
|
|
538
|
+
{
|
|
539
|
+
secretId: 'myapp/production/secrets',
|
|
540
|
+
},
|
|
541
|
+
{
|
|
542
|
+
DATABASE_URL: postgres(),
|
|
543
|
+
JWT_SECRET: string(),
|
|
544
|
+
}
|
|
545
|
+
);
|
|
419
546
|
|
|
420
547
|
// Load config from SSM
|
|
421
|
-
const config = await resolveSsm(
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
548
|
+
const config = await resolveSsm(
|
|
549
|
+
{
|
|
550
|
+
path: '/myapp/production/config',
|
|
551
|
+
recursive: true,
|
|
552
|
+
},
|
|
553
|
+
{
|
|
554
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
555
|
+
PORT: 3000,
|
|
556
|
+
}
|
|
557
|
+
);
|
|
428
558
|
```
|
|
429
559
|
|
|
430
560
|
### Production app with resolveAsync() (combining sources)
|
|
@@ -435,18 +565,27 @@ import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
|
435
565
|
|
|
436
566
|
const config = await resolveAsync({
|
|
437
567
|
resolvers: [
|
|
438
|
-
[
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
568
|
+
[
|
|
569
|
+
processEnv(),
|
|
570
|
+
{
|
|
571
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
572
|
+
PORT: 3000,
|
|
573
|
+
},
|
|
574
|
+
],
|
|
575
|
+
[
|
|
576
|
+
awsSecrets({ secretId: 'myapp/production/secrets' }),
|
|
577
|
+
{
|
|
578
|
+
DATABASE_URL: postgres(),
|
|
579
|
+
JWT_SECRET: string(),
|
|
580
|
+
},
|
|
581
|
+
],
|
|
582
|
+
[
|
|
583
|
+
awsSsm({ path: '/myapp/production/config' }),
|
|
584
|
+
{
|
|
585
|
+
TIMEOUT: 30,
|
|
586
|
+
},
|
|
587
|
+
],
|
|
588
|
+
],
|
|
450
589
|
});
|
|
451
590
|
```
|
|
452
591
|
|
|
@@ -455,12 +594,15 @@ const config = await resolveAsync({
|
|
|
455
594
|
```ts
|
|
456
595
|
import { resolveSecrets } from 'node-env-resolver-aws';
|
|
457
596
|
|
|
458
|
-
const config = await resolveSecrets(
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
597
|
+
const config = await resolveSecrets(
|
|
598
|
+
{
|
|
599
|
+
secretId: 'lambda/secrets',
|
|
600
|
+
},
|
|
601
|
+
{
|
|
602
|
+
API_ENDPOINT: string(),
|
|
603
|
+
TIMEOUT: 30,
|
|
604
|
+
}
|
|
605
|
+
);
|
|
464
606
|
|
|
465
607
|
export const handler = async (event) => {
|
|
466
608
|
// Use config
|
|
@@ -472,12 +614,15 @@ export const handler = async (event) => {
|
|
|
472
614
|
```ts
|
|
473
615
|
import { safeResolveSecrets } from 'node-env-resolver-aws';
|
|
474
616
|
|
|
475
|
-
const result = await safeResolveSecrets(
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
617
|
+
const result = await safeResolveSecrets(
|
|
618
|
+
{
|
|
619
|
+
secretId: 'myapp/production/secrets',
|
|
620
|
+
},
|
|
621
|
+
{
|
|
622
|
+
DATABASE_URL: string(),
|
|
623
|
+
API_KEY: string(),
|
|
624
|
+
}
|
|
625
|
+
);
|
|
481
626
|
|
|
482
627
|
if (result.success) {
|
|
483
628
|
// Use result.data with full type safety
|
|
@@ -496,11 +641,14 @@ if (result.success) {
|
|
|
496
641
|
```ts
|
|
497
642
|
import { safeResolveSecrets } from 'node-env-resolver-aws';
|
|
498
643
|
|
|
499
|
-
const result = await safeResolveSecrets(
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
644
|
+
const result = await safeResolveSecrets(
|
|
645
|
+
{
|
|
646
|
+
secretId: 'myapp/secrets',
|
|
647
|
+
},
|
|
648
|
+
{
|
|
649
|
+
DATABASE_URL: string(),
|
|
650
|
+
}
|
|
651
|
+
);
|
|
504
652
|
|
|
505
653
|
if (!result.success) {
|
|
506
654
|
console.error('Failed to load secrets:', result.error);
|
|
@@ -517,11 +665,14 @@ console.log(result.data.DATABASE_URL);
|
|
|
517
665
|
import { resolveSecrets } from 'node-env-resolver-aws';
|
|
518
666
|
|
|
519
667
|
try {
|
|
520
|
-
const config = await resolveSecrets(
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
668
|
+
const config = await resolveSecrets(
|
|
669
|
+
{
|
|
670
|
+
secretId: 'myapp/secrets',
|
|
671
|
+
},
|
|
672
|
+
{
|
|
673
|
+
DATABASE_URL: string(),
|
|
674
|
+
}
|
|
675
|
+
);
|
|
525
676
|
} catch (error) {
|
|
526
677
|
console.error('Failed to load secrets from AWS:', error);
|
|
527
678
|
}
|
|
@@ -530,14 +681,17 @@ try {
|
|
|
530
681
|
## Troubleshooting
|
|
531
682
|
|
|
532
683
|
**Permission denied**
|
|
684
|
+
|
|
533
685
|
- Check IAM permissions match examples above
|
|
534
686
|
- Verify resource ARNs are correct
|
|
535
687
|
|
|
536
688
|
**Secret not found**
|
|
689
|
+
|
|
537
690
|
- Verify secret ID or path exists
|
|
538
691
|
- Check region matches where secret is stored
|
|
539
692
|
|
|
540
693
|
**Network timeout**
|
|
694
|
+
|
|
541
695
|
- Check VPC/security group configuration
|
|
542
696
|
- Verify internet/NAT gateway access
|
|
543
697
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import {SecretsManagerClient,GetSecretValueCommand}from'@aws-sdk/client-secrets-manager';import {SSMClient,GetParameterCommand}from'@aws-sdk/client-ssm';import {fromIni}from'@aws-sdk/credential-providers';import {Buffer}from'node:buffer';function u(e){if(e.accessKeyId&&e.secretAccessKey)return {accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey,...e.sessionToken&&{sessionToken:e.sessionToken}};if(e.profile)return fromIni({profile:e.profile})}function o(e){let r=e.region??process.env.AWS_REGION;return {...r?{region:r}:{},credentials:u(e)}}function g(e){let r=e.match(/^aws-sm:\/\/(.+)$/);if(!r)throw new Error(`Invalid aws-sm reference: "${e}"
|
|
2
|
+
Expected format: aws-sm://secret-id or aws-sm://secret-id#json-key`);let s=r[1],t=s.indexOf("#");return t>0?{secretId:s.slice(0,t),key:s.slice(t+1)}:{secretId:s}}function h(e){let r=e.match(/^aws-ssm:\/\/(.+)$/);if(!r)throw new Error(`Invalid aws-ssm reference: "${e}"
|
|
3
|
+
Expected format: aws-ssm:///parameter-path`);return {parameterPath:r[1]}}function p(e){if(!e.startsWith("/"))throw new Error(`Invalid AWS SSM parameter name: "${e}"
|
|
4
|
+
SSM parameter names must start with "/"`);if(/^\/ssm(\/|$)/i.test(e))throw new Error(`Invalid AWS SSM parameter name: "${e}"
|
|
5
|
+
SSM parameter names cannot start with "/ssm"`);if(!/^[a-zA-Z0-9/._-]+$/.test(e)){let r=[...new Set(e.match(/[^a-zA-Z0-9/._-]/g)||[])].join("");throw new Error(`Invalid AWS SSM parameter name: "${e}"
|
|
6
|
+
Invalid character(s): ${r}`)}}function y(e){if(e.SecretString)return e.SecretString;if(e.SecretBinary)return Buffer.from(e.SecretBinary).toString("utf-8");throw new Error("Secret has no SecretString or SecretBinary value.")}async function A(e,r,s){let t;try{t=await s.send(new GetSecretValueCommand({SecretId:e}));}catch(i){let c=i instanceof Error?i.message:String(i);throw new Error(`Failed to fetch secret "${e}" from AWS Secrets Manager: ${c}
|
|
7
|
+
Tip: ensure your AWS credentials are configured and the secret exists in the correct region.`,{cause:i})}let n=y(t);if(!r)return n;let a;try{a=JSON.parse(n);}catch{throw new Error(`Secret "${e}" is not valid JSON \u2014 cannot extract key "${r}".
|
|
8
|
+
Remove the #${r} fragment if you want the full secret string.`)}if(typeof a!="object"||a===null)throw new Error(`Secret "${e}" is not a JSON object \u2014 cannot extract key "${r}".`);if(!(r in a)){let i=Object.keys(a).join(", ");throw new Error(`Secret "${e}" does not contain key "${r}". Available keys: ${i}`)}return String(a[r])}async function v(e,r,s){p(e);let t;try{t=await s.send(new GetParameterCommand({Name:e,WithDecryption:r}));}catch(n){let a=n instanceof Error?n.message:String(n);throw new Error(`Failed to fetch SSM parameter "${e}": ${a}
|
|
9
|
+
Tip: ensure the parameter exists and your IAM role has ssm:GetParameter permission.`,{cause:n})}if(!t.Parameter?.Value)throw new Error(`SSM parameter "${e}" exists but has no value.`);return t.Parameter.Value}function $(e={}){let r=new SecretsManagerClient(o(e));return {name:"aws-sm",async resolve(s){let{secretId:t,key:n}=g(s);return {value:await A(t,n,r),resolvedVia:"aws-secrets",metadata:{secretId:t,key:n,region:e.region??process.env.AWS_REGION}}}}}function E(e={}){let r=new SSMClient(o(e)),s=e.withDecryption??true;return {name:"aws-ssm",async resolve(t){let{parameterPath:n}=h(t);return {value:await v(n,s,r),resolvedVia:"aws-ssm",metadata:{parameterPath:n,region:e.region??process.env.AWS_REGION}}}}}var M=$(),R=E();export{$ as a,E as b,M as c,R as d};
|