Unable to import module 'lambda_function': No module named 'orjson.orjson'", - aws-lambda

I'm trying to run a Python Lambda function (which ran successfully in 3.6, and I'm now trying to upgrade to a supported version). I'm getting this result:
Response
{
"errorMessage": "Unable to import module 'lambda_function': No module named 'orjson.orjson'",
"errorType": "Runtime.ImportModuleError",
"stackTrace": []
}
The orjson library is presumably a dependency of one of my dependencies, as I'm not importing it directly. I have tried to include all necessary dependencies as a layer, and I have experimented with a variety of ways to structure the zip file with the dependencies installed, but this issue persists.
Is there anything I might be overlooking when creating the zip file for the layer?

I have been struggling with the same issue, and found out today that it is because orjson is built on my Mac while AWS Lambda uses Amazon Linux, making the orjson's cpython artifact incompatible when deployed.
Try using --use-container option when running sam build. This builds the artifact on an Amazon Linux container, and has resolved the issue for me.
Hope this helps.

Related

AWS Lambda for Secrets Manager Password Rotation giving Errors for RDS(postgres)

I have downloaded the python function from https://github.com/aws-samples/aws-secrets-manager-rotation-lambdas/blob/master/SecretsManagerRDSPostgreSQLRotationSingleUser/lambda_function.py. Also, I am using Terraform to deploy ( Python Runtime is 3.9 as suggested). However, After deployment I am getting the following errors in CloudWatch Logs
[ERROR] Runtime.ImportModuleError: Unable to import module 'function': No module named 'pg' Traceback (most recent call last):
I also tried to create a Lambda layer but it did not help.
Appreciate any help !!
That Lambda function is apparently dependent on the PyGreSQL library. I don't see anything in the documentation for that function about that, but that's obviously what it is using. I'm surprised those sample functions don't include a requirments.txt file for their dependencies. There are some open issues in that GitHub repository related to this.
You would have to package that function's code along with the PyGreSQL library and any other dependencies into a .zip file, and modify your Terraform code to deploy the .zip file to Lambda.

How to package dependencies using #aws-cdk/pipelines module construct? Getting error TS2307

I've been experimenting with using the new AWS CDK pipelines construct (https://docs.aws.amazon.com/cdk/api/latest/docs/pipelines-readme.html), and have successfully setup a couple of projects, including following the blog post announcement here: https://aws.amazon.com/blogs/developer/cdk-pipelines-continuous-delivery-for-aws-cdk-applications/.
However, as soon as I try to add a dependency to my lambdas the build fails with a
lib/lambda/handler.ts(2,24): error TS2307: Cannot find module 'stripe' or its corresponding type declarations.
I have installed a package.json file and the node_modules in the directory with the lambdas, tried zipping the lambdas and node_modules, tried uploading the zip file with the console, and tried getting the 'buildCommand' during the 'synthAction' step to install the dependencies. Nothing works.
The asset appears to get created in the cdk.out directory, and code changes are being uploaded, but node_modules themselves never get packaged along with the lambda functions.
I am using the 'SimpleSynthAction.standardNpmSynth' action, along with an 'npm run build' command in the 'buildCommand' step.
I've looked at Lambda can't find modules from outer folders when deployed with CDK, How to install dependencies of lambda functions upon cdk build with AWS CDK and https://github.com/aws-samples/aws-cdk-examples/issues/110#issuecomment-550506116 without luck.
What am I missing?
Looks like the issue has to do with bundling. I abandoned the 'all-in on CDK approach' and went back to using a SAM/CDK hybrid, but this new blog post suggests bundling is the answer.
https://aws.amazon.com/blogs/devops/building-apps-with-aws-cdk/
Specifically, it references this construct, which likely would have helped in my situation: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html
Something to try in future.

Error due to "BuildRequires: maven-local" in spec file for Packaging Maven project

Issue : I am following the URL. https://docs.fedoraproject.org/en-US/java-packaging-howto/packaging_maven_project/ to create a spec file for the rpm package.
In the website "BuildRequires: maven-local" is mentioned in the spec file.
but when I tried to search 'maven-local' using yum search maven-local command, I could not find anything and as I was not able to install the component, I'm getting an error when I am trying to do rpmbuild.
Note: I even installed maven separately in my system but it did not help me to fix the issue.

Webpack compilation, local Module not found: Error: Can't resolve

I modified a plugin and added it in my project's file system. So in my package.json I have : "some-module": "file:modules/some-module".
It works fine on my machine (Windows), but not on our build server (Linux CentOS), which fails during webpack compilation with error : Module not found: Error: Can't resolve 'some-module'.
The build server does clean the workspace before build, and after the failed build the plugin does exist in 'node_modules' folder of the workspace.
What could cause this error if the plugin does exists ?
EDIT : I tried on another linux machine (CentOS) and build failed for same error.
EDIT2 : I changed the local file: reference in my package.json file to a .tar.gz git URL. The error is now :
ERROR in ../node_modules/nativescript-pdfview-ng/pdfview-ng.ts
Module build failed (from ../node_modules/#ngtools/webpack/src/index.js):
Error: /root/myProject/node_modules/nativescript-pdfview-ng/pdfview-ng.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format ([link][1]).
at AngularCompilerPlugin.getCompiledFile (/root/myProject/node_modules/#ngtools/webpack/src/angular_compiler_plugin.js:719:23)
at plugin.done.then (/root/myProject/node_modules/#ngtools/webpack/src/loader.js:41:31)
at process._tickCallback (internal/process/next_tick.js:68:7)
Are you using NativeScript 5.x?
If so, make sure to prefix the plugin with "nativescript-", since {N} 5.x the module resolution seems to have changed with regards to Webpack'ed build.

node_module errors with AWS lambda, what's the best practice for dependencies?

I've been trying to convert and deploy one of our node.js apps into a lambda function and have been having some problems with the node_modules dependencies - saying that it can't find certain modules. I started by creating a package.json, npm install the dependencies locally then copy the node modules folder up to lambda.
For instance I have a project that requires sequelize and convict and have been getting errors saying that it cannot find the moment module as a sub-dependency. I see that moment is included in the root of my node_modules but it was not included in the sub folder under the sequelize module.
However, this project runs fine locally. What is the difference in lambda and what's the best practice for deploying a somewhat long list of node modules with it - just a copy of the node_modules folder? On some of the other simpler projects I have, the small amount of node_modules can be copied up with no problems.
{
"errorMessage": "Cannot find module 'moment'",
"errorType": "Error",
"stackTrace": [
"Function.Module._resolveFilename (module.js:338:15)",
"Function.Module._load (module.js:280:25)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)",
"VERSION (/var/task/node_modules/sequelize/node_modules/moment-timezone/moment-timezone.js:14:28)",
"Object. (/var/task/node_modules/sequelize/node_modules/moment-timezone/moment-timezone.js:18:2)",
"Module._compile (module.js:456:26)",
"Object.Module._extensions..js (module.js:474:10)",
"Module.load (module.js:356:32)",
"Function.Module._load (module.js:312:12)"
]
}
I resolved this by uploading all from a zip file which contains all the data I need for my lambda function.
you can just create your project in your local machine and make all the changes that you need then the file you are going to zip should have this same structure and also see that there is an option to load your code from a zip file.
This sounds to me like an issue caused by different versions of npm. Are you running the same version of nodejs locally as is used by Lambda (ie. v0.10.36)?
Depending on the version of npm you're using to install the modules locally, the node_modules directory's contents are laid out slightly differently (mainly in order to de-duplicate things), and that may be why your dependencies can't find their dependencies in Lambda.
After a bit of digging, it sounds like a clean install (ie. rm your node_modules directory and run npm install) might clean things up for you. The reason why is that it seems that npm doesn't install sub-dependencies if they're already present at the top level (ie. you installed moment before sequelize, etc).

Resources