forked from lambci/docker-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (50 loc) · 1.77 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
54
55
56
57
var spawnSync = require('child_process').spawnSync
var ENV_VARS = [
'AWS_REGION',
'AWS_DEFAULT_REGION',
'AWS_ACCOUNT_ID',
'AWS_ACCESS_KEY_ID',
'AWS_SECRET_ACCESS_KEY',
'AWS_SESSION_TOKEN',
'AWS_LAMBDA_FUNCTION_NAME',
'AWS_LAMBDA_FUNCTION_VERSION',
'AWS_LAMBDA_FUNCTION_MEMORY_SIZE',
'AWS_LAMBDA_FUNCTION_TIMEOUT',
'AWS_LAMBDA_FUNCTION_HANDLER',
'AWS_LAMBDA_EVENT_BODY',
]
var ENV_ARGS = [].concat.apply([], ENV_VARS.map(function(x) { return ['-e', x] }))
// Will spawn `docker run` synchronously and return stdout
module.exports = function runSync(options) {
options = options || {}
var dockerImage = options.dockerImage || 'lambci/lambda'
var handler = options.handler || 'index.handler'
var event = options.event || {}
var taskDir = options.taskDir == null ? process.cwd() : options.taskDir
var cleanUp = options.cleanUp == null ? true : options.cleanUp
var addEnvVars = options.addEnvVars || false
var dockerArgs = options.dockerArgs || []
var spawnOptions = options.spawnOptions || {encoding: 'utf8'}
var returnSpawnResult = options.returnSpawnResult || false
var args = ['run']
.concat(taskDir ? ['-v', taskDir + ':/var/task'] : [])
.concat(cleanUp ? ['--rm'] : [])
.concat(addEnvVars ? ENV_ARGS : [])
.concat(dockerArgs)
.concat([dockerImage, handler, JSON.stringify(event)])
var spawnResult = spawnSync('docker', args, spawnOptions)
if (returnSpawnResult) {
return spawnResult
}
if (spawnResult.error || spawnResult.status !== 0) {
var err = spawnResult.error
if (!err) {
err = new Error(spawnResult.stdout || spawnResult.stderr)
err.code = spawnResult.status
err.stdout = spawnResult.stdout
err.stderr = spawnResult.stderr
}
throw err
}
return JSON.parse(spawnResult.stdout)
}