alwaysAStudent
alwaysAStudent

Reputation: 2264

Import Module via AWS Lambda Console Editor

Trying out an application in NodeJS. Have the following imports

var https = require('https'),
    aws4  = require('aws4')

However, get the error

"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'aws4'",

Code works when execute locally on desktop after installing aws4 using npm install aws4. How to install a module when editing lambda script in aws console lambda editor?

Upvotes: 1

Views: 3583

Answers (1)

Thales Minussi
Thales Minussi

Reputation: 7215

You can't add modules from Lambda's console. You will have to use a package manager (like npm or yarn) and install the dependencies you need. This means declaring them in package.json and running npm/yarn install before uploading your function to AWS Lambda. A node_modules folder will be generated with all the packaged dependencies inside it. Zip it and upload it.

Your package.json should look something like this:

{
  "name": "your-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws4": "^1.8.0",
    "https": "^1.0.0"
  }
}

If you want to make your deployment life easier, you may want to take a look into AWS SAM and the Serverless Framework.

Keep in mind that if your package is too large (and too large,for AWS's console is only 2MB 3MB according to the docs) after the dependencies are added you may lose the ability to edit the code inline through AWS's console, which means you'd have to use your own editor/IDE to edit the code before uploading it.

Upvotes: 4

Related Questions