0

I'm writing an AWS Lambda using the NodeJS12.x runtime. It's fairly straightforward. It sends an email through Postmark. I know SES is an alternative, I've opted not to go that route here.

const postmark = require("postmark");
const postmarkClient = new postmark.ServerClient(process.env.POSTMARK_TOKEN);

exports.handler = function (event, context, callback) {
  postmarkClient.sendEmail({
    From: "some@email.address",
    To: "different@email.address",
    Subject: "My emails subject",
    TextBody: "My emails text body"
  });
}

The uploaded zip structure looks something like this:

lambda.zip:
- node_modules/
-- postmark/
-- axios/
-- other-modules/
- index.js
- package.json

When I upload that zip from my Windows machine and test the lambda, it gives me this error in my CloudWatch logs:

Error: Cannot find module './node_modules/postmark'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js

Cool. I did a good old afternoon of googling and came across, among many other things, another Stack Overflow answer for some fix for a similar problem in a linux system. Octopus suggested I ensure all the files are executable and that I own them by running these commands:

sudo chmod +x *.js -R
sudo chown myself.myself * -R
zip -r lambda.zip .

So I spun up my trusty Linux Subsystem for Windows (cause we have that now), cloned my repo, ran the commands, deployed that new zip file my AWS Lambda Function, and tested away. Voila, postmark delivered upon my inbox an email.

That's great. Windows loses to Linux again. I'm on that train. But I still work in Windows and need to develop and deploy my functions in this environment.

My two questions are as follows:

  1. What do I need to change to replicate these Linux actions on the Windows side of my machine?
  2. Why are these actions required?
Drayvock
  • 35
  • 6
  • Compare the zip structure that works to the one that doesn't work. What is different? You don't need to chmod +x the JavaScript source files or chown files before packaging them (at least I've never had to). And who is Viola? – jarmod Apr 24 '20 at 22:13
  • Great question! Ultimately one that would have led me down the right path. Four shots of liquor later, I have discovered my answer: My zipping tool was not zipping the same in both cases (though kdiff3 seems to think both directories are identical once unzipped). In Windows I was using an NPM module called jszip-cli to zip my items, versus on my Linux Subsystem I was using the built in zip tool. I swapped my Windows side to use the PowerShell zipping methodology and everything seemed to be kosher. For posterity, here's a post with the script I used: https://stackoverflow.com/a/26843122/3203689 – Drayvock Apr 24 '20 at 23:59

1 Answers1

0

It turns out the zipping tool I was using on Windows wasn't zipping in the same way a native tool would. I was using a node module called jszip-cli to zip my items. Once swapping to the PowerShell command below (which I found here), the lambda began working just fine:

powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar'); }"
Drayvock
  • 35
  • 6