'Why is my AWS Lambda function ending before finishing with no timeout message?

I've been using AWS Lambda and testing with SAM local for nearly a year with no major issues. However, I've written a Lambda function which modifies some files with the S3 API.

The function ends with a 502: Invalid lambda response received: Lambda returned <class 'NoneType'> instead of dict

This is before my function has had a chance to finish... I've managed to condense the code to the following:

exports.handler = async (event, context) => {
    console.log("Goldi");
    await fish(event, context);
    console.log("Locks");
    return { statusCode: 200, body: "Finished!" };
};

No matter whether I run this in SAM Local or upload to AWS Lambda, I get this output:

START RequestId: 6a30e157-3e9b-465e-a945-3e9f7fa2cd7e Version: $LATEST
2022-01-12T18:36:27.601Z        6a30e157-3e9b-465e-a945-3e9f7fa2cd7e    INFO    Goldi
2022-01-12T18:36:27.603Z        6a30e157-3e9b-465e-a945-3e9f7fa2cd7e    INFO    Some output from fish()...
END RequestId: 6a30e157-3e9b-465e-a945-3e9f7fa2cd7e
REPORT RequestId: 6a30e157-3e9b-465e-a945-3e9f7fa2cd7e  Init Duration: 0.18 ms  Duration: 12600.03 ms   Billed Duration: 12700 ms       Memory Size: 512 MB     Max Memory Used: 512 MB
Invalid lambda response received: Lambda returned <class 'NoneType'> instead of dict
2022-01-12 18:36:38 127.0.0.1 - - [12/Jan/2022 18:36:38] "POST / HTTP/1.1" 502 -

I've configured this Lambda function to have a timeout of several minutes and I do not call any functions in 'context'

I've sunk several hours into trying to figure out how a Lambda function can end without any error message (from my code) or a timeout notice.

Is this a known behaviour? Does anyone know how I can find out what causes the function to suddenly stop with no output?



Solution 1:[1]

What is the memory size configuration if the timeout is correct it might be the memory that is hampering the performance

Solution 2:[2]

It possible when lambda abruptly exits in one of the code path. Something in lines of System.exit() in Java.

In JS, the lambda are run on loop to consume the event. If your fish function closes the lambda environment by maybe calling end of runtime / closing the socket on which the response is supposed to be sent. Lambda will finish there itself without sending response or timeout.

req.on('socket', function (socket) { socket.unref() })

Solution 3:[3]

So from the following tutorial covering how to get lambdas and api-gateway working using CDK, I managed to isolate that without the following line will result in the 502 BAD GATEWAY error experienced, with the suggested return type as described. It's in the new apigateway.RestApi props.

defaultCorsPreflightOptions: {
...
        allowOrigins: ['http://localhost:3000'],
      },

The op doesn't specify his infrastructure propositioning method. If not using the CDK and using Cloud Formation YAML then it's probably related to the equivalent in the expanded YAML (although the net result of the expansion is beyond my competency).

method.response.header.Access-Control-Allow-Origin

BrokerAPItest41BB435C:
    Type: AWS::ApiGateway::Resource
    Properties:
      ParentId: !GetAtt 'BrokerAPID825C3CC.RootResourceId'
      PathPart: test
      RestApiId: !Ref 'BrokerAPID825C3CC'
    Metadata:
      aws:cdk:path: BrokerAwsDeployStack/BrokerAPI/Default/test/Resource
  BrokerAPItestOPTIONS843EE5C3:
    Type: AWS::ApiGateway::Method
    Properties:
      HttpMethod: OPTIONS
      ResourceId: !Ref 'BrokerAPItest41BB435C'
      RestApiId: !Ref 'BrokerAPID825C3CC'
      AuthorizationType: NONE
      Integration:
        IntegrationResponses:
          - ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: '''Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'''
              method.response.header.Access-Control-Allow-Origin: '''http://localhost:3000'''
              method.response.header.Vary: '''Origin'''
              method.response.header.Access-Control-Allow-Methods: '''OPTIONS,GET,PUT,POST,DELETE,PATCH,HEAD'''
            StatusCode: '204'
        RequestTemplates:
          application/json: '{ statusCode: 200 }'
        Type: MOCK
      MethodResponses:
        - ResponseParameters:
            method.response.header.Access-Control-Allow-Headers: true
            method.response.header.Access-Control-Allow-Origin: true
            method.response.header.Vary: true
            method.response.header.Access-Control-Allow-Methods: true
          StatusCode: '204'
    Metadata:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 priyanshu kumar
Solution 2 Lunarantic
Solution 3 Daniel Gerson