"Runtime.UserCodeSyntaxError" while trying to declare an async AWS Lambda function

Viewed 4821

I've had an old AWS Lambda function, that was declared as synchronous (used Promises), the declaration looked like this:

exports.getSong = (event, context, callback) => { }

It worked as intended. Recently, I decided to re-write it using async/await and thus, tried declaring the getSong function asynchronously, like so:

exports.getSong = async (event, context) => { }

And while trying to execute, I get the following error in it's entirety:

{
  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Unexpected token '.'",
  "trace": [
    "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '.'",
    "    at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1133:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)",
    "    at Module.load (internal/modules/cjs/loader.js:977:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:877:14)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)",
    "    at internal/main/run_main_module.js:18:47"
  ]
}

It absolutely not clear what the issue is from this error message, but by googling for similar issues and narrowing everything down, I figured out that the problem is the function declaration.

I tried declaring the getSong function as synchronous, and then running another asynchronous function inside it, like so:

var anotherAsyncFunction = async () => { }
exports.getSong = (event, context, callback) => { anotherAsyncFunction() }
    

But then, I get the same error. So clearly, it has something to do with the Asynchronous function declaration inside the Lambda. What might be the issue here? Thank you.

1 Answers

I encounter a similar issue. for me it was res.Body?.toString('utf-8') which lambda could not understand what is Body?. i think in your case you should do the following :

export const getSong = async (event, context) => { ...}

also for whoever has the same problem, the best way to figure out the reason is to go to your lambda code page, it should show a red X on the line of code

Related