I have based a small lambda function off the cdk workshop here. I'm writing the lambda function in typescript, deploying via a pipeline which creates a cloud formation stack containing the lambda function.
I'm trying to use the sdk v3 in lambda, as demoed here. But then I see conflicting documentation here.
Are these errors because I'm trying to use V3 and I shouldn't, or for some other reason? The handler is set correctly, the function runs but fails with the error:
{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module '@aws-sdk/client-sns'\nRequire stack:\n- /var/task/ReceiveMessageLoraThing.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js", "stack": [ "Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk/client-sns'", "Require stack:", "- /var/task/ReceiveMessageLoraThing.js", "- /var/runtime/UserFunction.js", "- /var/runtime/index.js", " at _loadUserApp (/var/runtime/UserFunction.js:100: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:999:30)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)", " at Module.load (internal/modules/cjs/loader.js:863:32)", " at Function.Module._load (internal/modules/cjs/loader.js:708:14)", " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)", " at internal/main/run_main_module.js:17:47" ] } The file is deployed as js, with the correct handler set. If I comment out the require statement, it works fine:
// works "use strict"; //const sns = require("@aws-sdk/client-sns"); exports.handler = async (event) => { console.log("hello"); return true; } // doesn't work "use strict"; const sns = require("@aws-sdk/client-sns"); exports.handler = async (event) => { console.log("hello"); return true; } There are no node_modules or layers generated by using the code from this workshop, but before I go there I want to know if I can actually use V3 on lambda yet.
docker run --rm -it --entrypoint /bin/bash amazon/aws-lambda-nodejs[:version]. The pre-installed packages available in the Node 14 version are in/var/runtime/node_modules, which contains onlyaws-sdk.npmdepends on in/var/lang/lib/node_modules/npm/node_modules, but you can't require them by name from your handler, and it would probably be a bad idea to rely on them