A while back I had some issues with AWS Lambda and specific
npm-packages. Since I’m new to exploring the architecture, I thought I’d put some time and write the solution down for future reference.
The issue: While compiling and installing npm-libraries for AWS Lambda specific Node.js version, the compiled architecture does not match.
While my development environment contains of a variety of operating systems, Amazon runs a specific distribution of Linux – one which I couldn’t find anywhere. So to be sure my binary could run on this specific architecture; why not use another resource built by Amazon: EC2.
Fire up a new EC2 instance
Your EC2 instance and Lambda must be running on the same platform.
Currently Lambda runs on Amazon Linux distribution
amzn-ami-hvm-2015.03.0.x86_64-gp2 with kernel
This is subject of change, so be sure to check before deploying a new EC2 instance which needs to be running the same to be sure your compiling works.
This AWS documentation page is your best friend: Lambda Execution Environment and Available Libraries.
Connect to your new instance
Your console is your best friend – use
ssh. Everything you need to know is in your AWS Console. Keep track of your
.pem-file, you are going to need it later.
The Node.js version also needs to match running version on Lambda. Current version is
v0.10.36 but this is subject of change.
Your best friends:
- How to yum install Node.JS on Amazon Linux
- Lambda Execution Environment and Available Libraries (for current versions).
Note: If you’re planning on using your new EC2 instance to more things than compiling binaries for Lambda and it’s locked Node.js version, I strongly recommend nvm for installing multiple versions of Node.js.
npm install your module(s)
Now it’s time to install your
npm-package on your new instance.
npm install package-name for each module you want to install.
npm install node-libcurl
Some packages even supports the
--build-from-source flag, which discards binaries prebuilt.
Fetch your compatible module(s)
We now need to fetch our module from our EC2 instance. We are going to use
scp and we need our
.pem file again.
scp -i -r path/to/publickey.pem user@ecinstance:/path/to/remote/module /path-to-local
We are using flag
-i (identity_file) to point out our identity file (our
.pem-file) and flag
-r to recursively copy directories.
Stack Overflow help:
Upload our new module to Lambda and try it out!
.zip-alternative to upload directly to AWS Lambda or you can upload to S3.
If your are new to this step using archives instead of inline code; take a look at this page: Creating a Deployment Package (Node.js). Simple put, do not archive the root folder, but it’s first level of descendants (step 4 in AWS documentation).
Hopefully; great success!