-
Notifications
You must be signed in to change notification settings - Fork 17
/
lambda.js
110 lines (105 loc) · 3.56 KB
/
lambda.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-disable global-require,import/no-unresolved */
const AWS = require("aws-sdk");
const awsServerlessExpress = require("aws-serverless-express");
let config;
let app;
let server;
let jobs;
try {
({ config } = require("./build/src/config"));
app = require("./build/src/server/app");
server = awsServerlessExpress.createServer(app.default);
jobs = require("./build/src/workers/job-processes");
} catch (err) {
if (!global.TEST_ENVIRONMENT) {
console.error(`Unable to load built server: ${err}`);
}
({ config } = require("./src/config"));
app = require("./src/server/app");
server = awsServerlessExpress.createServer(app.default);
jobs = require("./src/workers/job-processes");
}
// NOTE: the downside of loading above is environment variables are initially loaded immediately,
// so changing them means that the code must test environment variable inline (rather than
// use a const set on-load)
// We should NOT load app and server inside the handler, or all connection pools and state are
// re-instantiated per-request:
// See: http://docs.aws.amazon.com/lambda/latest/dg/best-practices.html#function-code
// "Separate the Lambda handler (entry point) from your core logic"
function cleanHeaders(event) {
// X-Twilio-Body can contain unicode and disallowed chars by aws-serverless-express like "'"
// We don't need it anyway
if (event.headers) {
delete event.headers["X-Twilio-Body"];
}
if (event.multiValueHeaders) {
delete event.multiValueHeaders["X-Twilio-Body"];
}
}
exports.handler = (event, context, handleCallback) => {
// Note: When lambda is called with invoke() we MUST call handleCallback with a success
// or Lambda will re-run/re-try the invocation twice:
// https://docs.aws.amazon.com/lambda/latest/dg/retries-on-errors.html
if (config.LAMBDA_DEBUG_LOG) {
console.log("LAMBDA EVENT", event);
}
if (!event.command) {
// default web server stuff
const startTime = context.getRemainingTimeInMillis
? context.getRemainingTimeInMillis()
: 0;
cleanHeaders(event);
const webResponse = awsServerlessExpress.proxy(server, event, context);
if (config.DEBUG_SCALING) {
const endTime = context.getRemainingTimeInMillis
? context.getRemainingTimeInMillis()
: 0;
if (endTime - startTime > 3000) {
// 3 seconds
console.error(
"SLOW_RESPONSE milliseconds:",
endTime - startTime,
event
);
}
}
return webResponse;
}
// handle a custom command sent as an event
const { functionName } = context;
if (event.env) {
for (const a in event.env) {
process.env[a] = event.env[a];
}
}
console.log(`Running ${event.command}`);
if (event.command in jobs) {
const job = jobs[event.command];
// behavior and arguments documented here:
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property
job(
event,
function dispatcher(dataToSend, callback) {
const lambda = new AWS.Lambda();
return lambda.invoke(
{
FunctionName: functionName,
InvocationType: "Event", // asynchronous
Payload: JSON.stringify(dataToSend)
},
(err, dataReceived) => {
if (err) {
console.error("Failed to invoke Lambda job: ", err);
}
if (callback) {
callback(err, dataReceived);
}
}
);
},
handleCallback
);
} else {
console.error(`Unfound command sent as a Lambda event: ${event.command}`);
}
};