AWS-serverless-express never resolving with promises
Make sure you are setting context.callbackWaitsForEmptyEventLoop
to false
read more about it here https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
The issue was regarding my testing tool:
https://www.npmjs.com/package/run-local-lambda
When I spoke with the AWS Support, they told me to use:
sam local invoke "Test" -e event.json
With a event.json file that includes the following:
{ "httpMethod": "OPTIONS", "//body": "{\"name\": \"Sam\"}", "path": "/api/auth", "resource": "/{proxy+}", "queryStringParameters": {}, "pathParameters": { "proxy": "users" }, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, sdch, br", "Accept-Language": "en-US,en;q=0.8", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "US", "Content-Type": "application/json", "Host": "xxxxxxxxxx.execute-api.us-east-1.amazonaws.com", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36", "Via": "1.1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)", "X-Amz-Cf-Id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxx==", "X-Forwarded-For": "11.111.111.111, 11.111.111.111", "X-Forwarded-Port": "111", "X-Forwarded-Proto": "http", "x-apigateway-event": "hej" }, "requestContext": { "accountId": "111111111111", "resourceId": "xxxxxx", "stage": "prod", "requestId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "identity": { "cognitoIdentityPoolId": "", "accountId": "", "cognitoIdentityId": "", "caller": "", "apiKey": "", "sourceIp": "11.111.111.111", "cognitoAuthenticationType": "", "cognitoAuthenticationProvider": "", "userArn": "", "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36", "user": "" }, "resourcePath": "/{proxy+}", "httpMethod": "GET", "apiId": "xxxxxxxxxx" }}
And a template file to use just for testing that looks like this:
AWSTemplateFormatVersion : '2010-09-09'Transform: AWS::Serverless-2016-10-31Description: A test projectResources: Test: Type: AWS::Serverless::Function Properties: Runtime: nodejs10.x Handler: index.handler Timeout: 10 Environment: Variables: NODE_ENV: "test" DB_NAME: "dbname" DB_USER: "dbuser" DB_PASSWORD: "secret" DB_URL: "dburl"
The code I use looks like this in the handler:
'use strict'const awsServerlessExpress = require('aws-serverless-express')const app = require('./main.js')const server = awsServerlessExpress.createServer(app, null) exports.handler = (event, context) => { return awsServerlessExpress.proxy(server, event, context,'PROMISE').promise}
Then my code executed wonderful with no timeouts ever.