-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
51 lines (39 loc) · 1.18 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
'use strict';
const AWS = require('aws-sdk');
const elasticsearch = require('elasticsearch');
const httpAWSESClass = require('http-aws-es');
module.exports = {
createClient: createClient,
getOptions: getOptions
};
function getOptions(options) {
options = options || {};
// serverless-offline will set IS_OFFLINE based on whether we're offline
const devMode = Boolean(process.env.IS_OFFLINE);
const prefix = options.envPrefix || 'AWS';
const region = options.region || process.env[`${prefix}_REGION`];
const host = options.host || process.env[`${prefix}_HOST`];
delete options.region; // this doesn't belong in ES options
if (!region) {
throw new TypeError('region is required');
}
if (!host) {
throw new TypeError('host is required');
}
const credentials = options.credentials || new AWS.EnvironmentCredentials(prefix);
const config = Object.assign({}, options, {
host: host,
amazonES: {
region,
credentials
}
});
// don't sign the request in offline mode
if (!devMode) {
config.connectionClass = httpAWSESClass;
}
return config;
}
function createClient(options) {
return new elasticsearch.Client(getOptions(options));
}