Skip to content

Commit

Permalink
Add DOCKER_LAMBDA_USE_STDIN option for events via stdin
Browse files Browse the repository at this point in the history
Fixes lambci#64
Fixes lambci#71
  • Loading branch information
mhart committed Mar 10, 2018
1 parent b4ceb02 commit be7d4df
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ docker run --rm -v "$PWD":/var/task lambci/lambda:dotnetcore2.0 test::test.Funct

# Run custom commands on the default container
docker run --rm --entrypoint node lambci/lambda -v

# For large events you can pipe them into stdin if you set DOCKER_LAMBDA_USE_STDIN (on any runtime)
echo '{"some": "event"}' | docker run --rm -v "$PWD":/var/task -i -e DOCKER_LAMBDA_USE_STDIN=1 lambci/lambda
```

You can see more examples of how to build docker images and run different
Expand Down Expand Up @@ -207,6 +210,7 @@ Env vars:
- `AWS_ACCESS_KEY_ID`
- `AWS_SECRET_ACCESS_KEY`
- `AWS_SESSION_TOKEN`
- `DOCKER_LAMBDA_USE_STDIN`

Options to pass to `dockerLambda()`:
- `dockerImage`
Expand Down
3 changes: 2 additions & 1 deletion dotnetcore2.0/run/MockBootstraps/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ static string GetFunctionHandler(string[] args)
/// Gets the event body from arguments or environment
static string GetEventBody(string[] args)
{
return args.Length > 1 ? args[1] : EnvHelper.GetOrDefault("AWS_LAMBDA_EVENT_BODY", "{}");
return args.Length > 1 ? args[1] : (Environment.GetEnvironmentVariable("AWS_LAMBDA_EVENT_BODY") ??
(Environment.GetEnvironmentVariable("DOCKER_LAMBDA_USE_STDIN") != null ? Console.In.ReadToEnd() : "{}"));
}
}

Expand Down
16 changes: 13 additions & 3 deletions go1.x/run/aws-lambda-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/lambda/messages"
"io/ioutil"
"math"
"math/rand"
"net"
Expand Down Expand Up @@ -34,7 +35,15 @@ func main() {
if len(os.Args) > 2 {
eventBody = os.Args[2]
} else {
eventBody = getEnv("AWS_LAMBDA_EVENT_BODY", "{}")
eventBody = os.Getenv("AWS_LAMBDA_EVENT_BODY")
if eventBody == "" {
if os.Getenv("DOCKER_LAMBDA_USE_STDIN") != "" {
stdin, _ := ioutil.ReadAll(os.Stdin)
eventBody = string(stdin)
} else {
eventBody = "{}"
}
}
}

mockContext := &MockLambdaContext{
Expand All @@ -53,7 +62,7 @@ func main() {

awsAccessKey := getEnv("AWS_ACCESS_KEY", getEnv("AWS_ACCESS_KEY_ID", "SOME_ACCESS_KEY_ID"))
awsSecretKey := getEnv("AWS_SECRET_KEY", getEnv("AWS_SECRET_ACCESS_KEY", "SOME_SECRET_ACCESS_KEY"))
awsSessionToken := getEnv("AWS_SESSION_TOKEN", getEnv("AWS_SECURITY_TOKEN", ""))
awsSessionToken := getEnv("AWS_SESSION_TOKEN", os.Getenv("AWS_SECURITY_TOKEN"))
port := getEnv("_LAMBDA_SERVER_PORT", "54321")

os.Setenv("AWS_LAMBDA_FUNCTION_NAME", mockContext.FnName)
Expand Down Expand Up @@ -191,7 +200,8 @@ func logEndRequest(mockContext *MockLambdaContext, err error) {
}

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok && len(value) > 0 {
value := os.Getenv(key)
if value != "" {
return value
}
return fallback
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var ENV_VARS = [
'AWS_LAMBDA_FUNCTION_TIMEOUT',
'AWS_LAMBDA_FUNCTION_HANDLER',
'AWS_LAMBDA_EVENT_BODY',
'DOCKER_LAMBDA_USE_STDIN',
]
var ENV_ARGS = [].concat.apply([], ENV_VARS.map(function(x) { return ['-e', x] }))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.UUID;

import sun.misc.Unsafe;
Expand Down Expand Up @@ -66,7 +67,7 @@ public class LambdaRuntime {

String[] args = getCmdLineArgs();
HANDLER = args.length > 1 ? args[1] : getEnvOrDefault("AWS_LAMBDA_FUNCTION_HANDLER", getEnvOrDefault("_HANDLER", "index.Handler"));
EVENT_BODY = args.length > 2 ? args[2] : getEnvOrDefault("AWS_LAMBDA_EVENT_BODY", "{}");
EVENT_BODY = args.length > 2 ? args[2] : getEventBody();

LambdaRuntime.needsDebugLogs = false;

Expand All @@ -80,6 +81,15 @@ public class LambdaRuntime {
setenv("_HANDLER", HANDLER, 1);
}

private static String getEventBody() {
String eventBody = getEnv("AWS_LAMBDA_EVENT_BODY");
if (eventBody == null) {
eventBody = getEnv("DOCKER_LAMBDA_USE_STDIN") != null ?
new Scanner(System.in).useDelimiter("\\A").next() : "{}";
}
return eventBody;
}

private static String getEnvOrDefault(String key, String defaultVal) {
String envVal = getEnv(key);
return envVal != null ? envVal : defaultVal;
Expand Down
4 changes: 3 additions & 1 deletion nodejs/run/awslambda-mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var fs = require('fs')
var crypto = require('crypto')

var HANDLER = process.argv[2] || process.env.AWS_LAMBDA_FUNCTION_HANDLER || process.env._HANDLER || 'index.handler'
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY ||
(process.env.DOCKER_LAMBDA_USE_STDIN && fs.readFileSync('/dev/stdin', 'utf8')) || '{}'

var FN_NAME = process.env.AWS_LAMBDA_FUNCTION_NAME || 'test'
var VERSION = process.env.AWS_LAMBDA_FUNCTION_VERSION || '$LATEST'
Expand Down
4 changes: 3 additions & 1 deletion nodejs4.3/run/awslambda-mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var fs = require('fs')
var crypto = require('crypto')

var HANDLER = process.argv[2] || process.env.AWS_LAMBDA_FUNCTION_HANDLER || process.env._HANDLER || 'index.handler'
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY ||
(process.env.DOCKER_LAMBDA_USE_STDIN && fs.readFileSync('/dev/stdin', 'utf8')) || '{}'

var FN_NAME = process.env.AWS_LAMBDA_FUNCTION_NAME || 'test'
var VERSION = process.env.AWS_LAMBDA_FUNCTION_VERSION || '$LATEST'
Expand Down
4 changes: 3 additions & 1 deletion nodejs6.10/run/awslambda-mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var fs = require('fs')
var crypto = require('crypto')

var HANDLER = process.argv[2] || process.env.AWS_LAMBDA_FUNCTION_HANDLER || process.env._HANDLER || 'index.handler'
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY || '{}'
var EVENT_BODY = process.argv[3] || process.env.AWS_LAMBDA_EVENT_BODY ||
(process.env.DOCKER_LAMBDA_USE_STDIN && fs.readFileSync('/dev/stdin', 'utf8')) || '{}'

var FN_NAME = process.env.AWS_LAMBDA_FUNCTION_NAME || 'test'
var VERSION = process.env.AWS_LAMBDA_FUNCTION_VERSION || '$LATEST'
Expand Down
6 changes: 4 additions & 2 deletions python2.7/run/runtime-mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def _random_invoke_id():
def _arn(region, account_id, fct_name):
return 'arn:aws:lambda:%s:%s:function:%s' % (region, account_id, fct_name)

_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'))
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY', '{}')
_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'))
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY',
(sys.stdin.read() if os.environ.get('DOCKER_LAMBDA_USE_STDIN', False) else '{}'))
_GLOBAL_FCT_NAME = os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'test')
_GLOBAL_VERSION = os.environ.get('AWS_LAMBDA_FUNCTION_VERSION', '$LATEST')
_GLOBAL_MEM_SIZE = os.environ.get('AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1536')
Expand Down
6 changes: 4 additions & 2 deletions python3.6/run/runtime-mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def _random_invoke_id():
def _arn(region, account_id, fct_name):
return 'arn:aws:lambda:%s:%s:function:%s' % (region, account_id, fct_name)

_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'))
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY', '{}')
_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'))
_GLOBAL_EVENT_BODY = sys.argv[2] if len(sys.argv) > 2 else os.environ.get('AWS_LAMBDA_EVENT_BODY',
(sys.stdin.read() if os.environ.get('DOCKER_LAMBDA_USE_STDIN', False) else '{}'))
_GLOBAL_FCT_NAME = os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'test')
_GLOBAL_VERSION = os.environ.get('AWS_LAMBDA_FUNCTION_VERSION', '$LATEST')
_GLOBAL_MEM_SIZE = os.environ.get('AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1536')
Expand Down

0 comments on commit be7d4df

Please sign in to comment.