Getting Started With Serverless Using Node.js

Tudip

Tudip

13 May 2019

What is Serverless computing?

A term Serverless refers to the cloud computing model in which cloud service providers manages computing resources dynamically. The pricing for the utilized resources is based on the amount of resources consumed by an application rather than the allocated/pre-purchased resources.

We can also say that serverless computing can simplify the process of deploying code into production. Maintenance, scaling, and capacity planning operations may be hidden from the developer. A serverless code can be used in conjunction with code deployed in traditional styles, such as microservices. Alternatively, there are no provisioned servers at all and applications can be written to be purely serverless.

Today we will experience the magnificence and other purposes of utilizing Serverless. We will define the main key topics that are crucial in getting started with the technology. Lastly, we will go ahead and jump right into some code and write our own Serverless functions, emulate the environment locally and monitor performance! Please note, We need an AWS account to follow along with the code examples.

Why Serverless?

Why going Serverless is so cool unexpectedly? Is it great not to utilize servers any longer? I cherish servers, but why not utilize them? Servers are magnificent. You utilize the direction line (we can use commands) to guide them. For what reason would anyone need to surrender that? I was truly confounded. Be that as it may, hello, making a stride back, I understand they’re not ideal. They’re an agony to oversee in groups. They don’t scale effortlessly. These are just the primary things that ring a bell.

We should switch our mindset completely. Consider just utilizing functions. No all the more overseeing servers. You just consider the code. Sounds rather cool. We as developers shouldn’t have to do the tedious work on the command line. Let the operations folks (ops guys) handle that. What do we by any chance call this kind of engineering/architecture? Just utilizing functions? Tiny services? Modest administrations?

Functions as a Service (FaaS)

It is called functions as a service, and it’s amazing. The idea depends on Serverless computing. It enables us to deploy any individual piece of code or function. The code runs and returns a value, thus finishing the procedure. Sounds straightforward, isn’t it? All things considered, it is. In the event that you’ve at any point composed a REST Programming interface, you’ll feel comfortable. Each one of the services and endpoints you would typically keep in one place is now sliced up into a bunch of tiny snippets, microservices. The objective is to totally extract away servers from the engineer and only bill based on the amount of times the functions have been invoked. Which means services, such as these are easy to scale.

Be that as it may, all isn’t so bright on this side of the fence. FaaS has been experiencing some getting teeth issues. How would you think errors are handled or taken care of? Not having a physical server to monitor is somewhat of an influencing knowledge. Having insight into your framework is sensibly hard. Especially on a larger scale.

What lies behind Serverless Implementation?

To get a comprehension of how to compose Serverless applications we first need to address the subject of what lies behind everything. The tools and services available to us that makes everything conceivable.

AWS Lambda

AWS Lambda is a service that lets you run code without taking care of or managing servers.

As per AWS documentation, Lambda is an event-based system for running code in the cloud. You don’t need to worry about servers, only the code you write. It scales automatically and only creates a charge for the time it actually is running the code, the compute time. But, most importantly, it scales automatically! How awesome is that? Without more worrying about if the EC2 instance you spun up is large enough to serve all your users.

AWS Lambda gives you a chance to run the code without managing servers. You pay just for the compute time you consume, please note there is no charge when your code isn’t running. With Lambda, you can run code for all intents and purposes any kind of utilization or backend service all with zero administration. We just need to upload the code and Lambda deals with everything required to run and scale your code with high accessibility. You can set up your code to naturally trigger from different AWS services or call it straightforwardly from any web or Mobile application.

When Should I Use AWS Lambda?

AWS Lambda is an ideal platform for some application situations. It provides us to compose our application code in languages supported by AWS Lambda, and keep running within the AWS Lambda standard runtime environment and assets supplied by Lambda.

When using AWS Lambda, you are responsible for your code. AWS Lambda deals with the register armada that offers a parity of memory, CPU, organize, and different assets. This is in return for adaptability, which implies you can’t sign in to compute instances or modify the working framework (like Operating System) or language runtime. On the other hand, AWS Lambda performs operational and authoritative exercises on your behalf, including monitoring fleet health, applying security patches, sending your code, provisioning capacity, and observing and logging your Lambda capacities.

If you need to manage your own compute resources, Amazon Web Services also offers other computer services to meet your needs.

Amazon Elastic Compute Cloud (Amazon EC2) service gives you the alternative to modify operating systems, network and security settings, and the whole programming stack, however, you are in charge of provisioning capacity, monitoring fleet health and performance, and utilizing Availability Zones for adaptation to non-critical failure.

Flexible Beanstalk offers a simple solution to utilize service for conveying and scaling applications onto Amazon EC2 in which you hold proprietorship and full power over the underlying EC2 instances.

How it works

AWS API Gateway

Lambda would be deficient without the API Gateway. All lambda functions require an event to be activated to invoke them. Gateway gives the REST endpoints which trigger the functions. Assume you have the normal Express application. You would more often than not make an app.get() method for a specific route, this way:

app.get('/', function(req, res, next) { /* your code here... */ });

At the point when a client hits the ‘/’ route an event will trigger the callback work. Whether a Gateway is the route and Lambda is the callback function.

The Serverless Framework

Managing all of this is a pain. To manage all the stuff we have toolkit called Serverless is your toolkit for deploying and operating serverless architecture. Focus on your application, not your infrastructure.

The Serverless framework packages every one of the apparatuses you need into a manageable package, making it basic and straightforward to make and deploy serverless applications. It’s so great, it abstracts away all the tedious tasks you need to do in the AWS Console, for example, making functions and associating them to events. The main drawback is that you need to push code to AWS each time you wish to test your functions while imitating the environment locally is a touch of torment.
The utilization of cases when Serverless is the better decision is immense. In view of the simple scaling and low support, any application you have underway where your client throughput shifts quickly is a legitimate contender to utilize serverless design. In conclusion, in the event that you are not good at the Linux shell, and if DevOps isn’t your thing, you have each motivation to attempt Serverless.

A new mindset

Serverless architecture is unforgiving. That’s true. Just setting it up takes a decent amount of mental power. I’m not counting emulating it locally. That is an entire other mammoth inside and out.

The threatening vibe expects us to change our methodology. We need to live with the way that we don’t have a review of our whole system. Be that as it may, people adjust and survive. In comes the Serverless framework like a knight in a sparkling defensive layer.

Let’s create a basic Serverless function.

Setting up Serverless is basic. You have to introduce it through npm and attach your AWS account. Try not to stress, on the off chance that you get threatened by the AWS Console, it’s impeccably fine. I’ll separate the procedure and we’ll go through step by step.

Step 1. First of all, you need to install Serverless globally

Fire up a terminal window and run:

$ npm install -g serverless

You’ve presently introduced the Serverless framework globally on your machine. The Serverless commands are currently accessible to you from wherever in the terminal.
Note: If you’re utilizing Linux, you may need to run the direction as sudo.

Step 2. Create an IAM User in your AWS Console

Open up your AWS Console and press the administrations dropdown in the upper left corner. You’ll see a ton of services appear. Go ahead and write IAM in the search box and press on it.

You’ll be diverted to the fundamental IAM page for your account. Continue to include another user.

Pick an astounding name for your new IAM user and give the user programmatic access. Continue to the subsequent stage.

Now you can add required permissions to the user. Because we are going to let Serverless create a delete various assets on our AWS account go ahead and check the AdministratorAccess.

Continuing to the next stage you will see the user was created. Now, and just now will you approach the users Access Key ID and Secret Access Key. Make a point to record them or download the .csv document. Protect them, absolutely never show them to anyone. I’ve pixelated them despite the fact that this is a demo, to ensure you comprehend the seriousness of guarding them.

With that done we can, at last, proceed to entering the keys into the Serverless configurations.

Step 3. Enter IAM keys in the Serverless configuration

Great! With the keys saved, you can set up Serverless to get access to your AWS account. Turn back to your terminal and type all of this in one line:

$ serverless config credentials --provider aws --key xxxxxxxxxxxxxx --secret xxxxxxxxxxxxxx

Hit enter! Now your Serverless establishment comprehends what account to associate with when you run any terminal command. How about we hop in and see it in real life.

Step 4. Create your first service

Make another directory to house your Serverless application services. Flame up a terminal in there. Now you’re prepared to make another service. What’s a service you inquire? View it as a task. In any case, not by any means. It’s the place you characterize AWS Lambda Functions, the events that trigger them and any AWS framework assets they require, all in a document called serverless.yml.

Back in your terminal type:

$ serverless create --template aws-nodejs --path serverless-demo-service

The create command will make another service. Stunner! Be that as it may, here’s the fun part. We have to pick a runtime for the function. This is known as the template. Going in aws-node will set the runtime to Node.js. Exactly what we need, a path will create a folder for the service. In this example, naming it serverless-demo-service.

Step 5. Explore the service directory with a code editor

Open up the serverless-demo-service folder with your favorite code editor. There should be three files in there. The serverless.yml contains all the configuration settings for this service. You can specify general configuration settings as well as per function settings. Your serverless.yml looks like this, only with a load of comments.

Open up the serverless-demo-service folder with your most loved code editor. There ought to be three files in there. The serverless.yml contains all the configuration settings for this service. Here you determine both general configuration settings and per function settings.

# serverless.yml
service: serverless-demo-service

Provider:
name: aws
runtime: nodejs6.10

Functions:
hello:
handler: handler.hello

The functions property records every one of the functions in the service. You can see hello is the main function currently in the handler.js file. The handler property focuses on the file and module containing the code you need to keep running in your function. As a matter of course, this handler document is named handler.js. Exceptionally advantageous for sure.

Opening up the handler.js you’ll see the handler module and function named hello. The function takes three parameters. The event parameter speaks to the event information go to the function. The context informs us concerning the context of the function, it’s running time, state and other vital data. The last parameter is a callback work which will send information back. In this model, the response is sent back as the second parameter of the callback work. The first dependably speaks to an error. On the off chance that there is no error null is passed along.

// handler.js
module.exports.hello = (event, context, callback) => {
const response = { statusCode: 200, body: 'Serverless Demo!' };
callback(null, response);
};

This is all great, but we still can’t trigger the function. As there is no event connected to it, hence no way to call/trigger the function. Let’s fix this. Jump back to the serverless.yml and uncomment the lines where you see events:

This is incredible, yet despite everything, we can’t trigger the function. There is no event associated with it, henceforth no real way to trigger the function. We should fix this. Hop back to the serverless.yml and uncomment the lines where you see events:

# serverless.yml
service: serverless-demo-service

provider:
name: aws
runtime: nodejs6.10

functions:
hello:
handler: handler.hello
events: # uncomment these lines
- http:
path: hello/get
method: get

Watch out so you don’t wreck the indentation of the file, events ought to be straightforwardly underneath handler. Incredible, with that done we can, at last, deploy the function to AWS.

Step 6. Deploying to AWS
The deployment process is very straightforward. Within the service directory run below command in your terminal:

$ serverless deploy -v

You’ll see the terminal light up with a huge amount of messages. That is the – v doing its enchantment.

But, most important here is what will log back the endpoint. Serverless has automagically created an API Gateway endpoint and connected it to the Lambda function. How awesome is that!? Hitting the endpoint in the browser will give back the text “Serverless Demo!”

Note: If you want to run the function through the command line you can run:

$ serverless invoke -f hello -l

This will return the full response object just as information in regards to the state of the Lambda work, for example, duration and memory utilization.

Relieving the pain

It is really irritating to need to deploy the function to AWS each time you need to test it out. Wouldn’t it be amazing if there was an approach to copy the environment locally?

With that unbalanced straying, Currently, I can at long last test all the code locally before pushing it to AWS. That diminishes a great deal of weight on my back.

It’s very easy to add Serverless Offline to your services. Introducing one npm module and adding two lines to the serverless.yml is all you need.

No better method to demonstrate it to you than to show you.

Step 1. Go to service directory and initialize the npm

Now you need to go to the serverless-demo-service directory and open up a terminal window in there. Once you are inside you can run:

Now you have to open up a terminal window and step inside the serverless-demo-service directory in there. Once inside you can run:

$ npm init

Step 2. Install Serverless Offline

With npm introduced there nothing more to do than simply run the installation.

$ npm install serverless-offline --save-dev

By using the –save-dev flag we save the package as a development dependency.

Before moving to the next step, you first need to let the terminal aware that it has a new command available. So within the serverless.yml file add two new lines.

# serverless.yml
service: serverless-demo-service

provider:
name: aws
runtime: nodejs6.10

functions:
hello:
handler: handler.hello
events:
- http:
path: hello/get
method: get
# adding these two lines
plugins:
- serverless-offline

Step 3. Run it locally

To ensure you’ve installed everything correctly run:

$ serverles

Note: If you need to check more helpful information about Serverless Offline run serverless offline –help in your terminal window.

Just go ahead and spin up the local emulation of Lambda and API Gateway.

$ serverless offline start

You’ll see every one of your routes recorded in the terminal. Your Lambdas are currently running on your localhost. The default port is 3000. Don’t hesitate to open up a program and look at it. Hitting the endpoint http://localhost:3000/hello/get will send back indistinguishable content from in the model above with the deployed function.

How great is this? Directly we don’t have to continuously push the code to AWS to check whether it’s working. We can test it locally and possibly push when we’re certain it works.

Wrapping up

You have now seen the progress from traditional web development into the serverless revolution. With these straightforward tools, we now have all that we have to make wonderful, scalable, and reliable applications. Tools like Serverless facilitate the painful transition amazingly well. They are of great help for going down the way of incredible obscure of serverless architecture.

Read more about What is Serverless Computing?

Request a quote