Automating the creation of an AWS Lex and Lambda chatbots with Python

Part of my role at Rubrik is to educate and help customers and technologists who want to develop scripts and solutions utilizing APIs. One of my favorite ways to do that is through the creation of a chatbot, at Rubrik, we use Roxie, Rubrik’s intelligent personal assistant. Roxie is built utilizing two AWS services; Lex and Lambda. Lex handles the conversational pieces, while Lambda goes out and fulfills the question (or, you know, gets the answer). While it’s most definitely a great exercise to go through the process of creating Roxie manually (I promise, you’ll learn lots about AWS and consuming APIs in general), it’s also not something I enjoy doing over, and over, and over, and over (you get the picture). So with that said, during my last little development stint with Roxie I decided to figure out how to automate the complete creation of both the Lex Bot itself and the Lambda function! My weapon of choice, Python – the results, well, I think they are pretty good! So with that said, let me walk through the process of automating the creation of a Lex and Lambda chatbot with Python.

Prerequisites

As with anything tech/developer-related, there are a number of prerequisites that must be met in order to successfully follow along here, some obvious, some are not…

  • Python – yeah, you’ll need python installed on your workstation
  • AWS Account – pretty obvious, but you will need an AWS account to play with
  • AWS CLI – Having the AWS CLI installed and configured just makes things easier, that way we can just grab access and secret keys for the python code directly from your credentials file.
  • Boto3 SDK – We will be interacting with a number of AWS services via Python, so boto3 is a must! (pip install boto3)
  • AWS Lambda Role – An IAM role within AWS to assign to the Lambda function
  • AWS Lex Role – An IAM role within AWS to allow the Lex bot to run under. This is autocreated when you manually create a bot, but not when you programatically do it. You are best to simply go and manually create a test bot, get the Role ARN, then delete the bot 🙂

With these in place, let’s just jump right into the code!

Step 1 – Create the Lambda Function

Sometimes it’s just easier to see all the code and then break it down further afterward.

Alright, let’s break it down

  • Lines 1-3 – These simply import packages
  • Lines 5-14 – This defines a variety of variables that we will need for the configuration
  • Lines 16-18 – This ensures that we are working within the proper AWS region
  • Line 21 – Establishes a Lambda client
  • Lines 23-40 – Code to actually create the Lambda function.
    • Line 27 – This defines the role under which the fuction will run, this will need to be created a head of time.
    • Line 28 – This points to a zip file containing the code for the Lambda function
    • Lines 30-33 – The VPC configuration the function will run within
    • Lines 34-39 – Any environment variables you want to live within your Lambda function.

Now we aren’t actually completely done with Lambda as of yet, we will need to add permissions to our function allowing our Lex Bot to execute it. That said, we don’t have a Lex Bot yet, so let’s create that, and we’ll come back to Lambda.

Step 2 – Create the Lex Bot

So Lambda was pretty quick, but Lex is a little more involved and has a few more moving parts! Again, let me show you the complete code, and further break it down… Just a note, the following code utilizes the Lex v2 APIs!

Alright, again, let’s break this all down

  • Lines 1-5 – import things
  • Lines 7-14 – provide some configuration items
  • Lines 16-18 – establish we are in the proper region
  • Line 20 – Creates a Lex client
  • Line 21 – Creates a Lambda client
  • Lines 23-32 – This is the code to initially create the Bot
    • Line 27 – Sets the COPPA value to false
    • Line 31 – Specifies the proper AWS Lex IAM role (created automatically when manually creating a bot)
  • Line 33 – Retrieves the newly created Bot ID
  • Lines 35-50 – Polls the bot creation status to ensure it is indeed created before continuing
  • Lines 52-78 – Creates the en_US locale within the bot, and waits until complete
  • Lines 80-99 – Creates some intents within the bot. In this case, intents are specified in a separate file (intents.json). I’ll provide a sample of that file below.
  • Lines 101 – 106 – This grabs the bot alias ID as we need it further down
  • Lines 108-112 – This grabs the ARN for our Lambda function, as we need it further down
  • Lines 113-131 – This instructs our Lex bot to utilize the specified Lambda function to fulfill intents
  • Lines 133-144 – This adds a permission to the Lambda function, allowing the Lex Bot to execute it.
  • Lines 146-152 – This simply just kicks off a build process on our bot.

And that’s basically it! Hopefully, these two chunks of code may help you through the process of automating the creation of a Lex and Lambda chatbot with Python I know this has saved me a ton of time and has helped to simplify things for our customers looking to simply get a chatbot up and running, If you are interested, check out the Roxie project. The python script located there goes through the complete process of even generating the configuration. Thanks for reading!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top