DEV Community

Cover image for Automating My Newsletter Generation with MailChimp, Google Sheets, and AWS Lambda

Automating My Newsletter Generation with MailChimp, Google Sheets, and AWS Lambda

Kyle Galbraith on July 16, 2018

I am constantly building and launching new projects. One of the many things I evaluate when starting a new project is how I am going to maintain it...
Collapse
 
vfulco profile image
Vincent Fulco (It / It's)

I am fairly new to node.js. If anyone had some quick tips, would appreciate it. I am getting an error loading the handler module. I thought it was a permissions error but I have them as loose as possible. Originally edited on Ubuntu 18.04 before using serverless to push to AWS Lambda. TIA

Unable to import module 'handler': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/var/task/handler.js:2:25)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)

Collapse
 
kylegalbraith profile image
Kyle Galbraith

Vincent,

This error is typically seen when running AWS Lambda and you have defined the handler for the Lambda incorrectly. For example handler.js defines the following Lambda entry point:

module.exports.generateNewsletter = (event, context, callback) => {
  //some code
}

Therefore the handler must be defined for the Lambda function as handler.generateNewsletter as you see in the serverless.yml file.

So I suspect the error you are seeing is that either A) You have handler.js but a different name for the function entry point that is not generateNewsletter or B) Your entry point is not in a file called handler.js.

Collapse
 
vfulco profile image
Vincent Fulco (It / It's)

Will give this review / fix a try in the next few days. Off to buy your book and videos. May I suggest an AWS Lambda for noobs book next if you are taking votes? Thanks for the excellent fast response!

Best, V.

Thread Thread
 
vfulco profile image
Vincent Fulco (It / It's)

I am really stumped. I git cloned the repo exactly as is so I don't see why the function names would be messed up. Everything is in its respective original directory/folder location from the original repo. And I verified permissions were readable+writeable by all. Still can't find handler.js which is right there in the same dir as serverless.yml. Node.js is 8.10 and I updated npm to the latest while trying to eliminate factors. I will re-verify everything from the article.

Thread Thread
 
vfulco profile image
Vincent Fulco (It / It's)

It's got to be something else. I tried a basic 'hello world' exercise from the serverless.com site and get the proper response. Both serverless.yml and handler.js are in the same dir. Similar to this repo when I git clone it. Since I have not changed the name of the function or its placement, I suspect some component of handler.js. Learning more about troubleshooting lambda quickly. To clarify, even though when we download the authentication file from google and a name is assigned, we have to rename it to g-auth.json and have it in the root dir? I have tried it in a few places.

Thread Thread
 
kylegalbraith profile image
Kyle Galbraith

Sounds like you probably didn't run npm install before running serverless deploy. This is required in order to deploy the necessary node_modules that contain the dependencies needed for the Lambda to run. I have updated this blog post to reflect that.

Thread Thread
 
vfulco profile image
Vincent Fulco (It / It's)

Thanks Kyle. I retried with the npm install and still didn't have any luck. After grubbing around google, I decided to give package.json a look and there were some babel dependencies that crept in. I have no idea how since I git cloned your repo but after doing a diff on the files and replacing with the original code, I have eliminated that error.

Now I am getting a undefined type error when it reads the 'text' column from the spreadsheet. Good crash course on node.js troubleshooting. ;-)

Collapse
 
venikunche profile image
Veni Kunche

Thanks for sharing this, Kyle! Ever since you mentioned it on Twitter, I've been trying to figure out how to automate a lot of my manual tasks. I use Airtable to curate my newsletter. I'm learning AWS and serverless to automate creating the MailChimp campaign. This is really helpful!

Collapse
 
kylegalbraith profile image
Kyle Galbraith

Thank you for the kind words Veni! This should definitely be applicable to AirTable as well. AWS Lambda/Serverless is fantastic for automating all those manual tasks that suck time from your day.

Collapse
 
ayunas profile image
Amir Yunas

I'm a beginner coder and AWS'er. I run awsbeginner.com. This post was way above my head unfortunately. Maybe after a bunch more practice with coding and learning Lambda, I'll be able to follow along easier.