Skip to content

Commit 34d9da0

Browse files
author
John Stalbaum
committed
Came up with a way to send large custom events into a Lambda (fixes lambci#64)
1 parent 080ae34 commit 34d9da0

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ docker run --rm -v "$PWD":/var/task lambci/lambda:java8 org.myorg.MyHandler
6464

6565
# Run custom commands on the default container
6666
docker run --rm --entrypoint node lambci/lambda -v
67+
68+
# Use the Node.js v6.10 runtime with a large custom event stored in a file ~/my_event/event.json
69+
docker run --rm -v "$PWD":/var/task -v ~/my_event:/var/event lambci/lambda:nodejs6.10
6770
```
6871

6972
You can see more examples of how to build docker images and run different

nodejs/run/awslambda-mock.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
var crypto = require('crypto')
2+
var fs = require('fs')
23

34
var HANDLER = process.argv[2] || process.env.AWS_LAMBDA_FUNCTION_HANDLER || process.env._HANDLER || 'index.handler'
4-
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
5+
6+
var READ_BODY = null;
7+
try {
8+
READ_BODY = fs.readFileSync('/var/event/event.json', 'utf8');
9+
} catch (e) {
10+
}
11+
var EVENT_BODY = READ_BODY || process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
512

613
var FN_NAME = process.env.AWS_LAMBDA_FUNCTION_NAME || 'test'
714
var VERSION = process.env.AWS_LAMBDA_FUNCTION_VERSION || '$LATEST'

nodejs4.3/run/awslambda-mock.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
var crypto = require('crypto')
22

33
var HANDLER = process.argv[2] || process.env.AWS_LAMBDA_FUNCTION_HANDLER || process.env._HANDLER || 'index.handler'
4-
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
4+
5+
var READ_BODY = null;
6+
try {
7+
READ_BODY = fs.readFileSync('/var/event/event.json', 'utf8');
8+
} catch (e) {
9+
}
10+
var EVENT_BODY = READ_BODY || process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
511

612
var FN_NAME = process.env.AWS_LAMBDA_FUNCTION_NAME || 'test'
713
var VERSION = process.env.AWS_LAMBDA_FUNCTION_VERSION || '$LATEST'

nodejs6.10/run/awslambda-mock.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
var crypto = require('crypto')
2+
var fs = require('fs')
23

34
var HANDLER = process.argv[2] || process.env.AWS_LAMBDA_FUNCTION_HANDLER || process.env._HANDLER || 'index.handler'
4-
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
5+
6+
var READ_BODY = null;
7+
try {
8+
READ_BODY = fs.readFileSync('/var/event/event.json', 'utf8');
9+
} catch (e) {
10+
}
11+
var EVENT_BODY = READ_BODY || process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
512

613
var FN_NAME = process.env.AWS_LAMBDA_FUNCTION_NAME || 'test'
714
var VERSION = process.env.AWS_LAMBDA_FUNCTION_VERSION || '$LATEST'

python2.7/run/runtime-mock.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def _arn(region, account_id, fct_name):
2323
return 'arn:aws:lambda:%s:%s:function:%s' % (region, account_id, fct_name)
2424

2525
_GLOBAL_HANDLER = sys.argv[1] if len(sys.argv) > 1 else os.environ.get('AWS_LAMBDA_FUNCTION_HANDLER', os.environ.get('_HANDLER', 'lambda_function.lambda_handler'))
26-
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY', '{}')
26+
if os.path.exists('/var/event/event.json'):
27+
with open('/var/event/event.json', 'r') as event_file:
28+
_GLOBAL_EVENT_BODY = event_file.read()
29+
else:
30+
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY', '{}')
2731
_GLOBAL_FCT_NAME = os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'test')
2832
_GLOBAL_VERSION = os.environ.get('AWS_LAMBDA_FUNCTION_VERSION', '$LATEST')
2933
_GLOBAL_MEM_SIZE = os.environ.get('AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1536')

python3.6/run/runtime-mock.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def _arn(region, account_id, fct_name):
2323
return 'arn:aws:lambda:%s:%s:function:%s' % (region, account_id, fct_name)
2424

2525
_GLOBAL_HANDLER = sys.argv[1] if len(sys.argv) > 1 else os.environ.get('AWS_LAMBDA_FUNCTION_HANDLER', os.environ.get('_HANDLER', 'lambda_function.lambda_handler'))
26-
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY', '{}')
26+
if os.path.exists('/var/event/event.json'):
27+
with open('/var/event/event.json', 'r') as event_file:
28+
_GLOBAL_EVENT_BODY = event_file.read()
29+
else:
30+
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY', '{}')
2731
_GLOBAL_FCT_NAME = os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'test')
2832
_GLOBAL_VERSION = os.environ.get('AWS_LAMBDA_FUNCTION_VERSION', '$LATEST')
2933
_GLOBAL_MEM_SIZE = os.environ.get('AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1536')

0 commit comments

Comments
 (0)