最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Runtime.UserCodeSyntaxError" while trying to declare an async AWS Lambda function - Stack Overfl

programmeradmin6浏览0评论

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._pile (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.

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._pile (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.

Share Improve this question edited Aug 5, 2020 at 11:02 almarc asked Aug 5, 2020 at 10:25 almarcalmarc 1,6583 gold badges14 silver badges32 bronze badges 4
  • looks like you need to add more code for us to debug (ie around line 98) – LostJon Commented Aug 5, 2020 at 12:33
  • 2 I don't have 98 lines of code. That's why I said the error messages are unhelpful. – almarc Commented Aug 5, 2020 at 12:34
  • is the code above the only code of your lambda? – LostJon Commented Aug 5, 2020 at 12:35
  • your getSong function on the bottom bit of code is not async...are you expecting to handle anotherAsyncFunction using await or are you going to use .then in conjunction with callback – LostJon Commented Aug 5, 2020 at 12:37
Add a ment  | 

1 Answer 1

Reset to default 4

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论