Creating first Serverless AWS Lambda API in Node.js
AWS Lambda functions are event-driven and serverless — triggered to process a piece of code and return a result. In this article, we are going to walk through creating one Node.js API.🤓
So…Why use Serverless?
Lower-Cost:💸
In a serverless architecture, you will pay only for what you use. There is no waste of capacity, no resources are wasted, or money.
(⚠️: If by mistake you through your API in public group R.I.P to your AWS bill)
No need to manage the Server:🥺
Forget the headache of backend infrastructure management. No Server down, no prearrangement or maintaining of servers ever again.
Unlimited Scalability:✨
You can scale the functions according to users. Scaling up or down is as simple as executing a few lines of code you could handle large traffic.
Prerequisites
- AWS account with access to IAM and Lambda.
- Node.js 8 or later.
- NPM
Setting Up Serverless framework
npm install -g serverless
running the above command will install serverless in you’re local.
The next step is setting up you’re serverless project🙃
## Create a new Serverless service/project run command ##
$ serverless
after that do “yes” for the new project and select AWS Node.js as the environment.
Give your project an appropriate name. Do “yes” to the rest of the steps to create your folder.
>> cd {your app name}
after that and you will see a
handler.js
The handle.js file is for your writing your business logic.
Serverless.yml
Your application is deployed using the Serverless framework based on the serverless.yml
configuration file.
The below link is a list of all available properties in serverless.yml
when the provider is set to aws
https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/
now we need three dependency
"express": "^4.17.1", // for node.js routing and api
"pg": "^8.5.1", // for postgress-db querry for node.js
"serverless-http": "^2.6.0" // This module allows you to 'wrap' your API for serverless use
Rename your handler.js file to app.js .
….Your app.js will look like
Import’s + DB query + wrapping “app” in the serverless handler.🤯
.then(()=>client.end()); //at this point we are closing the db conn
the serverless.yml file would not be changed majorly. except for the function part, we will add the HTTP events (Simply put, events are the things that trigger your functions to run.
If you are using AWS as your provider, all events
in the service are anything in AWS that can trigger an AWS Lambda function, like an S3 bucket upload, an SNS topic, and HTTP endpoints created via API Gateway. )
now the final time to deploy the app….🧐
run command
serverless deploy (or sls deploy)
though sls deploy
re-deploys the entire stack through cloud formation and can be noticeably slooowww🐌.
On the flip side, sls deploy function -f myFuncName
only zips up the code (& any dependencies) and updates the Lambda function only. This is much faster than waiting for an entire stack update.
# Redeploy entire stack through cloud formation
sls deploy
4# Redeploy only the code + dependencies to update the AWS lambda function
sls deploy function -f myFuncName
Offline Emulation
Now you might be asking, what about offline emulation? It’s absolutely a way to speed up dev cycles without having to re-deploy anything.
npm install serverless-offline
Then add the plugins
key in serverless.yml
plugins:
- serverless-offline
Now you should have the serverless offline
commands for testing on localhost the function before deploying.