Search code examples
aws-lambdaaws-sdk-js

Does AWS Lambda support aws-sdk v3 or not?


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.


Solution

  • Yes, you can use AWS SDK v3, just like any other JS library.

    The Node environment for Lambda comes with installed AWS SDK v2 (the previous one), as you can see here: https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html

    So to use v3, you should bundle it with your application as with any other dependency, and deploy the bundle (https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html).

    Actually, even with SDK v2 being available on the Lambda env, it was still a best practice to bundle it by yourself with your app and deploy it. This way you would always use the version you specified, and not the one that is on Lambda and can be updated any time. Even if they do not make any incompatible changes, it's always possible that the new SDK version, not tested with your specific app, will have some bug that will break your Lambda (very unlikely, but possible).

    Edit (2022-11-22): Node.js 18 Lambda runtime includes SDK v3 and does not include v2 (https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/).