1

I have a very basic node.js example running AWS and I need to modify the code to add a dependency on the "request" module.

I have understood that you need to package this up into a zip file with the necessary node module.

I download the index.js and add the code. I create package.json:

{ "name": "function", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "zip": "zip function.zip package.json *.js node_modules" }, "dependencies": { "aws-sdk": "^2.4.10", "request": "^1.0" } }

I use npm install to pull in the dependency. This populated the node_modules subfolder with the module and its dependencies.

Then I just zipped this up but the AWS console would not upload it.

This Q&A Creating a lambda function in AWS from zip file told me not to zip normally but to use npm like so: ` npm run zip

> function@1.0.0 zip /Users/paul_tanner/Desktop/index
> zip function.zip package.json *.js node_modules

  adding: package.json (deflated 36%)
  adding: index.js (deflated 73%)
  adding: node_modules/ (stored 0%)`

Before trying to upload and test this I opened it up to check that the dependencies were included. They were not.

Just for the hell of it I also tried uploading the resulting "function.zip". Again, AWS Lambda would not upload it.

So the question is how should one create and upload an AWS Lambda function with dependency?

Community
  • 1
  • 1
user1712240
  • 131
  • 1
  • 2
  • 8

2 Answers2

0

The Linux zip utility has worked fine for me, Looking at the output of the zip command that you show, I think the only bit you are missing is the recursive option (-r). Try this:

# zip -r function.zip package.json *.js node_modules 

If the file is too big, you cannot upload it directly, in such cases use S3 to store you packaged function and finally load it into Lambda.

0

Another option is to "statically" link all of the modules into one big .js file and upload that.

This questions describes how: Packaging code for AWS Lambda

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245