-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 loc) · 1.17 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
const fs = require('fs');
const getValues = (path = '.env') => {
let values = {};
try {
values = fs
.readFileSync(path, { encoding: 'utf-8' })
.trim()
.split('\n')
.map((line) => line.split(/=(.*)/))
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
} catch (err) {
// Catches missing environment files
}
return values;
};
class ServerlessSSMProvider {
constructor(serverless) {
this.serverless = serverless;
this.config = this.serverless.service.custom['serverless-ssm-provider'];
this.values = this.config ? getValues(this.config.file) : getValues();
const aws = this.serverless.getProvider('aws');
const request = aws.request.bind(aws);
aws.request = (service, method, params, options) => {
const { Name } = params;
const Value = this.values[Name];
if (service === 'SSM' && method === 'getParameter' && Value) {
return Promise.resolve({
Parameter: { Value },
});
}
return request(service, method, params, options);
};
this.serverless.setProvider('aws', aws);
}
}
module.exports = ServerlessSSMProvider;