AWS Lambda is the new beast in town with its industry leading infrastructure, it is the best serverless technology out there thus far. One of the key use cases for serverless functions are bots. In theory, bots should be fast, responsive and infinitely scalable. If we think about web requests vs chatbot conversation multiplied by concurrent users, unless you want to spend infinite dollars for server maintenance and spend time for provisioning and managing your servers, personally I think that serverless is the way to go.

When building applications for AWS Lambda, the core components are functions and event sources. These are the things you need to know in order to get your first function up and running. In this post, I will cover what you have to do in order set it up yourself and later how to do the same using platform like recime to boost **productivity**.

Steps to get your first lambda function up and running :

Create the function

This basically a node module with the main entry-point that looks something like the following:

exports.myHandler = function(event, context, callback) {

console.log("value1 = " + event.key1);

console.log("value2 = " + event.key2);

callback(null, "some success message");

// or

// callback("some error type");

}

This should have reference to your original bot module where it will execute the bot and send the result back to the function. A typical nodejs lambda function will have following structure:

filename.js

node_modules/async

node_modules/async/lib

node_modules/async/lib/async.js

node_modules/async/package.json

Here async is the external module which I have used as an example. Once, you are ready zip everything up and additionally upload it to S3 if you are going to specify S3 as package source.

Prepare for Deployment and Setting up AWS Account + CLI

Select a template or blueprint and configure the function. This step can include adding the environment variables that your function will use, specifying the handler name and S3 location for your package or upload it directly using the wizard. Generally it is recommended to use S3 for package size more than 10MB.

Configure the trigger that will be used as an event source, which in this case is API Gateway. Now go to API Gateway console and create an REST API and use it as a trigger source. Your API needs to support both GET and POST in order to authenticate and execute the bot in platforms like Facebook, WeChat, etc. In addition, OPTIONS to handle CORS if you want to build a bot that will be used as a website widget.

Finally, your bot might need some local storage. File storage is not a good solution and if you are considering security and your bot will be used for health and insurance companies then it is best to use AWS DynamoDB which covers a lot of these standards required for such bots like HIPPA. There is a documentation on how to do the configuration and associate it with Lambda function. I would recommend to go through it.

Save and Test

I’ve left out few other things like configuring the right permission and how to create dynamoDB table properly with correct partition and sort key which I leave to the reader as an exercise.

Here to mention that developers are required to write the integration logic before connecting his/her bot to Facebook, Slack, Telegram, Viber, etc. Also, if you are using native BSON modules since your bot is eventually running in a linux environment then you will also need to build your package in a linux VM to have the correct binary package.

Now, the recime (ˈresəˌmē) way:

Create a Recime account

First, create an account with Recime.io. Install the CLI using brew or choco in macOS or windows or manually in linux (https://github.com/Recime/recime-cli). recime-cli is written in Go therefore it will work in wide varieties of operating systems with a very low footprint which Go is known for.

Login into Recime CLI (so that later you can analyze / manage the bot from developer dashboard) . Things like github integration, version control and multiple teams are in the pipeline as I write this post.

recime-cli login

Create your Bot ( — lang es6|typescript):

recime-cli create mint-bot

It will take you through a small wizard that basically fills up the package.json and prepares the module for you (it’s an npm module and you can use any number of npmjs modules you want). As for example, api.ai, wit.ai, Watson, etc.

Next step is to run and test the bot. You can do it by typing the following command:

recime-cli run -w

-w flag is for watch which I turn on in the background for doing live debugging with Facebook, Slack, Telegram, etc (please checkout the recime documentation for more).

Deploy

Finally, as I’ve just created my awesome bot, its time to deploy. I’ll do this by typing the following command:

recime-cli deploy

All the steps, I have mentioned above for deploying your Lambda function with API gateway is being done on the fly at this step and gives you the region specific endpoint after the process is completed.

This will also install any native modules (npm) and configure dynamoDB for use as key-value store. This is useful for storing coversational states and building a more engaging experience for your users:

db.set("sender", { "id" : 1}); db.get("sender").then((result)=>{

assert.equal(1, result.id);

});