Using Amazon Lex with Web Apps (Lex+Lambda+API Gateway)

Pablo Orders Flowers from Pena

Pablo Orders Flowers from Pena

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.

Disclaimer

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.

lex_bot_creation

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,

  1. Sign in to the IAM console at https://console.aws.amazon.com/iam/.
  2. For Roles, choose Create a new role. For AWS Service Role, choose Lambda.
  3. 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.
  4. 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.

policy_sim_aws

Now we are ready to create our actual Lambda function,

  1. Log in to the AWS Lambda console, and choose Create a Lambda function.
  2. Choose the Author From Scratch.
  3. Leave the trigger empty, and then choose Next.
  4. For the Lambda function, type LexLambdaAccess, and for the Runtime environment, choose js 6.10.
  5. 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.
  6. Paste the following code.
  7. 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.

  1. Log in to the API Gateway console, and choose Create API.
  2. Give the API the name anything (in my case Cabbie). Choose Create API.
  3. On the Resources page, choose Actions, and then choose Create Resource.
  4. Give name message. And click on Create Resource.
  5. 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.
  6. Set the following details and click on save. api_gateway
  7. Now click on Integration Request.integration_request_lambda
  8. And configure Body Mapping Template. As showntemplate
  9. 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.

postman_test

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).

4 comments for “Using Amazon Lex with Web Apps (Lex+Lambda+API Gateway)

  1. akshay
    November 6, 2017 at 1:06 pm

    Could you please explain to me how to integrate our own Api’s with lex bot using lambda?

  2. Karthik
    May 18, 2018 at 4:03 am

    Can you please share the code snippet of body mapping template for the API Gateway?

  3. Jackson
    September 19, 2018 at 11:06 pm

    Where do you find the user ID for the uuid in the JSON you post?

Leave a Reply

Your email address will not be published.