DEV Community

Cover image for How to deploy a Node.js application to AWS Lambda using Serverless

How to deploy a Node.js application to AWS Lambda using Serverless

Adnan Rahić on June 07, 2018

Being a developer is amazing. Writing code, solving problems and thinking of ingenious solutions for complicated algorithms is what we live for. Bu...
Collapse
 
harrylincoln profile image
Harry Lincoln

Hey Adnan, just something to add to your banging tutorial...

There's a bit of a CORS issue with my setup on the express side, regardless of what you set in the API gateway.

The final step was to make sure that Express could handle it with a cheeky include of this:
npmjs.com/package/cors

Collapse
 
vittoriogassman profile image
Víctor Liendo

Hello Adnan. Reading this at the beginning: "However, it could be as large as any Node.js application you have in production, and it would all work like a charm".

This could be exactly my case. I have been developing a backend using NodeJs, TypeORM, TypeScript, PostgreSQL and Typeorm. Could an entire backend project work as a LAMBDA function ? I have coded about 10%, desployed it as a single LAMBDA function and for now it works ok, but I'm afraid about how the node_modules is growing, because typeORM, sharp and aws-sd are big in size ...

Collapse
 
adnanrahic profile image
Adnan Rahić

Hey Victor! AWS Lambda does has its limits. Read more here. Long story short your deployment package, the .zip file needs to be under 50mb when you're uploading it directly. You can bypass this by uploading your .zip from S3 to Lambda. In that case the limit is 250mb. Read more here.

But, my advice for you here would be to create Lambda functions as microservices. Separate the logic within the application so you can distribute everything across multiple functions. Only use dependencies in places where they are needed, not across all functions. And, keep in mind AWS SDK is included in Lambda, you don't need to explicitly install it in the node_modules.

Check this tutorial out. It can help you understand how to structure your functions.

Good luck, and happy coding. :)

Collapse
 
vittoriogassman profile image
Víctor Liendo

Thanks buddy. Your suggestions have been very useful. First thing i did was removing aws-sdk from node-modules and every thing has gone ok. Second i have just started breaking my backend into smaller parts proxied through the same API Gateway in AWS ... Everything alright so far. Thank you very much

Collapse
 
piyushgargdev profile image
Piyush Garg

Hey there, Nice Post. I made an npm package for handling AWS Lambda responses. Do checkout 😎

AWS-TS This package lets you handle and send responses from AWS lambda with ease. You have the ability to send various types of responses such as JSON or Plain Text without worrying about headers and status codes. You can also enable or disable cors for all or specific responses or set custom headers.

🚀 npmjs.com/package/aws-ts

Collapse
 
prakashr87 profile image
Prakash

Article was very nice, Helped me to start AWS Lambda.

    Actually iam new to Aws Lambda. I have Node.js REST API project(which running on EC2 now) with 100+ api's and 50+ routes and model files. How can i deploy my project into AWS Lambda?.  

Thanks.

Collapse
 
striderhnd profile image
Erick Gonzales

Nice article Adnan, I'm developing a service with Lambda but I'm having a little issue for some reason, my API Gateway is timing out, not sure if is the mongo connection or other issue related to Lambda

Collapse
 
jmtyra profile image
Jmtyra

Thanks for making this article! I love the stuff for beginners like myself. :) Not sure if anyone else ran into this issue but the 'sls' command wouldn't work and I had to use 'serverless' instead. Might be because I'm on a Windows box? Not sure. Either way this was a lot of fun, thanks again! :)

Collapse
 
eduardomarcolino profile image
Eduardo Marcolino

Great article. Thank you very much for posting it!
I'm definitely going to try this thing out.

Collapse
 
anirudhmaddy profile image
Anirudh

How do I test the application in the local environment before deploying it to AWS?