Migrate your Node.js application to serverless using AWS Lambda
A step-by-step tutorial where we’ll learn how to convert your Node.js project to AWS Lambda and start saving money!
AWS Lambda is the main serverless AWS computing service. With AWS Lambda, you don’t have to create EC2 instances, you don’t have to provide any server, and you don’t care about scaling; you are just focused on your code.
One of the main reasons for using AWS Lambda is that you’ll only pay for what you use. If you are developing a proof of concept and don’t have many users, this is extremely useful! I used to pay $10 per month per proof of concept, and right now, it’s free for me! Also, the AWS Lambda free tier is amazing; it includes one million free requests and 400,000 GB-seconds of computing time per month! And if you go over it, you will only pay $0.20 per 1M requests (apart from other factors like the duration of the request).
The only limitation? AWS Lambda functions may run up to 15 minutes per execution, so you shouldn’t use this service if you have a really heavy endpoint. This shouldn’t be your case if you are developing a Node.js application like in this case. Let’s learn how to do it with a few commands!
- Install Serverless
- Refactor Express code
- Create Serverless.yml file
- Change behavior and models to allow concurrency
- Deploy the serverless application on AWS Lambda
- Change environment variables in the Frontend
1) Install Serverless
Before we start the process, we need to install three dependencies:
- “Serverless”, the Serverless framework.
- “Serverless-offline”, a plugin that will emulate AWS Lambda and API Gateway on your local machine. This will be really useful to test everything locally before deploying it.
- “Serverless-http”, a module that allows you to ‘wrap’ your API for serverless use.