aws / serverless-java-container

A Java wrapper to run Spring, Spring Boot, Jersey, and other apps inside AWS Lambda.
https://aws.amazon.com/serverless/
Apache License 2.0
1.5k stars 560 forks source link

jersey lambda fn has null outputstream #230

Closed jessefarinacci closed 5 years ago

jessefarinacci commented 5 years ago

Scenario

public class StreamLambdaHandler implements RequestStreamHandler {

    private static final Logger LOG = LoggerFactory.getLogger(StreamLambdaHandler.class);
    private static final String DEFAULT_PACKAGES = "org";
    private final ResourceConfig application;
    private final JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

    public StreamLambdaHandler() {
        this(DEFAULT_PACKAGES);
    }

    public StreamLambdaHandler(String packages) {
        super();
        application = new ResourceConfig().packages(packages).register(JacksonFeature.class);
        handler = JerseyLambdaContainerHandler.getAwsProxyHandler(application);
    }

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        LOG.info("APPLICATION: " + application);
        LOG.info("HANDLER:     " + handler);
        LOG.info("INPUT:       " + inputStream);
        LOG.info("OUTPUT:      " + outputStream);
        LOG.info("CONTEXT:     " + context);

        handler.proxyStream(inputStream, outputStream, context);
    }
}

Expected behavior

OUTPUT: some non-null value

Actual behavior

OUTPUT:

Steps to reproduce

Full log output

[main] INFO com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Starting Lambda Container Handler
START RequestId: 771df6a0-de9b-4766-9ad8-78c338708264 Version: $LATEST
[main] INFO org.buffalo.coders.aws.lambda.common.StreamLambdaHandler - APPLICATION: org.glassfish.jersey.server.ResourceConfig@4426bff1
[main] INFO org.buffalo.coders.aws.lambda.common.StreamLambdaHandler - HANDLER: com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler@3c7c886c
[main] INFO org.buffalo.coders.aws.lambda.common.StreamLambdaHandler - INPUT: lambdainternal.util.NativeMemoryAsInputStream@55493582
[main] INFO org.buffalo.coders.aws.lambda.common.StreamLambdaHandler - OUTPUT:
[main] INFO org.buffalo.coders.aws.lambda.common.StreamLambdaHandler - CONTEXT: lambdainternal.api.LambdaContext@1a20270e
[main] ERROR com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Error while handling request
java.lang.NullPointerException
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104)
[main] ERROR com.amazonaws.serverless.proxy.AwsProxyExceptionHandler - Called exception handler for:
java.lang.NullPointerException
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:48)
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:28)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:174)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:209)
at org.buffalo.coders.aws.lambda.common.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104)
java.lang.NullPointerException
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:48)
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:28)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:174)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:209)
at org.buffalo.coders.aws.lambda.common.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104)
END RequestId: 771df6a0-de9b-4766-9ad8-78c338708264
REPORT RequestId: 771df6a0-de9b-4766-9ad8-78c338708264  Duration: 58.83 ms      Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 87 MB
sapessi commented 5 years ago

Hey @jessefarinacci, where are you invoking the Lambda function from? Looks like a NPE when trying to put the ALB context in the request. Normally, I would expect the requestContext to be not null even if the request did not come from ALB. This is similar to #237 where the event is triggered by an unsupported event source (CloudWatch).

sapessi commented 5 years ago

Closing since version 1.3.1 is out.