A serverless architecture is a way to build and run applications and services without having to manage infrastructure. - AWS Documentation

Functions as a Service (FaaS)

Serverless computing - It gives us the ability to deploy any individual piece of code, or function. The code runs and returns a value, in turn ending the process.

If we write a REST API all the services and endpoints we  usually keep in one place are now sliced up into tiny snippets, microservices.  Such services are easy to scale.

The tools and services.

  1. AWS Lambda-
    AWS Lambda is a compute service that lets you run code without provisioning or managing servers. - AWS Documentation
    Lambda is an event based system for running code in the cloud. We don’t worry about servers, it scales automatically and charges for the time it actually is
    running the code.
  2. AWS API Gateway-
    Lambda would be incomplete without the API Gateway. Lambda functions need an event to be triggered to invoke them. API Gateway provides the REST endpoints which trigger the functions.
  3. The Serverless framework-
    The Serverless framework bundles up the tools we need into a package, and makes it simple to create and deploy the application. It abstracts all the tedious tasks we have to do in the AWS Console, which includes creating a function.

A simple Serverless function

1. Install Serverless framework

Open terminal and run

$ npm install -g serverless

2. Create IAM User

In AWS console create new IAM user and give programmatic access. Provide user AdministratorAccess. Once the user is created download the Access Key ID and Secret Access Key.

3. Enter IAM keys in the Serverless configuration

Open terminal and type the following command

$ serverless config credentials --provider aws --key your-Access Key ID --secret your-Secret Access Key

This tells Serverless the account to connect to when we run terminal commands.

4. Create a service

A service is where we define Lambda Functions, the events that trigger them and any resources they require in a file called serverless.yml.
In terminal type

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

The command will create a service. When creating a new service we need to pick a runtime for the function called as template. The aws-node  will set the runtime to Node.js. The path will create a folder for the service.

5. Explore the service directory with a code editor

Open the my-service folder with code editor. The serverless.yml contains the configuration settings for this service. Here we can specify both general configuration settings and per function settings. The serverless.yml looks like this-

service: my-service  provider:  
 name: aws  
 runtime: nodejs6.10  
functions:
 hello:
   handler: handler.hello

The functions lists all the functions in the service. Initially there is only one service hello in handler.js. The handler pointer to the file and modules containing the code.
In handler.js there is a handler module and function named hello. It looks like this

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

Here there is no event connected to it, so  we can’t trigger the function. To trigger the event in serverless.yml uncomment the lines under events.

# serverless.yml
service: my-serviceprovider:  
 name: aws  
 runtime: nodejs6.10
functions:
 hello:
   handler: handler.hello
   events: # uncomment these lines
     - http:
         path: hello/get
         method: get

6. Deploying to AWS

Run this command to deploy

$ serverless deploy -v

Serverless will automagically create an API Gateway endpoint connected to the Lambda function. How awesome is that!?  
Enter the endpoint in the browser you will see the text Go Serverless.