-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (37 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import AWS from 'aws-sdk';
import fs from 'fs';
const {
AWS_LAMBDA_FUNCTION_NAME,
AWS_REGION,
SECRET_NAME,
} = process.env;
const secretsManager = new AWS.SecretsManager({
region: AWS_REGION,
});
async function getSecret(secretName) {
const secretValue = await secretsManager.getSecretValue({ SecretId: secretName }).promise();
if (secretValue?.SecretString) {
return JSON.parse(secretValue.SecretString);
} else {
const buffer = Buffer.from(secretValue.SecretBinary, 'base64');
return buffer.toString('ascii');
}
}
async function main() {
if (process.argv.length !== 3) {
throw new Error('Cannot determine temp file');
}
const tmpFile = process.argv[2];
const secretName = SECRET_NAME ?? `lambda/${AWS_LAMBDA_FUNCTION_NAME}`;
const secret = await getSecret(secretName);
const fsStream = fs.createWriteStream(tmpFile);
for (const [key, value] of Object.entries(secret)) {
fsStream.write(`export ${key}="${value}"\n`);
}
fsStream.end();
}
process.on('unhandledRejection', (e) => {
console.error(e);
process.exit(1);
});
main();