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

I am trying to execute a couple of asynchronous requests from a function lambda. The first call resolveEndpoints()succeeds, and the second with an error

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

Handler:

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)});
};

If I delete the call resolveEndpoint(), it getAuthToken()will be successful.

resolveEndpoint() returns a promise that is resolved upon completion of the request.

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);
        });
    });
};
+4
source share
1 answer

Lambda's default timeout is 3 seconds, and I hit this outside of a single HTTP call. You just need to update the SAM template to increase the timeout for handlers who need to call several third-party services.

An updated template with a timeout set to 10 seconds allows the handler to start until completion.

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
+2

Source: https://habr.com/ru/post/1688655/


All Articles