AWS Lambda - Create Thumbnail - node.js

I am using AWS Lambda for creating resized thumbnails. The source is an S3 bucket and the destination is another S3 bucket. I was able to create the lambda function from Amazon CLI. The function is triggered whenever a new image is added to the source bucket. But, the resize/thumbnail creation is not working. I can see some issue in the CloudWatch Console.
The content of the uploaded ZIP package is:
CreateThumbnail.js, node_modules
Unable to import module 'CreateThumbnail': Error
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/CreateThumbnail.js:2:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
Best regards,
BP

Finally able to find the source of the bug. The command used to make the zip file was causing the issue.
The correct command is:
zip -r CreateThumbnail.zip *

Related

How do I use a custom AWS SDK with nodejs?

I want to make a nodejs application that uses the AWS SDK. However, it only really needs to use S3, and I want the application to have a small filesize.
AWS has a custom sdk builder which should make a custom SDK with only the required packages. Unfortunately, this doesn't work, even when I select all of the services. How do I make a custom SDK that works with node?
I've tried using the npm install aws-sdk, and that works, but it's 40MB. I've also tried selecting all of the services in the service builder (only 3MB), but that doesn't work.
It always gives an error at this line:
var aws = require("./aws-sdk-2.462.0.min.js");
ReferenceError: AWS is not defined
at Object.<anonymous> (/home/cwaugh/workspaces/meta-lambda/aws-sdk-2.462.0.min.js:7:30180)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/cwaugh/workspaces/meta-lambda/handler.js:5:11)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
That's the browser SDK. For Node you can use this module which only provides an S3 Client. That page has a clear example showing how to call putObject using this custom SDK.
There's many different individual modules for a variety of AWS services. You can find more information here.
This is now possible with AWS SDK for JavaScript v3
You can install only the required packages as follows :
npm install #aws-sdk/client-s3
Use them as :
const {S3} = require('#aws-sdk/client-s3');

Serverless Node TypeScript app can't import module

I'm tired of this error! This is Serverless app with Lambda in AWS. Locally it works perfectly. Testing locally didn't work for load of other reasons. I'm testing in staging. Yet I'm getting this error:
Unable to import module 'src/engine/handler': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous>(/var/task/src/engine/controllers/image.js:43:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I followed all the steps advised elsewhere including: moving all dependencies to dependencies (out of dev), installing serverless and all. I'm using Node 6.10.3 and TypeScript 3.1. Can you help?
If you're trying host your nodejs projects which has so many dependencies not related to aws services inside, you better zip your projects in S3 first then import lambda from S3.

Cannot find librdkafka on Heroku

I am trying to get a simple Kafka consumer up and running on Heroku. I am using Node 8.11 and node-rdkafka. I have it running locally. When I try to run it on Heroku, I get this error:
Error: librdkafka++.so.1: cannot open shared object file: No such file or
directory
at Object.Module._extensions..node (module.js:681:18)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at bindings (/app/node_modules/bindings/bindings.js:81:44)
at Object.<anonymous> (/app/node_modules/node-rdkafka/librdkafka.js:10:32)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
I know the library is a wrapper for librdkafka and it looks like it just can't find it. According to the list of things preinstalled on Heroku Ubuntu instance, it looks like librdkafka-dev and librdkafka1 are installed. I don't know how to use those, or if something else is going on.
You have to use the Confluent librdkafka package.
Add this to your Aptfile:
:repo:deb [arch=amd64] http://packages.confluent.io/deb/4.0 stable main
Still having issues after doing this, but they are different issues and it looks like I'm closer.

Including node modules in AWS Lambda

I am trying to create a nodejs lambda function with the web3 module.
However, when zipping my handler.js and the entire node_modules directory and testing the lambda, the function cannot find web3.
Structure of the zip file:
handler.js
node_modules/
-web3_module_1/
-web3_module_2/
-other_modules_used_by_web3/
According to this source, the zip file structure should be the following:
handler.js
web3_module_1/
web3_module_2/
other_module/
But even with this structure, I get an error:
Unable to import module 'handler': Error
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/handler.js:3:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
What is the correct way to achieve this?

Unable to import module 'index': Error in aws cloud watch logs while using claudia.js

Unable to import module 'index': Error in aws cloud watch logs.
upload lambda using claudia works fine but api does not work and cloud watch logs displays following errors:
Unable to import module 'index': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/node_modules/request/request.js:12:21)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I have some code that only works in local node version (so it passes validation locally), but fails once it goes to server version. use nvm to run node locally for lambda projects and you'll catch those errors before deployment
so running : "npm install npm" in project directory solves the issue. then uploaded that again to aws using claudia.
all works fine.
thanks Gojko Adzic.

Resources