Create and deploy a Chatbot using Amazon Lex, AWS Amplify, Express

ky lim
4 min readMar 26, 2021

--

In this tutorial, we want to create a chatbot that can work as a microservice for other applications use, through an API interface. This tutorial can also be applied to those who want to integrate their Amazon Lex chatbot to their React, Angular, React Native app (javascript) and deploy the app. The chatbot is created using Amazon Lex. Then AWS Amplify is used to integrate the chatbot to an Express App which creates the REST API that would be deployed using Serverless.

Create a chatbot with Amazon Lex by AWS

First, we shall create our desired custom chatbot. To do so, we shall use Amazon Lex. Amazon Lex is a service for building conversational interfaces into any application using voice and text. Before you begin, you would first have to understand how to use Amazon Lex and some basic terminologies. It is pretty straightforward and user-friendly with a GUI console. If you have tried it before then you are good to go. Otherwise, you can take a look at some of the following materials to learn how to use Amazon Lex.

Video Tutorial from Edureka!:

Hands-on step by step tutorial from the Amazon Lex Developer Guide:

Create a basic Express App for the integration of the chatbot later

Now we shall create a skeleton Express App that we will edit later to integrate the chatbot. This is very simple and there are many tutorials online. Here’s one that you can follow! It includes deployment but you can skip that for now.

Use Amazon Amplify and Amazon Cognito to integrate chatbot into Express App

Make sure you have installed the Amplify CLI. If you need to do so, follow this tutorial page. For the last paragraph which states to do ‘amplify init’, do it in the source directory of your Express App. Then just answer the questions prompted. Choose your preferred editor. For the type of app that you are building, choose ‘Javascript’. For the framework, you can choose ‘None’. The rest of the questions, you can just choose the default one.

Once AWS Amplify is initialised in the project folder, go to the aws-exports.js file and remove all the code before pasting the following code in. Replace the “your-bot-name” with your Amazon Lex chatbot’s name.

awsconfig = {   
Auth: {
identityPoolId: 'us-east-1:xxx-xxx-xxx-xxx-xxx',
region: 'us-east-1'
},
Interactions: {
bots: {
"your-bot-name": {
"name": "your-bot-name",
"alias": "$LATEST",
"region": "us-east-1",
},
}
}
};
module.exports = awsconfig;

For the Auth, we need to set up an Amazon Cognito identity pool. To set up the Amazon Cognito, just follow this tutorial page. Take note of the identity pool ID and use it to replace ‘us-east-1:xxx-xxx-xxx-xxx-xxx’ in the code snippet above.

Once all that is done we just need to integrate the Chatbot to the Express App. This would require us to edit the app.js file. A code snippet of how this can be done is shown below. You can customise the routes and the return values or even add more chatbots to this project. (app.js and aws-exports.js are in the same directory). If you are integrating your Chatbot to an existing app, you can use this method to create an API which can be called by your app.

const Amplify = require('aws-amplify');
const { Interactions } = require('aws-amplify');
const awsconfig = require('./aws-exports')
Amplify.default.configure(awsconfig);const serverless = require('serverless-http');
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/api/info', (req, res) => {
res.send({ application: 'Book Trip Bot ', version: '1' });
});
app.post('/api/intent', async (req, res) => {
const userInput = req.body.userInput;
const response = await Interactions.send("Your-bot-name", userInput);
res.send(response);
});
app.listen(port, () => console.log(`Listening on: port ${port}`));
//module.exports.handler = serverless(app);

To test if your API works, use insomnia/postman or similar web apps to send HTTP API requests to your running Express App.

Now all that is left is to deploy it! You can simply continue the tutorial above that included deployment of Express App to AWS Lambda. Do take note that to input your credentials do the following:

serverless config credentials -p aws -k ACCESS_KEY_ID -s ACCESS_SECRET_KEY

You may also have to edit some parameters in the .yml file if any error occurs. For the runtime, I had to use nodejs14.x. I hope this was helpful to you and I guess it was rather simple now that it’s done. Personally, I was slightly confused at the beginning on how to do this and I tried using ‘amplify add api’, ‘amplify add interactions’ to try to integrate the Amazon Lex chatbot into the Express App but then I found this way to be much simpler and there are way fewer files created. Credits to all the different tutorials, documentations I linked in this post.

--

--

ky lim

Aspiring Software Developer who is currently in the last year of my Computer Science major at the National University of Singapore