DEV Community

Cover image for Getting Started with AWS Lambda and Node.js

Getting Started with AWS Lambda and Node.js

Adnan Rahić on May 25, 2018

Once upon a time, not so long ago, a word caught my ear. Lambda. That struck a chord, remembering the good old days of playing Half-Life as a kid. ...
Collapse
 
maxart2501 profile image
Massimo Artizzu

Nice one, Adnan. I'm bookmarking this article.
It would be interesting to know about a couple more things:

  • Reading/writing to disk: is it allowed/slow/limited/expensive?
  • Using a DB like MySQL or Redis or whatever: how to?
  • How reliable are the free tiers? Do they take long to start up?
  • Nice if you happen to know: how does AWS Lambda compare to other lambda services, e.g. Google Functions?
Collapse
 
adnanrahic profile image
Adnan Rahić

Thanks! Glad you're interested in learning more.

  • Read/Write to Disk: There are a few things to wrap your head around regarding serverless architectures in general. There's no persistent storage because it is essentially a container. You have a tiny /tmp dir where you can write to disk, but you seriously do not want to.
  • DB: You have to use a database which is hosted separately. You can, of course, spin up a server and host it yourself, but that's too painful for me so I just use DBaaS solutions such as MongoDB Atlas or AWS RDS. But you can use whatever you like here pretty much. I explained hooking up Atlas to Lambda here.
  • Free tiers: AWS Lambda's free tier is only about invocations and compute time. The performance and start-up speeds are the same nevertheless. Lambda cold starts are usually between 1 and 3 seconds. Which is manageable if you use warm-up scripts and proper tooling. I'd suggest you do use the Serverless Framework and some monitoring solution like Dashbird if you want to create production apps. Take a look at the articles covering all things tagged #serverless on my profile
  • Other FaaS solutions: AWS Lambda is by far the most used and covers most languages. But Google Cloud Functions (Firebase Functions) are great as well. Of course Azure has their Functions too. All of those get the job done the same way. Even the pricing is literally the same. It all boils down to which provider you use and you pick your FaaS solution based on that.

Hope this clears things up. :)

Collapse
 
maxart2501 profile image
Massimo Artizzu

Awesome, thank you! :D

Collapse
 
shmdhussain12 profile image
Mohamed Hussain • Edited

Great Adnan...Clear Explanation on what to do with AWS Lamdba...one question , is that possible to stop the api gateway which we created for roll-a-dice temporarily, if so how could I do it?....

Do we need to include the node_modules folder when we upload the zip to lambda?

Collapse
 
adnanrahic profile image
Adnan Rahić

You can't "stop" the API Gateway entirely, but you can add an API Key to rate-limit the endpoint.

Yes, you need to include the node_modules. I tend to write Lambda code locally and use the Serverless Framework to deploy the code and resources. Check out this article I wrote to learn how.

Collapse
 
ankursingh16 profile image
AnkurSingh16

Thats a very nice article Adnan. I'm looking to find a way to write Lambda code in local IDE so that I can have version control. Any suggestions?

Collapse
 
adnanrahic profile image
Adnan Rahić

I use VSCode and the Serverless Framework to manage my resources and code. Check out this article I wrote to learn how.

Collapse
 
mistermoses profile image
Dan

The upload zip instructions don't make sense. If I run mkdir roll-a-dice && npm init -y then the node_modules directory will be outside my project directory and will not be include in the zip.

Collapse
 
adnanrahic profile image
Adnan Rahić

Oh, that's a typo. Thanks for noticing Dan! I've fixed it now.

Collapse
 
chathula profile image
Chathula Sampath

great explanation!!! :D

Collapse
 
adnanrahic profile image
Adnan Rahić

Glad you liked it!

Collapse
 
rishinharis profile image
Rishin Haris

Hi Adnan,
Can you tell me how to host nuxt js server in lambda?

Collapse
 
adnanrahic profile image
Adnan Rahić

That's actually a great question. I haven't tried it yet, but I'd say it's better to use nuxt generate and host static content on S3. If you get around to trying it, feel free to get in touch with me!

Collapse
 
avalander profile image
Avalander

Awesome article, thanks for writing! I've been trying to learn AWS lambda recently, but I find the official docs rather confusing, this article helped a lot!

Collapse
 
adnanrahic profile image
Adnan Rahić

Glad it helped! I can understand the pain of reading the AWS docs. They are surprisingly bad for such a huge provider.

Collapse
 
buntine profile image
Andrew Buntine

Nice article. It would also be interesting to talk about receiving input to the lambda handler. I see the event argument is passed into the function but it's never used.

Collapse
 
adnanrahic profile image
Adnan Rahić

Thanks! :)
Here's another article I wrote a while back.

It's a bit more detailed, with examples of receiving input. :)

Collapse
 
crisp3333 profile image
Gabi • Edited

I assumed you wanted us to cd into 'roll-a-dice' directory and do an 'npm install moment --save' (for the uploading zip folder part).

Collapse
 
sbasaez profile image
sbasaez

adnan excelente articulo lo lei y me quedo mucho mas claro aws lambda si tubiera mas informacion y la puedes compartir seria de gran utilidad

saludos