Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getParameterSync results in Runtime.UserCodeSyntaxError: SyntaxError: Unexpected end of JSON input #25

Open
rosshettel opened this issue Jun 8, 2021 · 2 comments

Comments

@rosshettel
Copy link

This happens on AWS lambda Node 14.x. Running the same code on our ECS service did not result in this error. Not sure exactly what is causing it, but switching to the async call getParameter fixed the error for us, so seems like there is a bug in the sync version of getParameter.

Here's the full stack trace, but it's not very helpful:

{
    "errorType": "Runtime.UserCodeSyntaxError",
    "errorMessage": "SyntaxError: Unexpected end of JSON input",
    "stack": [
        "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected end of JSON input",
        "    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:1063:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)",
        "    at Module.load (internal/modules/cjs/loader.js:928:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:769:14)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}
@rosshettel
Copy link
Author

Thought I'd check out the 4.0.0-Beta1 tag since I saw it reworked the sync calls, but alas I got another error:

{
  "errorType": "Error",
  "errorMessage": "internal/modules/cjs/loader.js:888\n  throw err;\n  ^\n\nError: Cannot find module '/var/task/1807'\n    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)\n    at Function.Module._load (internal/modules/cjs/loader.js:730:27)\n    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)\n    at internal/main/run_main_module.js:17:47 {\n  code: 'MODULE_NOT_FOUND',\n  requireStack: []\n}\n",
  "trace": [
    "Error: internal/modules/cjs/loader.js:888",
    "  throw err;",
    "  ^",
    "",
    "Error: Cannot find module '/var/task/1807'",
    "    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:730:27)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)",
    "    at internal/main/run_main_module.js:17:47 {",
    "  code: 'MODULE_NOT_FOUND',",
    "  requireStack: []",
    "}",
    "",
    "    at findPort (/var/task/index.js:138794:11)",
    "    at start (/var/task/index.js:138763:16)",
    "    at sendMessage (/var/task/index.js:138864:17)",
    "    at createClient (/var/task/index.js:138904:27)",
    "    at Object.<anonymous> (/var/task/index.js:111186:17)",
    "    at __webpack_require__ (/var/task/index.js:20:30)",
    "    at Object.<anonymous> (/var/task/index.js:82354:24)",
    "    at __webpack_require__ (/var/task/index.js:20:30)",
    "    at Object.<anonymous> (/var/task/index.js:77604:73)",
    "    at __webpack_require__ (/var/task/index.js:20:30)"
  ]
}

This is my code for reference:

    import paramStore from 'aws-param-store'

    const param = paramStore.getParameterSync(key)

@avysk
Copy link

avysk commented Sep 27, 2021

I've hit a problem very similar to yours, and I think I know the reason.

Look here:

let result = spawnSync( 'node', [ __dirname + '/ssm_sync' ], {

What the code tries to do is to spawn another node process and communicate with it over stdin/stdout. Basically, the library tries to run node ssm_sync and read the output. When you run the code locally, there's ssm_sync.js inside node_modules, so it just works. However, webpack has no idea that this file has to be added, and node ssm_sync in lambda just crashes with Error: Cannot find module blah-blah-blah. All that output goes to stderr; stdout is empty. But param_query.js does this:

let queryResult = JSON.parse( result.stdout.toString() );
without checking for error conditions, and as a result calls JSON.parse() on empty string, which produces exactly the error message we see. I think (didn't try) that the problem can be resolved by telling webpack that ssm_sync.js, ssm.js and aws_paginated_call.js files have to be copied next to what webpack produces.

Update: I've added the following to webpack config, and the problem is gone:

  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        { from: 'node_modules/aws-param-store/lib/ssm_sync.js' },
        { from: 'node_modules/aws-param-store/lib/ssm.js' },
        { from: 'node_modules/aws-param-store/lib/aws_paginated_call.js' },
      ],
    }),
  ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants