serverless-plugin-simulate
serverless-plugin-simulate copied to clipboard
Returns error 400 if lambda outputs anything to stdout
This is a Bug Report
Description
If a lambda function writes anything to stdout, the JSON parser here will fail and a 400 status code will be returned. It should only attempt to parse the last line of stdout as a JSON object, not all of it. In the Node container console.log
is intercepted so this only occurs in non-js projects or if you're doing logging that writes to stdout directly.
To reproduce:
-
mkdir foo && sls create -t aws-nodejs && npm init
- Hit enter a bunch of times
-
npm install --save-dev serverless-plugin-simulate request
- Replace
serverless.yml
with this:
service: aws-nodejs
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
plugins:
- serverless-plugin-simulate
- Replace
handler.js
with this
'use strict';
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
process.stdout.write('oh no');
callback(null, response);
};
-
sls simulate apigateway
- in another terminal:
curl http://localhost:3000/hello
You should see this in the first terminal:
Serverless: [GET /hello] => λ:hello
Serverless: Invoke URL: http://localhost:3000
Serverless: Creating event
Serverless: Invoking hello
Serverless: Invoking function handler.hello
WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.
START RequestId: 4b732836-5d9b-117d-df61-40353e82ede0 Version: $LATEST
oh no
END RequestId: 4b732836-5d9b-117d-df61-40353e82ede0
REPORT RequestId: 4b732836-5d9b-117d-df61-40353e82ede0 Duration: 2.70 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 28 MB
{"statusCode":200,"body":"{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":{\"path\":\"/hello\",\"headers\":{\"host\":\"localhost:3000\",\"user-agent\":\"curl/7.55.1\",\"accept\":\"*/*\"},\"pathParameters\":{},\"requestContext\":{\"accountId\":\"localContext_accountId\",\"resourceId\":\"localContext_resourceId\",\"stage\":\"dev\",\"requestId\":\"localContext_requestId_9738752882301769\",\"identity\":{\"cognitoIdentityPoolId\":\"localContext_cognitoIdentityPoolId\",\"accountId\":\"localContext_accountId\",\"cognitoIdentityId\":\"localContext_cognitoIdentityId\",\"caller\":\"localContext_caller\",\"apiKey\":\"localContext_apiKey\",\"sourceIp\":\"::ffff:127.0.0.1\",\"cognitoAuthenticationType\":\"localContext_cognitoAuthenticationType\",\"cognitoAuthenticationProvider\":\"localContext_cognitoAuthenticationProvider\",\"userArn\":\"localContext_userArn\",\"userAgent\":\"curl/7.55.1\",\"user\":\"localContext_user\"}},\"resource\":\"localContext_resource\",\"httpMethod\":\"GET\",\"queryStringParameters\":{},\"body\":\"{}\",\"stageVariables\":{}}}"}
Serverless: {}
Serverless: Creating error response
GET /hello 400 881.250 ms - 38
And this from curl {"message":"Error executing function"}
.
Expected behavior is return a 200 status code. That's what happens on AWS.
lib/invoke/local.js
appears to be a copy of index.js
from docker-lambda
. Or maybe the other way around. This bug is fixed there, in lambci/docker-lambda#17. This plugin should depend on the docker-lambda
npm package.
Additional Data
- Serverless Framework Version you're using: 1.26.0
- Serverless Docker Plugin Version you're using: 0.0.17 and HEAD
- Operating System: Ubuntu Linux
- Stack Trace: See above
- Provider Error messages: See above
I have no idea why I thought those two files were the same actually. Working on this now.