Skip to content

Instantly share code, notes, and snippets.

@serg06
Last active February 5, 2020 03:13
Show Gist options
  • Save serg06/177a96cfe0a2432435de6132d62d5da5 to your computer and use it in GitHub Desktop.
Save serg06/177a96cfe0a2432435de6132d62d5da5 to your computer and use it in GitHub Desktop.
Amazon Lambda Node.js 12.x async handler function example
/* Grr, Amazon's documentation sucks!
* Their own documentation: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
* says that you're allowed to return strings, but that always results in "Internal Server Error"!
* In reality, your result has to be structured like the one you see below.
*/
exports.handler = async (event, context) => {
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({"here is": "an example"})
};
}
/* Note:
*
* They say you can return "a response, error, or promise."
*
* If you want to return a promise, do resolve(the same response format as above).
* If you want to return an error, not sure what exactly the response should look like,
* I'm guessing it should be the same but with statusCode set to some error status code.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment