

End of support notice: On September 15, 2025, AWS will discontinue support for Amazon Lex V1. After September 15, 2025, you will no longer be able to access the Amazon Lex V1 console or Amazon Lex V1 resources. If you are using Amazon Lex V2, refer to the [Amazon Lex V2 guide](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html) instead. . 

# Exercise 2: Create a Custom Amazon Lex Bot
Exercise 2: Create a Custom Bot

In this exercise, you use the Amazon Lex console to create a custom bot that orders pizza (`OrderPizzaBot`). You configure the bot by adding a custom intent (`OrderPizza`), defining custom slot types, and defining the slots required to fulfill a pizza order (pizza crust, size, and so on). For more information about slot types and slots, see [Amazon Lex: How It Works](how-it-works.md).

**Topics**
+ [

# Step 1: Create a Lambda Function
](gs2-prepare.md)
+ [

# Step 2: Create a Bot
](gs2-create-bot.md)
+ [

# Step 3: Build and Test the Bot
](gs2-build-and-test.md)
+ [

# Step 4 (Optional): Clean up
](gs2-clean-up.md)

# Step 1: Create a Lambda Function


First, create a Lambda function which fulfills a pizza order. You specify this function in your Amazon Lex bot, which you create in the next section.

**To create a Lambda function**



1. Sign in to the AWS Management Console and open the AWS Lambda console at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).

1. Choose **Create function**.

1. On the **Create function** page, choose **Author from scratch**. 

   Because you are using custom code provided to you in this exercise to create a Lambda function, you choose author the function from scratch.

   Do the following:

   1. Type the name (`PizzaOrderProcessor`).

   1. For the **Runtime**, choose the latest version of Node.js.

   1. For the **Role**, choose **Create new role from template(s)**.

   1. Enter a new role name (`PizzaOrderProcessorRole`).

   1. Choose **Create function**.

1. On the function page, do the following: 

   In the **Function code** section, choose **Edit code inline**, and then copy the following Node.js function code and paste it in the window. 

   ```
   'use strict';
        
   // Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes")
   function close(sessionAttributes, fulfillmentState, message) {
       return {
           sessionAttributes,
           dialogAction: {
               type: 'Close',
               fulfillmentState,
               message,
           },
       };
   }
    
   // --------------- Events -----------------------
    
   function dispatch(intentRequest, callback) {
       console.log(`request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);
       const sessionAttributes = intentRequest.sessionAttributes;
       const slots = intentRequest.currentIntent.slots;
       const crust = slots.crust;
       const size = slots.size;
       const pizzaKind = slots.pizzaKind;
       
       callback(close(sessionAttributes, 'Fulfilled',
       {'contentType': 'PlainText', 'content': `Okay, I have ordered your ${size} ${pizzaKind} pizza on ${crust} crust`}));
       
   }
    
   // --------------- Main handler -----------------------
    
   // Route the incoming request based on intent.
   // The JSON body of the request is provided in the event slot.
   export const handler = (event, context, callback) => {
       try {
           dispatch(event,
               (response) => {
                   callback(null, response);
               });
       } catch (err) {
           callback(err);
       }
   };
   ```

1. Choose **Save**.

## Test the Lambda Function Using Sample Event Data


In the console, test the Lambda function by using sample event data to manually invoke it. 

**To test the Lambda function:**

1. Sign in to the AWS Management Console and open the AWS Lambda console at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).

1. On the **Lambda function** page, choose the Lambda function (`PizzaOrderProcessor).`

1. On the function page, in the list of test events, choose **Configure test events**.

1. On the **Configure test event** page, do the following: 

   1. Choose **Create new test event**.

   1. In the **Event name** field, enter a name for the event (`PizzaOrderProcessorTest`).

   1. Copy the following Amazon Lex event into the window. 

      ```
      {
        "messageVersion": "1.0",
        "invocationSource": "FulfillmentCodeHook",
        "userId": "user-1",
        "sessionAttributes": {},
        "bot": {
          "name": "PizzaOrderingApp",
          "alias": "$LATEST",
          "version": "$LATEST"
        },
        "outputDialogMode": "Text",
        "currentIntent": {
          "name": "OrderPizza",
          "slots": {
            "size": "large",
            "pizzaKind": "meat",
            "crust": "thin"
          },
          "confirmationStatus": "None"
        }
      }
      ```

1. Choose **Create**.

AWS Lambda creates the test and you go back to the function page. Choose **Test** and Lambda runs your Lambda function.

In the result box, choose **Details**. The console displays the following output in the **Execution result** pane. 

```
{
  "sessionAttributes": {},
  "dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": "Okay, I have ordered your large meat pizza on thin crust."
    }
}
```

## Next Step


[Step 2: Create a Bot](gs2-create-bot.md)

# Step 2: Create a Bot


In this step, you create a bot to handle pizza orders. 

**Topics**
+ [

# Create the Bot
](gs2-create-bot-create.md)
+ [

# Create an Intent
](gs2-create-bot-intent.md)
+ [

# Create Slot Types
](gs2-create-bot-slot-types.md)
+ [

# Configure the Intent
](gs2-create-bot-configure-intent.md)
+ [

# Configure the Bot
](gs2-create-bot-configure-bot.md)

# Create the Bot


Create the `PizzaOrderingBot` bot with the minimum information needed. You add an intent, an action that the user wants to perform, for the bot later.

**To create the bot**

1. Sign in to the AWS Management Console and open the Amazon Lex console at [https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/).

1. Create a bot.

   1. If you are creating your first bot, choose **Get Started**. Otherwise, choose **Bots**, and then choose **Create**. 

   1. On the **Create your Lex bot** page, choose **Custom bot** and provide the following information:
      + **Bot name**: PizzaOrderingBot 
      + **Language**: Choose the language and locale for your bot.
      + **Output voice**: Salli 
      + **Session timeout **: 5 minutes.
      + **COPPA**: Choose the appropriate response.
      + **User utterance storage: Choose the appropriate response.**

   1. Choose **Create**. 

      The console sends Amazon Lex a request to create a new bot. Amazon Lex sets the bot version to `$LATEST`. After creating the bot, Amazon Lex shows the bot **Editor** tab, as in the following image:  
![\[\]](http://docs.aws.amazon.com/lex/latest/dg/images/gs1-20.png)
      + The bot version, **Latest**, appears next to the bot name in the console. New Amazon Lex resources have `$LATEST` as the version. For more information, see [Versioning and Aliases](versioning-aliases.md).
      + Because you haven't created any intents or slots types, none are listed. 
      + **Build** and **Publish** are bot-level activities. After you configure the entire bot, you'll learn more about these activities.

## Next Step


[Create an Intent](gs2-create-bot-intent.md)

# Create an Intent


Now, create the `OrderPizza` intent , an action that the user wants to perform, with the minimum information needed. You add slot types for the intent and then configure the intent later.

**To create an intent**

1. In the Amazon Lex console, choose the plus sign (\$1) next to **Intents**, and then choose **Create new intent**.

1. In the **Create intent** dialog box, type the name of the intent (`OrderPizza`), and then choose **Add**.

The console sends a request to Amazon Lex to create the `OrderPizza` intent. In this example you create slots for the intent after you create slot types.

## Next Step


[Create Slot Types](gs2-create-bot-slot-types.md)

# Create Slot Types


Create the slot types, or parameter values, that the `OrderPizza` intent uses.

**To create slot types**

1. <a name="slotTypeStart"></a>In the left menu, choose the plus sign (\$1) next to **Slot types**.

1. In the **Add slot type** dialog box, add the following: 
   + **Slot type name** – Crusts
   + **Description** – Available crusts
   + Choose **Restrict to Slot values and Synonyms**
   + **Value** – Type **thick**. Press tab and in the **Synonym** field type **stuffed**. Choose the plus sign (\$1). Type **thin** and then choose the plus sign (\$1) again.

   The dialog should look like the following image:  
![\[The edit slot type dialog box.\]](http://docs.aws.amazon.com/lex/latest/dg/images/gs1-25a.png)

1. Choose **Add slot to intent**.

1. <a name="slotTypeFinish"></a>On the **Intent** page, choose **Required**. Change the name of the slot from **slotOne** to **crust**. Change the prompt to **What kind of crust would you like?**

1. Repeat [Step 1](#slotTypeStart) through [Step 4](#slotTypeFinish) using the values in the following table:    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/lex/latest/dg/gs2-create-bot-slot-types.html)

## Next Step


[Configure the Intent](gs2-create-bot-configure-intent.md)

# Configure the Intent


Configure the `OrderPizza` intent to fulfill a user's request to order a pizza.

**To configure an intent**
+ On the **OrderPizza** configuration page, configure the intent as follows:
  + **Sample utterances** – Type the following strings. The curly braces \$1\$1 enclose slot names.
    + I want to order pizza please 
    + I want to order a pizza
    + I want to order a \$1pizzaKind\$1 pizza
    + I want to order a \$1size\$1 \$1pizzaKind\$1 pizza 
    + I want a \$1size\$1 \$1crust\$1 crust \$1pizzaKind\$1 pizza
    + Can I get a pizza please
    + Can I get a \$1pizzaKind\$1 pizza
    + Can I get a \$1size\$1 \$1pizzaKind\$1 pizza
  + **Lambda initialization and validation** – Leave the default setting.
  + **Confirmation prompt** – Leave the default setting.
  + **Fulfillment** – Perform the following tasks:
    + Choose **AWS Lambda function**.
    + Choose **PizzaOrderProcessor**. 
    + If the **Add permission to Lambda function** dialog box is shown, choose **OK** to give the `OrderPizza` intent permission to call the `PizzaOrderProcessor` Lambda function.
    +  Leave **None** selected.

  The intent should look like the following:  
![\[The intent editor.\]](http://docs.aws.amazon.com/lex/latest/dg/images/gs1-70c.png)

## Next Step


[Configure the Bot](gs2-create-bot-configure-bot.md)

# Configure the Bot


Configure error handling for the `PizzaOrderingBot` bot.

1. Navigate to the `PizzaOrderingBot` bot. Choose **Editor**. and then choose **Error Handling**, as in the following image:  
![\[\]](http://docs.aws.amazon.com/lex/latest/dg/images/gs1-80.png)

1. Use the **Editor** tab to configure bot error handling.
   + Information you provide in **Clarification Prompts** maps to the bot's [clarificationPrompt](https://docs.aws.amazon.com/lex/latest/dg/API_PutBot.html#lex-PutBot-request-clarificationPrompt) configuration. 

     When Amazon Lex can't determine the user intent, the service returns a response with this message. 
   + Information that you provide in the **Hang-up** phrase maps to the bot's [abortStatement](https://docs.aws.amazon.com/lex/latest/dg/API_PutBot.html#lex-PutBot-request-abortStatement) configuration. 

     If the service can't determine the user's intent after a set number of consecutive requests, Amazon Lex returns a response with this message.

   Leave the defaults.

## Next Step


[Step 3: Build and Test the Bot](gs2-build-and-test.md)

# Step 3: Build and Test the Bot


Make sure the bot works, by building and testing it. 

**To build and test the bot**

1. To build the `PizzaOrderingBot` bot, choose **Build**. 

   Amazon Lex builds a machine learning model for the bot. When you test the bot, the console uses the runtime API to send the user input back to Amazon Lex. Amazon Lex then uses the machine learning model to interpret the user input. 

   It can take some time to complete the build. 

1. To test the bot, in the **Test Bot** window, start communicating with your Amazon Lex bot. 
   + For example, you might say or type the following:  
![\[\]](http://docs.aws.amazon.com/lex/latest/dg/images/gs1-110.png)
   + Use the sample utterances that you configured in the `OrderPizza` intent to test the bot. For example, the following is one of the sample utterances that you configured for the `PizzaOrder` intent: 

     ```
     I want a {size} {crust} crust {pizzaKind} pizza
     ```

     To test it, type the following:

     ```
     I want a large thin crust cheese pizza
     ```

   When you type "I want to order a pizza," Amazon Lex detects the intent (`OrderPizza`). Then, Amazon Lex asks for slot information.

   After you provide all of the slot information, Amazon Lex invokes the Lambda function that you configured for the intent.

   The Lambda function returns a message ("Okay, I have ordered your ...") to Amazon Lex, which Amazon Lex returns to you..

## Inspecting the Response


Underneath the chat window is a pane that enables you to inspect the response from Amazon Lex. The pane provides comprehensive information about the state of your bot that changes as you interact with your bot. The contents of the panes show you the current state of the operation.
+ **Dialog State** – The current state of the conversation with the user. It can be `ElicitIntent`, `ElicitSlot`, `ConfirmIntent` or `Fulfilled`. 

   
+ **Summary** – Shows a simplified view of the dialog that shows the slot values for the intent being fulfilled so that you can keep track of the information flow. It shows the intent name, the number of slots and the number of slots filled, and a list of all of the slots and their associated values. See the following image:  
![\[The Amazon Lex console's response summary inspection pane.\]](http://docs.aws.amazon.com/lex/latest/dg/images/gs1-115.png)
+ **Detail** – Shows the raw JSON response from the chatbot to give you a deeper view into the bot interaction and the current state of the dialog as you test and debug your chatbot. If you type in the chat window, the inspection pane shows the JSON response from the [PostText](API_runtime_PostText.md) operation. If you speak to the chat window, the inspection pane shows the response headers from the [PostContent](API_runtime_PostContent.md) operation. See the following image:  
![\[The console's response inspection pane.\]](http://docs.aws.amazon.com/lex/latest/dg/images/gs1-116.png)

## Next Step


[Step 4 (Optional): Clean up](gs2-clean-up.md)

# Step 4 (Optional): Clean up


Delete the resources that you created and clean up your account to avoid incurring more charges for the resources you created.

You can delete only resources that are not in use. For example, you cannot delete a slot type that is referenced by an intent. You cannot delete an intent that is referenced by a bot.

Delete resources in the following order:
+ Delete bots to free up intent resources.
+ Delete intents to free up slot type resources.
+ Delete slot types last.



**To clean up your account**

1. Sign in to the AWS Management Console and open the Amazon Lex console at [https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/).

1. From the list of bots, choose **PizzaOrderingBot**.

1. To delete the bot, choose **Delete**, and then choose **Continue**.

1. In the left pane, choose **Intents**.

1. In the list of intents, choose **OrderPizza**.

1. To delete the intent, choose **Delete**, and then choose **Continue**.

1. In the left menu, choose **Slot types**.

1. <a name="chooseSlots"></a>In the list of slot types, choose **Crusts**.

1. <a name="deleteSlots"></a>To delete the slot type, choose **Delete**, and then choose **Continue**.

1. Repeat [Step 8](#chooseSlots) and [Step 9](#deleteSlots) for the `Sizes` and `PizzaKind` slot types.

You have removed all of the resources that you created and cleaned up your account.

## Next Steps

+ [Publish a Version and Create an Alias](https://docs.aws.amazon.com/lex/latest/dg/gettingstarted-ex3.html)
+ [Create an Amazon Lex bot with the AWS Command Line Interface](https://docs.aws.amazon.com/lex/latest/dg/gs-cli.html)