Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 654
thinkOfaNumber Avatar asked Feb 10 '21 02:02

thinkOfaNumber


People also ask

Is AWS SDK available on Lambda?

Short description. To integrate the latest version of an AWS SDK into your Lambda function's deployment package, create a Lambda layer, and then add it to your function. You can use either the AWS Command Line Interface (AWS CLI) or the Lambda console to create a Lambda layer and add it to your function.

What version of node does AWS Lambda support?

You can now develop AWS Lambda functions using the Node. js 16 runtime. This version is in active LTS status and considered ready for general use.

What does AWS Lambda support?

Lambda runs your code on high availability compute infrastructure and performs all the administration of your compute resources. This includes server and operating system maintenance, capacity provisioning and automatic scaling, code and security patch deployment, and code monitoring and logging.


1 Answers

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).

like image 99
Radzikowski Avatar answered Oct 19 '22 18:10

Radzikowski