deploying lambda with cdk doesn't recognize sdk nodejs v3 - aws-sdk

I am learning aws lambda, cdk and dynamodb and as a test am trying to update a lambda function code written in aws-sdk js v2 to v3, but after deploying it it doesn't work. I copy pasted the same code in a test lambda function directly in the web console and it worked fine.
Here is the error shown in cloudwatch:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module '#aws-sdk/client-dynamodb'\nRequire stack:\n- /var/task/hitcounter.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module '#aws-sdk/client-dynamodb'",
"Require stack:",
"- /var/task/hitcounter.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1072:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)",
" at Module.load (internal/modules/cjs/loader.js:937:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
" at internal/main/run_main_module.js:17:47"
]
}
is there a cdk related settings I have to set or change that allows me to use #aws-sdk v3? I don't want to use v2 since I am starting a project from start and rather use newer API's.

The AWS Lambda NodeJS environments come with AWS SDK v2.952.0 as of writing this. This is valid for any of the following runtimes: Node.js 10, Node.js 12, Node.js 14.
AWS has an official video on how to make a custom SDK version (including v3) available to your Lambda using Lambda layers.
Alternatively, since you use AWS CDK and the NodejsFunction construct, you can bundle it inside your Lambda artifact directly, by declaring it in the node_modules prop, as described here. Remember that you need to declare aws-sdk v3 as a dependency in your package.json file if you go this way.

Lambda environment doesn’t have SDK v3. You must bundle it. Use NodejsLambda construct which will bundle the code for you using esbuild.

Related

offline: Failure: package.json does not exist at /home/denzilgupta/serverless-testing/.webpack/service/services/trader/package.json

I am getting the following error while running serverless offline start
Can someone help me with this?
offline: POST /dev/trader/create (λ: CreateTrader)
offline: Failure: package.json does not exist at /home/denzilgupta/serverless-testing/.webpack/service/services/trader/package.json
Error: package.json does not exist at /home/denzilgupta/serverless-testing/.webpack/service/services/trader/package.json
at Object.../../node-pre-gyp/lib/pre-binding.js.exports.find (/home/denzilgupta/serverless-testing/.webpack/service/services/trader/webpack:/home/denzilgupta/serverless-testing/node_modules/node-pre-gyp/lib/pre-binding.js:18:1)
at Object.../../bcrypt/bcrypt.js (/home/denzilgupta/serverless-testing/.webpack/service/services/trader/webpack:/home/denzilgupta/serverless-testing/node_modules/bcrypt/bcrypt.js:5:1)
at webpack_require (/home/denzilgupta/serverless-testing/.webpack/service/services/trader/webpack:/webpack/bootstrap:19:1)
at Module.../../../services/trader/handler.js (/home/denzilgupta/serverless-testing/.webpack/service/services/trader/handler.js:164:64)
at webpack_require (/home/denzilgupta/serverless-testing/.webpack/service/services/trader/webpack:/webpack/bootstrap:19:1)
at /home/denzilgupta/serverless-testing/.webpack/service/services/trader/webpack:/webpack/bootstrap:83:1
at Object. (/home/denzilgupta/serverless-testing/.webpack/service/services/trader/handler.js:87:10)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /home/denzilgupta/serverless-testing/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:67:133
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at InProcessRunner.run (/home/denzilgupta/serverless-testing/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:67:9)
Looks like bcrypt work as a native module for nodeJS, probably it contains some DLL and/or binaries. To load this kind of module nodeJS needs the full path to the lib, it does not work well with module bundlers like webpack. It would work as an external plugin but it seems that it does not attend your use case as you are using serverless framework and need to bundle everything together.
I suggest changing to bcryptjs, it is 30% slower because of the implementation in JS instead of using C++ but it works with webpack.

Cannot find MySQL in NodeJS using AWS Lambda

I have the following code that I would like to execute. I have tried requiring mysql and node-mysql and they both give me the same error:
Code:
var AWS = require("aws-sdk");
var mysql = require("mysql");
exports.handler = (event, context, callback) => {
try {
console.log("GOOD");
}
catch (error) {
context.fail(`Exception: ${error}`)
}
};
Error:
{
"errorMessage": "Cannot find module 'mysql'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:417:25)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)",
"Object.<anonymous> (/var/task/index.js:2:13)",
"Module._compile (module.js:570:32)",
"Object.Module._extensions..js (module.js:579:10)",
"Module.load (module.js:487:32)",
"tryModuleLoad (module.js:446:12)",
"Function.Module._load (module.js:438:3)"
]
}
How do I import mysql into node using lambda or get this to work?
Ohk so this is expected to happen.
The problem is that AWS Lambda runs on a different machine and there is no way you can configure that particular machine to run in a custom environment. You can however package the Node Module of mysql or node-mysql in a zip and upload to AWS Lambda. Steps are,
npm install mysql --save
Zip your folder and INCLUDING your node package
Upload this zip file as your code in AWS Lambda.
You can also take a better approach by using Serverless Framework. More info here. In this approach, you write a YAML file which contains all the details and configuration you want to deploy your lambda with. Under your lambda configuration, specify path to your node module (say, nodemodule/**) under package -> include section. This will package your required alongwith your code. Later using command line you can deploy this lambda. It uses AWS Cloudformation service and is one of most prefered way of deploying resources.
More information on packaging using Serverless Framework can be found here.
Note: To use serverless framework there couple of steps like getting API keys for your user, setting right permissions in IAM etc. These are just initial setup and won't be need later. Do perform those prior to deploying using serverless framework.
Hope this helps!
In case any body needs an alternative,
You can use the cloud9 IDE which is free to open the lambda function and execute the npm init using the terminal window against the lambda function folder this will provide the node package file, which then can be used to install dependencies.
if using package.json, simply add below and run "npm install"
{
"dependencies": {
"mysql": "2.12.0"
}
}
I experienced this when using knex, although I had mysql in my package.json.
I had to require('mysql') in my lambda (or a file it references) so that Serverless packages it during deployment.

angular2 Uncaught TypeError: Cannot read property 'isDefaultChangeDetectionStrategy' in Chrome

My app local works in local and used to work in production. However, I get this error on console in production.
main.fd49057….bundle.js:57 Uncaught TypeError: Cannot read property 'isDefaultChangeDetectionStrategy' of undefined
at Object.<anonymous> (main.fd49057….bundle.js:57)
at r (inline.js:1)
at Object.<anonymous> (main.fd49057….bundle.js:43)
at r (inline.js:1)
at Object.<anonymous> (main.fd49057….bundle.js:1721)
at r (inline.js:1)
at Object.<anonymous> (main.fd49057….bundle.js:1658)
at r (inline.js:1)
at Object.<anonymous> (main.fd49057….bundle.js:1693)
at r (inline.js:1)
This is how I deploy to production on s3
ng build -prod
aws --profile ttt s3 sync dist/ s3://angular.bhead.com --acl public-read
I then deployed on firebase and it works...
Most disturbing is that it works in FF and Safari in production. I get this error in Chrome only with deployment in either s3 or Firebase.
Check your version of angular and make sure it matches all the other related libraries. I had the same issue when I added Angular 4's core library. I doesn't hold the angular core object in the same property as the previous versions. I hope that helps.

jHipster Generator for AWS not working

We have generated jHipster project using "yo jHipster" which runs fine locally.
Now, we want to deploy the application to AWS using Elastic Beanstalk.
I have tried deploying the WAR file generated for the jHipster project using the Elastic Beanstalk console, however, it fails to launch the application.
Then I came across a sub project of jHipster to generate the application for AWS directly. e.g. yo jHipster:aws
But I am getting the following error while running it. Any pointers to investigate this issue further please?
yo jhipster:aws
events.js:141
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'toLowerCase' of undefined
at module.exports.AwsGenerator.extend.initializing.checkDatabase (/usr/local/lib/node_modules/generator-jhipster/aws/index.js:23:37)
at Object. (/usr/local/lib/node_modules/generator-jhipster/node_modules/yeoman-generator/lib/base.js:436:25)
at /usr/local/lib/node_modules/generator-jhipster/node_modules/yeoman-generator/node_modules/run-async/index.js:24:25
at /usr/local/lib/node_modules/generator-jhipster/node_modules/yeoman-generator/lib/base.js:446:8
at processImmediate [as _immediateCallback] (timers.js:383:17)

Getting an error while trying to deploy with Modolus

I am following this tutorial (using meteor on modulus) to test telescope app and how the deploy works
I am getting this error :
Unable to connect to any application instances.
And this is the log:
/mnt/data/1/node_modules/fibers/future.js:245
throw(ex);
^ TypeError: Cannot read property 'name' of undefined
at app/server/migrations.js:469:43
at [object Object]._.extend.forEach (packages/mongo/mongo_driver.js:965:1)
at [object Object].Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:812:1)
at Object.migrationsList.updateUserNames (app/server/migrations.js:462:14)
at runMigration (app/server/migrations.js:45:52)
at app/server/migrations.js:10:5
at Array.forEach (native)
at Function..each..forEach (packages/underscore/underscore.js:105:1)
at Meteor.methods.removeMigration.Migrations.remove.name (app/server/migrations.js:9:5)
at /mnt/data/1/programs/server/boot.js:229:5 [2015-04-09T22:31:49.395Z] Failed to start application.
Thank you !!
This is a common issue when you don't redeploy the application after the first step.
run this 2 commands.
modulus project restart
modulus deploy
NOTE be sure the URL don't have any blank space either.
From step three of the tutorial,
The Meteor app is not quite ready yet and the logs will show errors. We have to deploy first in order to receive a URL for Meteor's required environment variables.
Have you goen through and correctly defined all of the environment variables?