Node8 Lambda function fails with "Internal Server Error" and no logs

I’m writing a function to be deployed to AWS Lambda using node 8 (it’s OSS, take a look: https://github.com/hacksecure/utility-serverless-functions). It runs perfectly on local (npm run dev) but when deployed, all I get is internal server error. The Cloudwatch logs have nothing besides the logs showing the request started, ended and specifications on the billing. The issue looks similar to this but pinning my version of Serverless to 1.25.x (as you can see in the package.json) did nothing. I do not have serverless globally installed so I know there’s no sort of weird version conflict.

I can’t find any good examples of anything but the simplest Promise based lambda functions so it’s very possible that my code has some flaw that serverless-offline can’t spot. Any thoughts?

I’ve had this error when my response didn’t have a “statusCode” property. Then I had the the same error when trying to add extra fields to the response, later learning that my data has to go under the “body” property. Maybe you are having the same issue :slight_smile:
In the end I created a response wrapper:

return {
    statusCode: status,
    body: JSON.stringify(data),
    headers: {
        "Content-Type": "application/json"
    }
};

EDIT: Just checked your repo, doesn’t your function have to be async?

I took inspiration from here which returned a promise but wasn’t async. Regardless, adding async did not change anything.

I added the content-type and that also did not help (although I should have that anyways so at least it’s there now).

  1. Change line 21 from reject to resolve https://github.com/hacksecure/utility-serverless-functions/blob/master/handlers.js
  2. Change line 16 of https://github.com/hacksecure/utility-serverless-functions/blob/master/handlers.js to body: JSON.stringify(parseMLHSite(data)) as parseMLHSite() returns a JSON object and body is supposed to contain a string.
1 Like

The second one fixed my issue - thanks so much!!

Make sure you do both. If you reject the promise then the API GW will detect that Lambda failed and send back an error message but not the one you’re trying to send. You need to resolve and set the response to be your error.

1 Like

I am using MySQL and Typescript

export const getAll = async (event: APIGatewayEvent, context: Context) => {

const accountId = event.pathParameters.accountId;
const data = await getConditionBookmarks(accountId); // I use Promise wrapped around sql query

return {
    statusCode: 200,
    body: JSON.stringify(data)
}

}

The code works in local ( sls offline ) , but after I deployed , I got { message : ‘Internal Server Error’ }

Have you looked at the CloudWatch Logs to see if the Lambda is returning successfully or failing?

MySQL connection timeout … don’t know why

"error": {
    "code": "PROTOCOL_INCORRECT_PACKET_SEQUENCE",
    "fatal": true
  }

Stackoverflow suggest edit awsm.json ( where is it ? )
“exclude”: [
“aws-sdk”,
“mysql”
],
“includePaths”: [
“node_modules/mysql”,
“node_modules/bignumber.js”,
“node_modules/readable-stream”,
“node_modules/isarray”,
“node_modules/core-util-is”,
“node_modules/inherits”,
“node_modules/string_decoder”
]

I’d start with the basics:

  1. Is your Lambda running inside a VPC?
  2. Is it the correct VPC (just in case you accidentally copied the wrong one)?
  3. Have you assigned it a security group or groups?
  4. Are you certain those security groups are allowed to access RDS?

If you’re 100% certain your configuration is correct them consider spinning up an EC2 instance with the same config and test it can connect.

  1. Is the first line in your handler context.callbackWaitsForEmptyEventLoop = false;? If not then add it.
2 Likes

So the MySQL DB is in another ASW root account , I created a new one just for testing serverless framework ,
but I am able to connect from my local environment and working normally.

I tried context.callbackWaitsForEmptyEventLoop = false;
still not working : ((

Based on your responses I would focus on security groups. It sounds like your Lambdas don’t have access to the database because the security groups are blocking it.

Which policy should I use ? I am using “AdministratorAccess”

Return this. Use JSON.stringify(body, null, 2)

return {
    statusCode,
    body: JSON.stringify(body, null, 2)
  }