AWS lambda nodejs 运行时:io: read/write on closed pipe

AWS lambda nodejs runtime: io: read/write on closed pipe

我正在尝试从 lambda 函数执行几个异步请求。第一次调用 resolveEndpoints() 成功,第二次失败

2017/11/03 17:13:27 Function oauth.callbackHandler timed out after 3 seconds

2017/11/03 17:13:27 Error invoking nodejs6.10 runtime: io: read/write on closed pipe

处理程序是:

exports.callbackHandler = async (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;

    let endpoints: any = await resolveEnpoints();

    config.accessTokenUri = endpoints.token_endpoint;

    let tokenRequestPath =
    `http://localhost:7001${event.path}?code=${event.queryStringParameters.code}&realmId=${event.queryStringParameters.realmId}&`;

    let accessToken: any = await getAuthToken(tokenRequestPath);

    callback(undefined, {statusCode: 200, body: JSON.stringify(accessToken.data)});
};

如果我删除 resolveEndpoint() 调用,则 getAuthToken() 成功。

resolveEndpoint() returns 请求完成后立即解决的承诺。

const resolveEnpoints = async () => {
    return new Promise((resolve, reject) => {
        request({
            url: config.sandboxEndpoint,
            headers: {
                'Accept': 'application/json'
            }
        }, (err, response) => {
            if (err) {
                reject(err);
            }

            let payload = JSON.parse(response.body);
            resolve(payload);
        });
    });
};

Lambda 的默认超时为 3 秒,我在一次 HTTP 调用后达到了这个值。只需要更新 SAM 模板以增加需要调用多个第三方服务的处理程序的超时时间。

将超时设置为 10 秒的更新模板允许处理程序 运行 完成。

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description: |
  Data service

Resources:
  OAuthCallback:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs6.10
      CodeUri: ./build/services/quickbooks
      Handler: oauth2.callbackHandler
      Timeout: 10
      Events:
        AuthRoute:
          Type: Api
          Properties:
            Path: /oauth2/callback
            Method: get