Is dynamodb-geo supported in lambda running Node? - node.js

I'm trying to create a simple application following this article, but I cannot get my Node Lambda function to find the dynamodb-geo package.
Here is what I have:
const AWS = require('aws-sdk');
const ddbGeo = require('dynamodb-geo');
exports.handler = async (event, context) => {
// Rest of the code here
};
And the error Lambda throws is:
START RequestId: 5d40d132-040f-447d-bd76-35c4cec0236a Version: $LATEST
2019-10-05T10:04:24.719Z undefined ERROR Uncaught
Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error:
Cannot find module
'dynamodb-geo'","stack":["Runtime.ImportModuleError: Error: Cannot
find module 'dynamodb-geo'"," at _loadUserApp
(/var/runtime/UserFunction.js:100:13)"," at
Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","
at Object. (/var/runtime/index.js:45:30)"," at
Module._compile (internal/modules/cjs/loader.js:778:30)"," at
Object.Module._extensions..js
(internal/modules/cjs/loader.js:789:10)"," at Module.load
(internal/modules/cjs/loader.js:653:32)"," at tryModuleLoad
(internal/modules/cjs/loader.js:593:12)"," at Function.Module._load
(internal/modules/cjs/loader.js:585:3)"," at
Function.Module.runMain (internal/modules/cjs/loader.js:831:12)","
at startup (internal/bootstrap/node.js:283:19)"]} END RequestId:
5d40d132-040f-447d-bd76-35c4cec0236a REPORT RequestId:
5d40d132-040f-447d-bd76-35c4cec0236a Duration: 1146.75 ms Billed
Duration: 1200 ms Memory Size: 512 MB Max Memory Used: 35 MB Unknown
application error occurred Runtime.ImportModuleError
Any clue on what could be happening?

The only included package on AWS Lambda is the aws-sdk package. Everything else (except standard node packages) needs to be packaged and uploaded with your code.
There are many tools to achieve this:
the AWS cli (see https://stackoverflow.com/questions/34437900...)
the Serverless framework
AWS Amplify
AWS CDK.

Have you installed the package?
Using npm or yarn: npm install --save dynamodb-geo or yarn add dynamodb-geo.
Doc: https://www.npmjs.com/package/dynamodb-geo

You can use a Lambda layer in order to easily import external packages:

Related

AWS SAM Lambda Application Development + AWS SDK -- Can't get a working example

I’m fairly new to AWS Lambda and I’m trying to set-up my environment to be able to develop a Lambda function, using vscode and to be able to use a debugger. The way to do this appears to be to develop a SAM (serverless application model) application, containing the lambda function. So I’ve set everything up as per the “hello world” example in the AWS tutorial, and that all seems to work OK.
I have also created a standalone Lambda Function that successfully accesses the AWS-SDK to display some EC2 instance information (see below).
// Standalone Lambda Function that Works OK
const AWS = require('aws-sdk')
AWS.config.update({region: 'eu-west-2'});
const ec2 = new AWS.EC2();
exports.handler = async function(event) {
return ec2.describeInstances().promise();
}
The problem I’m having is combining the two: that is accessing the aws-sdk from a SAM application. What isn’t helping is that when I try and use one of the pre-provided SAM templates that do access the aws-sdk (e.g. the S3 one), they won’t even build.
So I guess I have two basic questions:
Is SAM the answer to being able to run and debug Node.js applications locally?
And if so, how do I get a working SAM application that uses the ‘aws-sdk’?
I’m on the latest version of everything. Node.js, SAM CLI, etc. The development machine is an ubuntu VM running on an ubuntu host. But the non aws-sdk example seems to work fine.
Example of the example SAM application not building.
$ sam init
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
What package type would you like to use?
1 - Zip (artifact is a zip uploaded to S3)
2 - Image (artifact is an image uploaded to an ECR image repository)
Package type: 1
Which runtime would you like to use?
1 - nodejs14.x
2 - python3.8
3 - ruby2.7
4 - go1.x
5 - java11
6 - dotnetcore3.1
7 - nodejs12.x
8 - nodejs10.x
9 - python3.7
10 - python3.6
11 - python2.7
12 - ruby2.5
13 - java8.al2
14 - java8
15 - dotnetcore2.1
Runtime: 1
Project name [sam-app]: sam-app-sdk-test
Cloning app templates from https://github.com/aws/aws-sam-cli-app-templates
AWS quick start application templates:
1 - Hello World Example
2 - Step Functions Sample App (Stock Trader)
3 - Quick Start: From Scratch
4 - Quick Start: Scheduled Events
5 - Quick Start: S3
6 - Quick Start: SNS
7 - Quick Start: SQS
8 - Quick Start: Web Backend
Template selection: 5
-----------------------
Generating application:
-----------------------
Name: sam-app-sdk-test
Runtime: nodejs14.x
Dependency Manager: npm
Application Template: quick-start-s3
Output Directory: .
Next steps can be found in the README file at ./sam-app-sdk-test/README.md
The build errors
sam build
Building codeuri: /media/sf_Virtual-machine-shr/DEV-Projects/OaC/sam-app-sdk-test runtime: nodejs14.x metadata: {} functions: ['S3Json
LoggerFunction']
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrc
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Build Failed
Error: NodejsNpmBuilder:NpmInstall - NPM Failed: npm WARN deprecated request#2.88.2: request has been deprecated, see https://github.c
om/request/request/issues/3142
npm WARN deprecated request-promise-native#1.0.9: request-promise-native has been deprecated because it extends the now deprecated req
uest package, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator#5.1.5: this library is no longer supported
npm WARN deprecated resolve-url#0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated urix#0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#^2.1.2 (node_modules/jest-haste-map/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#2.3.2: wanted {"os":"darwin","arch":"any"} (current: {
"os":"linux","arch":"x64"})
npm ERR! Maximum call stack size exceeded
npm ERR! A complete log of this run can be found in:
npm ERR! /home/peter/.npm/_logs/2021-04-25T14_35_13_711Z-debug.log
Turning the working Lambda Function Into a SAM Application
If I take my working Lambda Function and try to deploy it as a SAM application that builds and deploys OK, but won’t run.
Running locally
$ sam local invoke
Invoking app.lambdaHandler (nodejs14.x)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-nodejs14.x:rapid-1.22.0.
Mounting /media/sf_Virtual-machine-shr/DEV-Projects/OaC/backups/nc-sam-awsbackup-snapshots/.aws-sam/build/ShareSnapshotsFunction as /var/task:ro,delegated inside runtime container
START RequestId: bf15ac81-7d54-48ad-bbb7-df5e196c09f0 Version: $LATEST
2021-04-25T14:28:27.244Z undefined ERROR Uncaught Exception {"errorType":"Runtime.HandlerNotFound","errorMessage":"app.lambdaHandler is undefined or not exported","stack":["Runtime.HandlerNotFound: app.lambdaHandler is undefined or not exported"," at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:1063:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)"," at Module.load (internal/modules/cjs/loader.js:928:32)"," at Function.Module._load (internal/modules/cjs/loader.js:769:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)"," at internal/main/run_main_module.js:17:47"]}
time="2021-04-25T14:28:27.252" level=panic msg="ReplyStream not available"
2021/04/25 14:28:27 http: panic serving 127.0.0.1:44020: &{0xc0000f4000 map[] 2021-04-25 14:28:27.252909676 +0000 UTC m=+0.322609308 panic <nil> ReplyStream not available <nil> <nil> }
goroutine 30 [running]:
<Stuff Cut Out>
2021-04-25T14:28:27.266Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"socket hang up","code":"ECONNRESET","stack":["Error: socket hang up"," at connResetException (internal/errors.js:607:14)"," at Socket.socketOnEnd (_http_client.js:493:23)"," at Socket.emit (events.js:327:22)"," at Socket.EventEmitter.emit (domain.js:467:12)"," at endReadableNT (internal/streams/readable.js:1327:12)"," at processTicksAndRejections (internal/process/task_queues.js:80:21)"]}
time="2021-04-25T14:28:27.288" level=error msg="Init failed" InvokeID= error="Runtime exited with error: exit status 129"
time="2021-04-25T14:28:27.288" level=error msg="INIT DONE failed: Runtime.ExitError"
Using "Invoke on AWS" from vscode.
Loading response...
Invocation result for arn:aws:lambda:eu-west-2:428829672862:function:nc-sam-awsbackup-snapshots-ShareSnapshotsFunction-Z9A1RR2GCQ9F
Logs:
START RequestId: f7a7d779-8ebd-4dde-987e-775f6116c2d4 Version: $LATEST
2021-04-25T14:21:24.159Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","code":"EACCES","errno":-13,"syscall":"open","path":"/var/task/app.js","stack":["Error: EACCES: permission denied, open '/var/task/app.js'"," at Object.openSync (fs.js:476:3)"," at Object.readFileSync (fs.js:377:35)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)"," at Module.load (internal/modules/cjs/loader.js:928:32)"," at Function.Module._load (internal/modules/cjs/loader.js:769:14)"," at Module.require (internal/modules/cjs/loader.js:952:19)"," at require (internal/modules/cjs/helpers.js:88:18)"," at _tryRequire (/var/runtime/UserFunction.js:75:12)"," at _loadUserApp (/var/runtime/UserFunction.js:95:12)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}
END RequestId: f7a7d779-8ebd-4dde-987e-775f6116c2d4
REPORT RequestId: f7a7d779-8ebd-4dde-987e-775f6116c2d4 Duration: 1439.52 ms Billed Duration: 1440 ms Memory Size: 128 MB Max Memory Used: 14 MB
Unknown application error occurred
Error
Payload:
{"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","trace":["Error: EACCES: permission denied, open '/var/task/app.js'"," at Object.openSync (fs.js:476:3)"," at Object.readFileSync (fs.js:377:35)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)"," at Module.load (internal/modules/cjs/loader.js:928:32)"," at Function.Module._load (internal/modules/cjs/loader.js:769:14)"," at Module.require (internal/modules/cjs/loader.js:952:19)"," at require (internal/modules/cjs/helpers.js:88:18)"," at _tryRequire (/var/runtime/UserFunction.js:75:12)"," at _loadUserApp (/var/runtime/UserFunction.js:95:12)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}
Loading response...
Invocation result for arn:aws:lambda:eu-west-2:428829672862:function:nc-sam-awsbackup-snapshots-ShareSnapshotsFunction-Z9A1RR2GCQ9F
Logs:
START RequestId: 779c6351-0775-4f04-8a5e-987791b027bf Version: $LATEST
2021-04-25T14:31:26.397Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","code":"EACCES","errno":-13,"syscall":"open","path":"/var/task/app.js","stack":["Error: EACCES: permission denied, open '/var/task/app.js'"," at Object.openSync (fs.js:476:3)"," at Object.readFileSync (fs.js:377:35)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)"," at Module.load (internal/modules/cjs/loader.js:928:32)"," at Function.Module._load (internal/modules/cjs/loader.js:769:14)"," at Module.require (internal/modules/cjs/loader.js:952:19)"," at require (internal/modules/cjs/helpers.js:88:18)"," at _tryRequire (/var/runtime/UserFunction.js:75:12)"," at _loadUserApp (/var/runtime/UserFunction.js:95:12)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}
END RequestId: 779c6351-0775-4f04-8a5e-987791b027bf
REPORT RequestId: 779c6351-0775-4f04-8a5e-987791b027bf Duration: 1449.59 ms Billed Duration: 1450 ms Memory Size: 128 MB Max Memory Used: 14 MB
Unknown application error occurred
Error
Payload:
{"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","trace":["Error: EACCES: permission denied, open '/var/task/app.js'"," at Object.openSync (fs.js:476:3)"," at Object.readFileSync (fs.js:377:35)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)"," at Module.load (internal/modules/cjs/loader.js:928:32)"," at Function.Module._load (internal/modules/cjs/loader.js:769:14)"," at Module.require (internal/modules/cjs/loader.js:952:19)"," at require (internal/modules/cjs/helpers.js:88:18)"," at _tryRequire (/var/runtime/UserFunction.js:75:12)"," at _loadUserApp (/var/runtime/UserFunction.js:95:12)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}
Running the Lambda Function within the AWS Console
Gives the same EACCES: permission denied

How to fix nodejs version issue in Aws-lambda function

I am trying to resize my images with the help of Aws lambda and getting error
and i am following this article
https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
{
"errorType": "Error",
"errorMessage": "The module '/var/task/node_modules/sharp/build/Release/sharp.node'\nwas compiled against a different Node.js version using\nNODE_MODULE_VERSION 48. This version of Node.js requires\nNODE_MODULE_VERSION 64. Please try re-compiling or re-installing\nthe module (for instance, using `npm rebuild` or `npm install`).",
"trace": [
"Error: The module '/var/task/node_modules/sharp/build/Release/sharp.node'",
"was compiled against a different Node.js version using",
"NODE_MODULE_VERSION 48. This version of Node.js requires",
"NODE_MODULE_VERSION 64. Please try re-compiling or re-installing",
"the module (for instance, using `npm rebuild` or `npm install`).",
" at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)",
" at Module.load (internal/modules/cjs/loader.js:600:32)",
" at tryModuleLoad (internal/modules/cjs/loader.js:539:12)",
" at Function.Module._load (internal/modules/cjs/loader.js:531:3)",
" at Module.require (internal/modules/cjs/loader.js:637:17)",
" at require (internal/modules/cjs/helpers.js:22:18)",
" at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:8:15)",
" at Module._compile (internal/modules/cjs/loader.js:701:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)",
" at Module.load (internal/modules/cjs/loader.js:600:32)"
]
}
Steps to fix it: -
Download your project(function) form AWS Lambda using Action->Export function and unzip it.
Install Latest version of node in your local system(say node 8.10.0). You can see i already have 8.10.0.
Go to your unzipped project folder via terminal and type
npm rebuild
After this again zip this project and uplaod it on AWS Lambda and select the node runtime one you updated with (suppose it is node 8.10.0 then select runtime in aws lambda function runtime as node 8.10x).
Hope it helps.
Thanks

Not able to load Node JS function in AWS Lambda using Serverless Couchbase Lounge - getting invalid ELF header error

The platform architecture: Serverless AWS Lambda using Couchbase as database and connect using Lounge library.
It works fine when we tried serverless local invoke but upon deploying to AWS Lambda, getting the below error:
Error: /var/task/node_modules/couchbase/build/Release/couchbase_impl.node: invalid ELF header
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 (/var/task/node_modules/bindings/bindings.js:84:48)
at Object.<anonymous> (/var/task/node_modules/couchbase/lib/binding.js:213:36)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
The closer thing i came across is this: Invalid ELF header Node js with couchbase db
but not sure how to install platform specific in aws lambda. I ruled out the webpack and package dependecies.
Version:
node: 8.10.0
npm: 6.4.1
npm package couchbase: 2.6.4
npm lounge: 0.26.0
Couchbase and CouchDB are NOT the same database, Couchbase's Database is called "Couchbase Server". So, in summary you are trying to connect in a database using the driver of the other.
In this framework it explicitly says CouchDB
https://github.com/mikeyk/couchdb-lounge

Cannot deploy a NodeJS AWS Lambda Function. Error at Object.fs.openSync

so until a few days ago I used to deploy my Node.JS AWS Lambda functions via a short and simple shell script, which worked perfectly fine.
Now I do not know why or what I have done different, but for some reason after every deploy of my lambda function I now get the following error, when invoking the function via Alexa:
19:47:58
module initialization error Error
module initialization error
Error
19:47:59
START RequestId: 596b2724-68f9-11e8-aaa7-d9ff3044ed9d Version: $LATEST
START RequestId: 596b2724-68f9-11e8-aaa7-d9ff3044ed9d Version: $LATEST
19:47:59
module initialization error: Error at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object.Module._extensions..js (module.js:662:20)
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)
module initialization error: Error
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object.Module._extensions..js (module.js:662:20)
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)
19:47:59
END RequestId: 596b2724-68f9-11e8-aaa7-d9ff3044ed9d
19:47:59
REPORT RequestId: 596b2724-68f9-11e8-aaa7-d9ff3044ed9d
Duration: 218.74 ms Billed Duration: 300 ms
Memory Size: 128 MB Max Memory Used: 20 MB
REPORT RequestId: 596b2724-68f9-11e8-aaa7-d9ff3044ed9d
Duration: 218.74 ms Billed Duration: 300 ms
Memory Size: 128 MB Max Memory Used: 20 MB
Now here comes the part that really makes me clueless how I could solve this, (and it is probably something local on my Mac, I guess).
If I download / export a previously working deployment package from my Lambda function and directly reupload the Zip file, it works.
If I unzip the file and rezip it again , I get the error
without me changing anything about the code. That is what makes me wonder if there is something wrong with the zipping on my mac or maybe the file system?
I am using Mac OSX High Sierra, Node 6.10 / 8.10 via nvm. The code on lambda runs with 6.10 and 8.10
Any suggestions appreciated :)
Edit: The last working commit was done on a windows PC... I am slightly worried whether the file system change somehow breaks the zipping / upload. I will try and have a deeper look into that.
We were uploading a .zip archive of the code to AWS Lambda.
The problem for us was that the files inside the .zip file didn't have world-readable permissions.
Okay, only thing that was able to solve this, was going back to the last commit that I did from OSX and ignoring the last 3 commits that I did on windows.
It looks like there were some path or encoding changes that were introduced by committing on windows which resolved in this error. Unfortunately I cannot see those in the commit diffs.
So looking for differences between operating systems might be important here.
I was facing the same issue. This worked for me on Windows-
export the lambda function (which downloads a .zip file)
extract files
Zip those files again
Upload this new zip to the lambda
I have run into issues with Lambda and code from Windows machines in the past. The solution (for me) was to chmod -R 644 lambdaDirectory from outside the directory, fixing whatever Windows did to the permissions. I have no clue what would cause the download/reupload to work, maybe AWS is doing something to the manifest?
I have also had issues with zip files coming from other machines that use third party compression tools, as they compress the entire directory into an archive named the same instead of just the contents. IE you don't want lambdaDirectory/ to be included in your lambdaDirectory.zip. To solve this, run zip -r lambdaDirectory.zip ./* from inside the directory.

node require bluebird failing

I am running a node.js app and for some reason the node app is failing when requiring the bluebird module require('bluebird').
node version: 0.10.40
bluebird version: 3.3.4
This is really strange and the only thing I have to go off of is the stack trace here:
TypeError: Cannot call method 'split' of undefined
at Object.setBounds (app/node_modules/bluebird/js/release/debuggability.js:614:48)
at module.exports (app/node_modules/bluebird/js/release/promise.js:762:11)
at Object.<anonymous> (app/node_modules/bluebird/js/release/bluebird.js:9:36)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
var firstStackLines = firstLineError.stack.split("\n");
Again, the app runs just fine. When I add this one line require('bluebird') it crashes and I get this stack trace. Any ideas on what this error is coming from?
[Edit]
at line node_modules/bluebird/js/release/debuggability.js:614:48
> console.log(firstLineError);
[Error]
> console.log(firstLineError.message);
> console.log(firstLineError.stack);
undefined
> console.log(firstLineError.toString());
Error: Error
> console.log(firstLineError instanceof Error);
True
It is an Error object that is essentially empty
temporary solution
When I change the line node_modules/bluebird/js/release/debuggability.js:614 to
var firstStackLines = (firstLineError.stack) ? firstLineError.stack.split("\n") : "";
and line 615 to
var lastStackLines = (lastLineError.stack) ? lastLineError.stack.split("\n") : "";
it works just fine. Not sure if this has lasting effects though
I had the similar failures, more specifically:
.../node_modules/bluebird/js/release/debuggability.js:673
var firstStackLines = firstLineError.stack.split("\n");
when using node 6.2.2 with bluebird^3.4.1.
Upgrading to bluebird^3.4.6 with node v6.2.2 helped fix the error.
Manually remove your node_modules folder and re-run npm install as it sounds like NPM didn't correctly install something.
You're using the latest version of Bluebird with an outdated version of node.
The problem for me was that I was using another module pretty-error that was modifying the console object and therefore, bluebirds error handling was receiving a mutated console object that was not the shape that bluebird expected.

Resources