簡體   English   中英

為什么我的 AWS Lambda function 在沒有超時消息的情況下結束?

[英]Why is my AWS Lambda function ending before finishing with no timeout message?

我一直在使用 AWS Lambda 並使用 SAM local 進行測試近一年,沒有出現重大問題。 然而,我已經寫了一個 Lambda function 修改了一些文件與 S3 API。

function 以 502: Invalid lambda 結束 response received: Lambda returned <class 'NoneType'> instead of dict

這是在我的 function 有機會完成之前......我已經設法將代碼壓縮為以下內容:

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

無論我是在 SAM Local 中運行它還是上傳到 AWS Lambda,我都會得到這個 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 -

我已將此 Lambda function 配置為超時幾分鍾,並且我不在“上下文”中調用任何函數

我花了幾個小時試圖弄清楚 Lambda function 如何在沒有任何錯誤消息(來自我的代碼)或超時通知的情況下結束。

這是已知行為嗎? 有誰知道我如何找出導致 function 突然停止而沒有 output 的原因?

什么是 memory 大小配置如果超時正確,它可能是阻礙性能的 memory

當 lambda 突然退出其中一個代碼路徑時,可能會出現這種情況。 Java 中的 System.exit() 行中的某些內容。

在 JS 中,lambda 循環運行以消耗事件。 如果您的魚 function 通過調用運行時結束/關閉應該發送響應的套接字來關閉 lambda 環境。 Lambda 將自行完成而不發送響應或超時。

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

因此,從以下介紹如何使用 CDK 使 lambdas 和 api-gateway 工作的教程中,我設法隔離了如果沒有以下行將導致遇到 502 BAD GATEWAY 錯誤,並使用所描述的建議返回類型。 它在new apigateway.RestApi道具中。

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

該操作沒有指定他的基礎設施提議方法。 如果不使用 CDK 並使用 Cloud Formation YAML,那么它可能與擴展的 YAML 中的等效項相關(盡管擴展的 .net 結果超出了我的能力范圍)。

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:

async添加到 function 定義以解決問題

 module.exports.default = async () => {
  return {
    statusCode: 200,
  };
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM