In this article, we will see how can we integrate AWS Lambda, Serverless Framework, and Node.js seamlessly and without any hassles step-by-step.
Being an event-driven, highly scalable JavaScript runtime the Node.js is, it is widely used to develop scalable network applications. Node.js is built upon Google Chrome’s V8 Javascript Engine and thus, Node.js is highly popular when it comes to the scalable applications.
Here‘s how you can install Node.js into your Linux System:
$ sudo apt update $ sudo apt install nodejs
You can check the installed version of Node.js by the following command:
$ node -v v10.0.0
$ sudo apt-get install curl $ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install nodejs
$ node -v v10.0.0
Now that you have successfully installed the Node.js and npm in your system let’s see how you can create a simple Node.js Server:
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => console.log(`Server running at http://${hostname}:${port}/`); });
Now run your Node.js program with the following command:
$ node app.js Server running at http://127.0.0.1:3000/
Now all you need to do is open the URL 127.0.0.1:3000 in a browser and it will print ‘Hello World’.
Let’s now understand what Serverless Technology actually is. A structural design pattern where applications are hosted by a Third-party service which eliminates the need of a separate Software and/or Hardware servers and Hardware Management. It refers to applications that significantly on Third-party service, or on custom code.
On Serverless Mark Robert quoted this:
“Serverless architectures are application designs that incorporate third-party “Backend as a Service” (BaaS) services, and/or that include custom code run in managed, ephemeral containers on a “Functions as a Service” (FaaS) platform. By using these ideas, and related ones like single-page applications, such architectures remove much of the need for traditional always-on server components.”
Now you might ask that “What is AWS Lambda then ?” AWS Lambda is a service provided by Amazon that lets you run your code without the need or provision of any servers on-the-go. AWS Lambda executes your code only when been called by other function(s) and provides great scalability from few requests per day to thousands of requests per second. The main advantage of this is that you only pay for the compute time you consume and there are no charges when it been ideal.
As we have covered all the basics about Serverless and AWS Lambda, let’s now setup Serverless in our system:
$ npm install -g serverless $ npm init -y $ npm install --save express serverless-http
Now that everything looks great, let’s get to the main part, the actual coding part:
Once you open the code editor, you’ll see three files where you can ignore .gitignore and focus on handler.js and serverless.yml. The handler.js is containing all your business logic, whereas the serverless.yml is the configuration file for all of the resources you’ll be using on AWS.
From here you can start coding in serverless.yml, you can delete the starter code and paste the below-given code in the file:
service: serverless-nodejs-app provider: name: aws runtime: nodejs10.0 stage: dev region: eu-central-1 functions: app: handler: app.server # reference the file and exported method events: # events trigger lambda functions - http: # this is an API Gateway HTTP event trigger path: / method: ANY cors: true - http: # all routes get proxied to the Express router path: /{proxy+} method: ANY cors: true
And we are done for the day! All that’s left is to Deploy it and you can deploy it by using the below-given commands:
$ sls deploy
Once the resources are created and the code is deployed, you’ll see an endpoint get sent back to you in the terminal.
Now that we have completed the coding part, let’s have a look at how it can be helpful in achieving your goals: