Amazon Lex Chatbot

Build a Chatbot using Amazon Lex and AWS Lambda

Vikram E
3 min readMay 15, 2017

A chatbot is a service, powered by rules that are based on Artificial Intelligence(AI) and Machine Learning algorithms, that you interact with a chat interface. It could be a fun service or a real world service like Facebook Messenger, Slack etc to provide users more engaging experience. Oh yeah! a big opportunity all big companies are capitalizing and building one as you read this story.

How does Amazon Lex work? How to build your simple chatbot using AWS Lambda? We’re going to find out right away. To visualize a chatbot, we will create a bot to start and stop AWS EC2 servers.

Amazon Lex

First things first, login to aws management console and open Lex to create a custom bot. Once we create a bot(bot name, IAM role etc), next step is to create a custom Intent and a custom slot.

Create custom Lex bot

Let’s get familiar with few basic terms as we get started. An intent represents an action that fulfills a user’s request via chat. Create a custom Intent and name it startServer to start an EC2 Instance. A slot represents list of values used by the Intent. Custom slot instance of a custom slot type INSTANCE_NAME take values dev, qa, prod, uat. Sample Utterance is structured text that links the intent to the likely words/sentences types by the user. For example “I want to start a server”. This intent will be fulfilled by a AWS Lambda Function. You can add your prompt and Goodbye optional message to enrich the chat experience.

start server intent

AWS Lambda

Yes! we will use the cool stuff in cloud computing — serverless architecture. AWS lambda lets you run code without provisioning or managing servers. Login to aws management console and create Lambda function. Name your function and choose language in runtime section, in my case it is python 2.7

In the first section we will import boto3 library and define lambda_handler function to route the control to dispatch function.

lambda_handler

In the dispatch function, intent_name is extracted from the intent request and the control is transferred to the actual code to start the EC2 server start_ec2 function. Based on the value from the current intent slot value instance name tag is extracted. Use boto3 AWS SDK for python function start_instances function to start the EC2 server. Finally return close dialogAction to Informs Amazon Lex not to expect a response from the user and provide close message.

start ec2 server

Demo

demo
chatbot demo

That’s a simple rule based chatbot.

This bot is very limited. It can only respond to very specific commands. If you say the wrong thing, it doesn’t know what you mean. It is only as smart as it is programmed to be. We only scratched the tip of the iceberg. This introduction can help you build an AI based chatbots using Machine Learning, it can use Natural Language Process(NLP) and get smarter as it learns from the conversations.

Time to invent one :)

--

--