A Guide to Amazon Lex and AWS Lambda Integration for Seamless Conversations

A Guide to Amazon Lex and AWS Lambda Integration for Seamless Conversations

Hello Learners, Hope you all have successfully built a chatbot using Amazon Lex.

If you haven't, go through Part 1, and click HERE .

In this blog, we will show, how to actually run the bot in a serverless approach. Also, we will use AWS Cloudwatch Service, to view our logs.

🎯AWS Services used:-

  1. Amazon Lex

  2. Aws Lambda

  3. Amazon Cloudwatch

So let's get started.

Step - 1: Create a Python Lambda function.

Create a lambda function for our bot. I've chosen Python 3.8 version here.

Now we need to add our Python function here. As we only created order intent, I'll add only the order function here. If you have created an intent for order and cancel both, you can check the whole code here.

The code is having discounts and other stuff.

©️ Code credit goes to - Architecture Bytes

Now Just update your code as shown below. And "Deploy"

Step-2: Configure Lambda on Intent

Go to the intent, for me it is "CreateOrderIntent".

Expand the fulfillment > Click on Advanced Options > chek the box "Use a Lambda function for fulfillment" as shown above.

Then update the intent.

Step-3 : Create a bot version with alias (Not Mandotary)

Till now we were using the demo version, it's time to create our own version.

Move to "bot versions" and "Create version". Note, it's not a mandatory step, I added it to let you all know about it as it will help in reducing the downtime in scenarios where modification and re-deployment are required.

Then simply go down and click on "Create".

You can see, now we have our own Version 1.

Now, we will create Alias.

In Amazon Lex, an alias is a label or reference to a specific version or snapshot of a chatbot. It helps manage multiple versions of the chatbot and allows you to control which version is active or live at any given time. Aliases are useful for A/B testing, gradual updates, or maintaining different versions for different environments. By assigning an alias to a version, you can update the chatbot by publishing a new version and redirect the alias to it, without modifying integrations or endpoints.

To create an alias, follow the below steps.

Move to Alias Section > Create Alias > Give a name > Choose the version (For us it's Version 1) > Create

Now , go to our newly created alias and click on the language "English US"

Now, in the source, give our function name and the version will be selected automatically as $Latest which is like a variable that will take the latest version always.

Click on Save.

A Snapshot of your work is saved with the lambda function active. You can use that while testing the draft version.

Now, You guys may face an issue 😶that I faced! Let's learn how to troubleshoot that also.

Troubleshoot Error :

As we all know, my purpose here extends beyond showcasing finished work. It is my genuine commitment to care for my fellow learners and our community. I encountered an error that led me to reflect on its nature and explore potential solutions.

Driven by my desire to help, I diligently troubleshooted the issue. Now, I feel compelled to share this information with all of you, ensuring that everyone is aware and equipped with the knowledge to tackle similar challenges.

So now, as already you have built the draft version, it has the lambda function integrated it may seem.

So, now if you just simply try to test, it will throw you an error like this ;

Cannot call FulfillmentCodeHook for Intent CreateOrderIntent. BotAlias/LocaleId TestBotAlias/en_US doesn't have an associated Lambda Function.

If you read the error message, it shows that our test alias still hasn't been associated with a Lambda function. Remember It's about the Test draft Alias. For version 1, we have added the source already.

To resolve the issue, we will simply associate our lambda function with this alias too, simple :).

To do this, click on the gear icon at the top right corner of the chat box.

Add the source here and "Save".

And Boooom! 🎉You can now see the output is coming as per the lambda function! 🤩😍

If we want to confirm, whether the lambda function is working or not, simply go to AWS Cloud Watch and you will see a new log group is automatically created. ✌️✌️

These are the logs generated for this function.

💡Any modification or contribution to this is highly appreciated.

💖 Thanks for being with me till the end. I hope that I provided you with some useful knowledge. Don't forget to spread the blog to your friends and community. 💖

Feel free to ask any more questions or for help if you need it.👍

Follow Sitabja Chatterjee for more content! ✌️