aws-param-store
aws-param-store copied to clipboard
getParameterSync results in Runtime.UserCodeSyntaxError: SyntaxError: Unexpected end of JSON input
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"
]
}
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)
I've hit a problem very similar to yours, and I think I know the reason.
Look here: https://github.com/vandium-io/aws-param-store/blob/00c426239376c3b0ec901e971af9fb96c601b96f/lib/param_query.js#L125
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: https://github.com/vandium-io/aws-param-store/blob/00c426239376c3b0ec901e971af9fb96c601b96f/lib/param_query.js#L131 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' },
],
}),
],