Nodejs AWS Lambda Functions dependencies not installing - node.js

So I've created a lambda function which was developed in SAM. Now when I've pushed all the code to the lambda function however when I trigger it it seems like it doesn't install the necessary dependencies like mysql etc
Shouldn't it automatically npm install all the required libraries? Or do I need to manually push the node_modules dir to the lambda function as well?

You need to include the node_modules bundleded with your lambda function.
Lambda executes the code straight away without an install step.
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html

Related

Deploying Google Cloud Functions using private NPM modules

When you deploy a Google Cloud Function, using the Node.js runtime, you can supply your code in a .zip file in Google Cloud Storage (--source=gs://...).
I make the zip file using all the compiled source, and the node_modules/ folder, so that GCF has everything it needs. I do this since I'm using private NPM modules, so npm i will not work inside the GCF build routine (that runs on cloud deploy inside GCP).
Problem:
Even if I include 100% of the needed modules in node_modules/, GCF still has deployment errors, since it tries to access the private NPM package repo.
If I remove the package.json's dependencies and devDependencies section, GCF will fail to start, saying it cannot find the modules.
So:
Why can't GCF just use bundled dependencies like other tools do (e.g. GitHub Actions)
What is the best practice for deploying GCFs using private NPM modules?
the best way is to include in your code a file called .npmrc, in your npm account can generate an access token with some permissions and that token is configured in the file that I mentioned above, in that way when the deploy is made, the command npm install can download the private packages.
Cloud Functions just released support for private dependencies and the Node.js runtime.
This allows accessing private packages without passing .nomrc credentials to Cloud Functions.

Serverless deployment to AWS Lambda missing modules?

First time dealing with serverless here. Have successfully deployed using serverless deploy after following the guide to migrating an existing express app over to serverless. But aws lambda keeps throwing an error:
“errorType”: “Runtime.ImportModuleError”,
“errorMessage”: “Error: Cannot find module ‘serverless-http’\nRequire stack:\n- /var/task/app.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js”,
So I’m confused. What am I doing wrong? The guide to converting an existing express app didn’t say we’d need to create an AWS Lambda Layer, but since it seems like Lambda can’t find the serverless-http module, does it mean that creating a layer is the fix?
Edit: my .zip file seems to only be an express.js file. I'm not sure if that's unusual but reading online reveals that most people seem to have a node_modules folder zipped up as well? I presume the lack of a node_module folder in the .zip file is causing this runtime.ImportModuleError fail? How do I get serverless to add a node_modules folder if that's the case?
node_modules folder is generally packaged with your code -- provided it exists in your directory (does it?).
If node_modules isn't installed on your local machine, then you can create it by using the npm install command. This command will install all the dependencies listed in the package-lock.json file (or package.json) -- sorry I'm not a node guy :(.
But it definitely sounds like you're not uploading your node_modules folder because it's not on your local machine. You have to initialize the directory first.

how to deploy express node app in AWS Lambda along with npm modules?

I want to deploy my node app in AWS lambda. The npm packages I am using are not pre-installed in lambda. So how can I deploy the whole node app in lambda? There is a option of uploading the files as a zip file. But how can I build the correct lambda file system?
You need to zip the file contents. basically
somezip.zip
node_modules
index.js
anyotherffile.js
package.json
Upload the zip and call the exposed handler function.
You could also use framework such as - https://www.npmjs.com/package/serverless
I recommend you to have a look at the AWS reference documentation.
The following steps could be done:
~/my-function$ npm install
~/my-function$ zip -r function.zip .
~/my-function$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip
Look for node environments available on the following page from AWS.
Be aware of the limitations applicable, as Dec 27 2019 , if the deployment package is larger than 50 MB, you must use Amazon S3.

Installing external resources in Lambda Functions

I am using AWS Lambda and I want to install some external resources
npm install textapi
...
var TextAPI = require('textapi');
how can I install this when running my Lambda function.
Note I know I have to upload a zip file just not sure what else is needed to get this running.
Your zip file should contain your entire app, from a Node perspective that will include your node_modules folder.
See the docs.

Bundle Node.js dependancies in AWS Zip

Im creating a lambda function using Node.js, and Im looking to bundle the dependencies into my zip to be used in my Lambda function in AWS. Specifically Im using the ffmpeg library, and I've got it running locally using npm. Is there a way that I can bundle this dependency with the .zip file that I upload so that I don't have to configure the dependency in AWS?
Not only is there a way to bundle this into your deployment package, but you have to do it this way. Lambda functions can't download dependencies.
Here's the documenation: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html
Also you might want to look at this project: https://github.com/binoculars/aws-lambda-ffmpeg and possibly this thread: https://forums.aws.amazon.com/thread.jspa?messageID=680948 for more specific information about running ffmpeg on Lambda.

Resources