The core of an application is the server that it’s operating on. But today modern technologies trends have evolved in such a way that there is no need for a server for your application or specifically your web applications. Well it is not actually that you don’t have a server. We have let say a system that won’t be running all the time like the particular server instead it will operate based on the usage or in other words it will be running if it is required to.
For example we have an API for an application that has the functionality of sending an email. In a server orientated architecture there would be a server (running PHP, Nodejs, python … ) that would run the system all the run and send an email when API is called. Here our system is continuously running all the time despite API being called or not. But what if we have to make our server run only when API is called unlike the earlier one. Yes lets dive into the world of SERVERLESS.
One of the primary advantages of going serverless is the cost. You only have to pay when it is working, unlike the conventional server system. Suppose what if the API is not called at all, in that case you still have to pay for it. But in serverless you are only paying for on-demand running only. Now let dive into action how it can be achieved.
Amazon AWS Lambda is a serverless application service. Using AWS Lambda service, we can deploy any number of serverless applications and pay for only what is really used which is time to process the request.
First you need to have the Amazon AWS account and AWS Access and Secret keys. Follow the steps below
- Log in to your account and go to security credentials page.
- Go to users and click on Add User button.
- Give a name, and check Programmatic Access check box.
- In the next screen, click on Attach Existing policies tab and search for “AdministratorAccess”. Check that permission and click on Review button and create a user. We need administrator access to our CLI to run
- Now copy that access key and secret key.
Back to our system. Let check we have working versions of npm/node. To create a Serverless application, we need to install it an NPM package called serverless as globally.
npm install -g serverless
Let it install and once it is installed, create a new serverless application Let’s create a boilerplate application of aws nodejs template
sls create --template aws-nodejs --path going-serverless-service
It will create a new serverless application as going-serverless-service
Switch to going-serverless-service project directory. We have two files as serverless.yml and second is handler.js.
serverless.yml
service: going-serverless-service
provider:
name: aws
runtime: nodejs8.10
functions:
runaway:
handler: handler.runaway
events:
- http:
path: runaway
method: post
plugins:
- serverless-offline
- service block defines the name of your service.
- provider is the base provider and environment of the application that we want to run on. Make sure its the identical that you have on your system.
- functions block take the Request and call the handler. Here request is runaway and handler is runaway and code for that is in handler.js.
Functions events are the parameters for which for what API the corresponding function be called. It has paths and methods like those of HTTP requests. It is executed with AWS API Gateway(optional also) - plugins block install third party necessary libraries to be able by our serverless system. Here serverless-offline is used. Make sure to install it by running install command and save to your package json
npm i serverless-offline --save-dev
This serverless offline makes use of local development for checking the correctness of our functions. This prevents us to deploy untested functions to the lambda server. After installation, run
sls offline
And goto the URL that it shows, Basically its https://localhost:3000/
handler.js
'use strict';
module.exports.runaway = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
// Return the response back
callback(null, response);
};
Here is our runaway function which returns the simple response. Note that we are getting event and context parameters from AWS Lambda.
Handler name and the function name is not mandatory to be same. If we check https://localhost:3000/runway , we get the message as defined
Now its tested lets deploye to the our aws lamda service.
first download aws cli and run
aws configure
Just inserted the asked access code and secret that you had achieved earlier.
Then let the serverless npm package do the rest of all by entering command.
sls deploy
After some loading it will give us the endpoint according to the our handler script.
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (22.73 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
......................................................................................................
Serverless: Stack update finished...
Service Information
service: going-serverless-service
stage: dev
region: us-east-1
stack: going-serverless-service
api keys:
None
endpoints:
POST - https://2dj8uurjx6.execute-api.us-east-1.amazonaws.com/endpointe
we get all the endpoints which will be different. Now open our AWS console on the browser and go to lambda screen. We should see your functions listed under the Functions tab.
That’s it, we just created our new basic serverless application using AWS lambda functions. Be sure to have a look at the AWS functions documentation for detailed info