Create an Amazon Lex Bot Using a Blueprint
In this exercise, you do the following:
- Create your first Amazon Lex bot and test it in the Amazon Lex console. For this exercise,
you use the OrderFlowers blueprint.
- Create an AWS Lambda function and test it in the Lambda console. While processing a
request, your bot calls this Lambda function. For this exercise, you use a Lambda blueprint (lexorder-
flowers-python) provided in the AWS Lambda console to create your Lambda function.
The blueprint code illustrates how you can use the same Lambda function to perform
initialization and validation, and to fulfill the OrderFlowers intent.
- Update the bot to add the Lambda function as the code hook to fulfill the intent. Test the
Blueprints will have preconfigured tasks for both Chatbot and Lambda
Step 1: Create a Chatbot using Lex console:
- Navigate to Lex console.
- On the Bots page, select the Create option.
- On the Create your Lex bot page, provide the following information and then select the
- Choose the OrderFlowers blueprint.
- Leave the default bot name (OrderFlowers).
- For COPPA, select the No option.
- Select the Create option. The console makes the necessary requests to Amazon Lex to
save the configuration. The console then displays the bot editor window.
- Wait for confirmation that your bot is built and test the bot.
Step 2: Create a Lambda Function (Console)
- To create the Lambda function (console), navigate to Lambda service
- Select the Create function.
- On the Create function page, choose Blueprints. Type lex- in the filter text box to find
the blueprint, choose the lex-order-flowers-python blueprint.
- Lambda function blueprints are provided in both Node.js and Python. For this exercise,
use the Python-based blueprint.
- On the Basic information page, do the following, and then select the Create function.
- Type a Lambda function name (OrderFlowersCodeHook).
- For the IAM role, select the Create a new role from template(s).
- Type a role name (LexOrderFlowersRole).
- Leave the other default values.
- Select the Create function and test the Lambda function.
- Choose Select a test events, Configure test event.
- Choose Lex-Order Flowers from the Event template list. This sample event matches
the Amazon Lex request/response .Give the test event a name
- Select the Create option.
- Verify that the Lambda function successfully executed. The response, in this case,
matches the Amazon Lex response model.
Step 3: Add the Lambda Function as Code Hook (Console)
- In the Amazon Lex console, select the OrderFlowers bot. The console shows the
OrderFlowers intent. Make sure that the intent version is set to $LATEST because this is
the only version that we can modify.
- Add the Lambda function as the fulfillment code hook and test it.
o In the Editor, choose AWS Lambda function as Fulfillment, and select the Lambda
function that you created in the preceding step (OrderFlowersCodeHook). Select
the OK option to give Amazon Lex permission to invoke the Lambda function.
o You are configuring this Lambda function as a code hook to fulfill the intent.
Amazon Lex invokes this function only after it has all the necessary slot data
from the user to fulfill the intent.
o Specify a Goodbye message.
o Choose Build.
o Test the bot using the previous conversation.
The last statement “Thanks, your order for roses…..” is a response from the Lambda function
that you configured as a code hook. In the preceding section, there was no Lambda function.
Now you are using a Lambda function to actually fulfill the OrderFlowers intent.
- Add the Lambda function as an initialization and validation code hook, and test.
The sample Lambda function code that you are using can both perform user input validation
and fulfillment. The input event the Lambda function receives has a field (invocationSource)
that the code uses to determine what portion of the code to execute.
Select the $LATEST version of the OrderFlowers intent. That’s is the only version that you can
- In the Editor, choose Initialization and validation in Options.
- Again, select the same Lambda function.
- Choose Build.
- Test the bot.
You are now ready to converse with Amazon Lex. To test the validation portion, choose time 6
PM, and your Lambda function returns a response (“Our business hours are from 10 AM to 5
PM.”), and prompts you again. After you provide all the valid slot data, the Lambda function
fulfills the order.