Serverless Architecture

In this blogpost we will explore the idea of Serverless Architecture and how it can help us reducing operational cost and complexity when creating new services.

In this blogpost we use the term Serverless as custom code that’s run in an ephemeral context. More know as Function as a Service (Fass), we can see it as an abstraction layer above Platform as a service (PaaS).

The main features of using a Serverless Architecture are:

  • No server provisioning and maintenance
  • Built-in automatic scaling
  • Only pay for what you use
  • You have to manage the persistence of the state

You can run your function is response to events, it can be an HTTP request that triggers your function or it can be the upload of a file in your storage system.

There are a lot of cloud-computing providers that already provide this architecture, such as AWS Lambda, Google Functions, Azure Functions, etc.

There are some frameworks that can help you when developing your Serverless application, like ServerlessClaudia or Apex.

In this blogpost we will explore how it’s possible to setup a Facebook Messenger bot under 15 minutes using this approach.

We will be using the ClaudiaJs framework, more precisely the Claudia Bot Builder.

We will deploy our bot to AWS Lambda.

Prerequisites

  • Node.js 4.3.2
  • NPM
  • Claudia.js 1.4.0 or later
  • An AWS account with the permissions to create Lambda functions, API Gateway end-points and IAM roles:

Screen Shot 2017-01-22 at 19.50.00.png

To setup your environment with the AWS credentials you can install the AWS CLI ($ brew install awscli), and run $ aws configure

Creating the bot

We need to create an empty folder and set a new NPM project inside it:

$ mkdir fb-bot

$ cd fb-bot

$ npm init -y

Then we need to add Claudia Bot Builder as a dependency:

$ npm install claudia-bot-builder -S

Then we create a file with the name bot.js, this will be the function called when someone writes to our bot:

$ touch bot.js

The content of bot.js is the following:

var botBuilder = require(‘claudia-bot-builder’);

module.exports = botBuilder(function (request) {
return ‘Hello world!’;
}, {platforms: [‘facebook’]});

We import the claudia-bot-builder module and when someone invokes it we return “Hello world!”. The final platforms indicates we only want to deploy a Facebook Messenger bot and not all the platforms available.

In order to deploy our function to AWS we run:

$ claudia create –region us-east-1 –api-module bot

Then we need to hook up our bot with Facebook Messenger:

$ claudia update –configure-fb-bot

We need to generate the page access token from Facebook Developers, and copy that back to Claudia when asked. For that you need to create a new App and a Page in the Facebook platform.

Screen Shot 2017-01-22 at 20.11.55.png

Screen Shot 2017-01-22 at 20.20.54.png

Then you have to setup a web hook in order for Facebook to call our function each time someones sends a message:

Screen Shot 2017-01-22 at 20.21.55.png

Screen Shot 2017-01-22 at 20.23.33.png

If you want to make the bot public, submit the app for App Review.

You can then test the bot by sending it a message trough Facebook:

Screen Shot 2017-01-22 at 20.24.44.png

You can check the sample project at our GitHub.

References

If you have any doubt or want to explore how it is possible to create bots for other platforms check out this blog post from Claudia.Js.

Mike Roberts write an excellent article exploring the theme and the different meanings Serverless has that you can find here.

You can find a very nice presentation of how to get started with Serverless Architectures by Jeremy Edberg the cofounder of CloudNative here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s