generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathconfig.js
More file actions
53 lines (45 loc) · 1.62 KB
/
config.js
File metadata and controls
53 lines (45 loc) · 1.62 KB
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
var config = {
infra: {},
app: {},
secret: {}
};
// Define a function to initialize the config
async function initializeConfig() {
config.infra.region = process.env.AWS_REGION;
config.app.hotel_param = process.env.HOTEL_NAME_PARAM;
config.secret.db_secret = process.env.MYSQL_SECRET;
// Await the retrieval of secret value and SSM parameter
config.secret.db_secret_value = await retrieve_secret_value(config.secret.db_secret);
config.app.hotel_name = await retrieve_ssm_parameter(config.app.hotel_param);
return config; // Return the fully loaded config
}
// Asynchronous function to retrieve secret value
async function retrieve_secret_value(secret_id) {
var AWS = require('aws-sdk');
var client = new AWS.SecretsManager({
region: config.infra.region
});
try {
const data = await client.getSecretValue({ SecretId: secret_id }).promise();
return data.SecretString || data; // Return the secret string if available
} catch (err) {
console.error("Error retrieving secret:", err);
throw err;
}
}
// Asynchronous function to retrieve SSM parameter
async function retrieve_ssm_parameter(parameter_name) {
var AWS = require('aws-sdk');
var client = new AWS.SSM({
region: config.infra.region
});
try {
const data = await client.getParameter({ Name: parameter_name }).promise();
return data.Parameter.Value; // Return the parameter value
} catch (err) {
console.error("Error retrieving parameter:", err);
throw err;
}
}
// Export the config as a promise
module.exports = initializeConfig();