When I run the function locally on NodeJS 11.7.0
it works, when I run it in AWS Lambda NodeJS 8.10
it works, but I've recently tried to run it in AWS Lambda NodeJS 10.x
and get this response and this error in Cloud Watch.
Any thoughts on how to correct this?
Response
{
"success": false,
"error": "Error: Could not find openssl on your system on this path: openssl"
}
Cloudwatch Error
ERROR (node:8) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Function
...
const util = require('util');
const pem = require('pem');
...
return new Promise((fulfill) => {
require('./certs').get(req, res, () => {
return fulfill();
});
}).then(() => {
const createCSR = util.promisify(pem.createCSR);
//This seems to be where the issue is coming from
return createCSR({
keyBitsize: 1024,
hash: HASH,
commonName: id.toString(),
country: 'US',
state: 'Maryland',
organization: 'ABC', //Obfuscated
organizationUnit: 'XYZ', //Obfuscated
});
}).then(({ csr, clientKey }) => {
...
}).then(async ({ certificate, clientKey }) => {
...
}, (err) => {
return res.status(404).json({
success: false,
error: err,
});
});
...
I've tried with
"pem": "^1.14.3",
and "pem": "^1.14.2",
OpenSSL binaries for AWS LambdaA layer for AWS Lambda that allows your functions to use openssl binaries.
You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and . zip file archives. To create the deployment package for a .
You can now develop AWS Lambda functions using the Node. js 16 runtime. This version is in active LTS status and considered ready for general use.
npm module has to be bundeled inside your nodejs package and upload to AWS Lambda Layers as zip, then you would need to refer to your module/js as below and use available methods from it. const mymodule = require ('/opt/nodejs/MyLogger');
Using npm packages and custom modules/packages with Lambda is easy. We’ll start by including prebuilt modules then move on to native ones. Step1: Create a new directory to hold your Lambda function and its modules. For this example, we’ll keep things simple and install the AWS SDK.
The Cannot find module error usually occurs for one of three reasons: The Lambda function's deployment package doesn't have the correct folder structure to allow the Lambda service to load the required modules and libraries.
if your dependency is not listed in that file, it will never get installed with the npm install command. you will need to manually install the packages and then run npm shrinkwrap to update the shrinkwrap file. I had an issue where manually installing a package had created a package-lock.json file after updating to node 8.0.0 and npm 5.0.0.
I tried the answer documented by @Kris White, but I was not able to get it to work. Each execution resulted in the error Could not find openssl on your system on this path: /opt/openssl
. I tried several different paths and approaches, but none worked well. It's entirely possible that I simply didn't copy the OpenSSL executable correctly.
Since I needed a working solution, I used the answer provided by @Wilfred Dittmer. I modified it slightly since I wasn't using Docker. I launched an Amazon Linux 2 server, built OpenSSL on it, transferred the package to my local machine, and deployed it via Serverless.
Create a file named create-openssl-zip.sh
with the following contents. The script will create the Lambda Layer OpenSSL package.
#!/bin/bash -x
# This file should be copied to and run inside the /tmp folder
yum update -y
yum install autoconf bison gcc gcc-c++ libcurl-devel libxml2-devel -y
curl -sL http://www.openssl.org/source/openssl-1.1.1d.tar.gz | tar -xvz
cd openssl-1.1.1d
./config --prefix=/tmp/nodejs/openssl --openssldir=/tmp/nodejs/openssl && make && make install
cd /tmp
rm -rf nodejs/openssl/share nodejs/openssl/include
zip -r lambda-layer-openssl.zip nodejs
rm -rf nodejs openssl-1.1.1d
Then, follow these steps:
curl -F "[email protected]" https://file.io
curl
is not installed on your dev machine, you can also upload the script manually using the File.io website.https://file.io/a1B2c3
tmp
directory by running cd /tmp
.curl {FILE_IO_URL} --output create-openssl-zip.sh
.
FILE_IO_URL
with the URL returned from File.io and copied in step 3.sudo bash ./create-openssl-zip.sh
. The script may take a while to complete. You may need to confirm one or more package install prompts.curl -F "[email protected]" https://file.io
.curl {FILE_IO_URL} --output lambda-layer-openssl.zip
.
FILE_IO_URL
with the URL returned from File.io and copied in step 13.curl
is not installed on your dev machine, you can also download the file manually by pasting the copied URL in the address bar of your favorite browser.build-lambda-layer-openssl
EC2 instance since it is not needed any longer.For completeness, here is a portion of my serverless.yml
file:
functions:
functionName:
# ...
layers:
- { Ref: OpensslLambdaLayer }
layers:
openssl:
name: ${self:provider.stage}-openssl
description: Contains openssl command line utility for lambdas that need it
package:
artifact: 'path\to\lambda-layer-openssl.zip'
compatibleRuntimes:
- nodejs10.x
- nodejs12.x
retain: false
...and here is how I configured PEM in the code file:
import * as pem from 'pem';
process.env.LD_LIBRARY_PATH = '/opt/nodejs/openssl/lib';
pem.config({
pathOpenSSL: '/opt/nodejs/openssl/bin/openssl',
});
// other code...
I contacted AWS Support about this and it turns out that the openssl library is still on the Node10x image, just not the command line utility. However, it's pretty easy to just grab it off a standard AMI and use it as a Lambda layer.
Steps:
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + ':/opt';
The layer will have been unzipped for you and because you set it to be executable beforehand, it should just work. The underlying openssl libraries are there, so just copying the cli works just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With