Let’s face it, in this regard AWS documentation is quite a piece of crap. And there is no doc on how to use Amazon Lex in Websites (random websites, not as a slack, Facebook plugin). After creating a bot in Lex console, they don’t even give you the option to use the bot from anything but mobile. Only SDKs for mobiles are shown.
If the bot itself is talking meaningless, I can’t do anything. It’s a sample bot shipped in AWS.
Demo is deployed here (Have mercy if it’s a bit slow. It’s heroku free tier).
API Gateway + Lex + Lambda + Magic = Awesomeness
The idea is to use API Gateway + AWS Lambda as a proxy layer between Web App and Amazon Lex. So the first requirement,
Getting The Lex Bot Up and Running
The point of this post is to show how Lex can work with Web Apps, not how to create a bot itself. So we’ll cut some corners and we’ll use the OrderFlowers bot available as an example bot in Lex.
Once the bot is created, you just need to build and publish it. Ah! pretty straight forward. Now our bot is ready, that in theory will be able to interact with human.
Creating The Lambda Proxy
For simplicity we’ll create the lambda just to get the message from a user and forward it to Lex. But normally that should not be the case. This lambda should ideally do all the pre-processing to make the input simpler. Because being frank, by trying hard to provide modularity (or whatever) Amazon has lost most of the power that a NLP bot should have in terms of word similarity and meaning extraction. Google’s Syntaxnet Parsey McParseface does this job way better. But you need to maintain infra and more importantly Amazon made something, they can sell it way better. So people will use Lex anyway (myself included).
Before creating the Lambda, we need to create a policy,
- Sign in to the IAM console at https://console.aws.amazon.com/iam/.
- For Roles, choose Create a new role. For AWS Service Role, choose Lambda.
- On the next page, choose Next Step without selecting a policy (you’ll define a custom policy). Name this role lambda-role-for-lex, and then choose Create role.
- Search the list of roles to find the new role. Expand the Inline Policies section and create a new custom policy by pasting in this JSON:
For the sake of it, you can double check if the policy is working. Go to IAM > Roles > LexForLambda. Click on inline policy and simulate policy. You should select Amazon Lex and postText in policy simulator. The result should be allowed.
Now we are ready to create our actual Lambda function,
- Log in to the AWS Lambda console, and choose Create a Lambda function.
- Choose the Author From Scratch.
- Leave the trigger empty, and then choose Next.
- For the Lambda function, type LexLambdaAccess, and for the Runtime environment, choose js 6.10.
- Leave the Handler set to index.handler, and then choose Choose an existing role. Type lambda-role-for-lex, which is the role that you created for this Lambda function to assume.
- Paste the following code.
- Click on Save.
At this point our Lambda is ready to proxy all requests to Lex service.
Creating an API Gateway Endpoint
This is the last step to get something up.
- Log in to the API Gateway console, and choose Create API.
- Give the API the name anything (in my case Cabbie). Choose Create API.
- On the Resources page, choose Actions, and then choose Create Resource.
- Give name message. And click on Create Resource.
- Click on the message resource on the Resources page, choose Actions, and then choose Create Method. Select POST from the drop down and click the tick icon.
- Set the following details and click on save.
- Now click on Integration Request.
- And configure Body Mapping Template. As shown
- Click on Save. Now our API gateway is ready to serve requests. Just Deploy the API from Action menu. We’re done.
Time to Test
We’ll use Postman to see the API endpoint is working or not. It’s very simple.
So we have successfully created an API to wrap LEX and provide answers to the outside world (Web, Mobile, Anything that can make a Web Request). That’s all we need to use Amazon Lex from Web App. For the paranoids, I’ve created a project to use the API via express (Node.js). Available Here.
Note: Dont be confused by the name. I forked the repo from heroku nodejs getting started to quickly deploy my project to heroku. The description proves that. And frankly I don’t have any energy left to Edit readme.md
Either way the demo is deployed here (Have mercy if it’s a bit slow. It’s heroku free tier).