-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (47 loc) · 1.29 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
48
49
50
51
52
53
#!/usr/bin/env node
const fs = require("fs");
const { SecretsManager } = require("@aws-sdk/client-secrets-manager");
const yargs = require("yargs");
const argv = yargs
.option("region", {
description: "AWS region",
type: "string",
demandOption: false,
default: "eu-central-1",
})
.option("secretId", {
description: "SecretId in AWS Secrets Manager",
type: "string",
demandOption: true,
}).argv;
// Configure AWS SDK
const region = argv.region;
const client = new SecretsManager({
region,
});
const secretId = argv.secretId;
return new Promise((resolve, reject) => {
// Retrieving secrets from Secrets Manager
client.getSecretValue({ SecretId: secretId }, (err, data) => {
if (err) {
console.error("Error occurred");
reject(err);
} else {
// Parsing the fetched data into JSON
const secretsJSON = JSON.parse(data.SecretString);
let secretsString = "";
Object.keys(secretsJSON).forEach((key) => {
secretsString += `${key}=${secretsJSON[key]}\n`;
});
// Write to .env file
fs.writeFile(".env", secretsString, (err) => {
if (err) {
reject(err);
} else {
console.log("Secrets successfully written to .env file");
resolve();
}
});
}
});
});