

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 1: Create an Amazon Lex Bot (AWS CLI)
Exercise 1: Create a Bot

In general, when you create bots, you:

1. Create slot types to define the information that your bot will be working with.

1. Create intents that define the user actions that your bot supports. Use the custom slot types that you created earlier to define the slots, or parameters, that your intent requires.

1. Create a bot that uses the intents that you defined. 

In this exercise you create and test a new Amazon Lex bot using the CLI. Use the JSON structures that we provide to create the bot. To run the commands in this exercise, you need to know the region where the commands will be run. For a list of regions, see [Model Building Quotas](gl-limits.md#gl-limits-model-building).

**Topics**
+ [

# Step 1: Create a Service-Linked Role (AWS CLI)
](gs-create-role.md)
+ [

# Step 2: Create a Custom Slot Type (AWS CLI)
](gs-create-flower-types.md)
+ [

# Step 3: Create an Intent (AWS CLI)
](gs-cli-create-order-flowers.md)
+ [

# Step 4: Create a Bot (AWS CLI)
](gs-cli-create-order-flowers-bot.md)
+ [

# Step 5: Test a Bot (AWS CLI)
](gs-create-test.md)

# Step 1: Create a Service-Linked Role (AWS CLI)
Step 1: Create a Service-Linked Role

Amazon Lex assumes AWS Identity and Access Management service-linked roles to call AWS services on behalf of your bots. The roles, which are in your account, are linked to Amazon Lex use cases and have predefined permissions. For more information, see [Using Service-Linked Roles for Amazon Lex](using-service-linked-roles.md).

If you've already created an Amazon Lex bot using the console, the service-linked role was created automatically. Skip to [Step 2: Create a Custom Slot Type (AWS CLI)](gs-create-flower-types.md). 

**To create a service-linked role (AWS CLI)**

1. In the AWS CLI, type the following command:

   ```
   aws iam create-service-linked-role --aws-service-name lex.amazonaws.com
   ```

1. Check the policy using the following command:

   ```
   aws iam get-role --role-name AWSServiceRoleForLexBots
   ```

   The response is:

## Next Step


[Step 2: Create a Custom Slot Type (AWS CLI)](gs-create-flower-types.md)

# Step 2: Create a Custom Slot Type (AWS CLI)
Step 2: Create a Custom Slot Type

Create a custom slot type with enumeration values for the flowers that can be ordered. You use this type in the next step when you create the `OrderFlowers` intent. A *slot type* defines the possible values for a slot, or parameter, of the intent.

To run the commands in this exercise, you need to know the region where the commands will be run. For a list of regions, see [Model Building Quotas](gl-limits.md#gl-limits-model-building).

**To create a custom slot type (AWS CLI)**

1. Create a text file named **FlowerTypes.json**. Copy the JSON code from [FlowerTypes.json](gs-cli-create-flower-types-json.md) into the text file.

1. Call the [PutSlotType](API_PutSlotType.md) operation using the AWS CLI to create the slot type. The example is formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\$1) Unix continuation character at the end of each line with a caret (^).

   ```
   aws lex-models put-slot-type \
       --region region \
       --name FlowerTypes \
       --cli-input-json file://FlowerTypes.json
   ```

   The response from the server is:

   ```
   {
       "enumerationValues": [
           {
               "value": "tulips"
           }, 
           {
               "value": "lilies"
           }, 
           {
               "value": "roses"
           }
       ], 
       "name": "FlowerTypes", 
       "checksum": "checksum", 
       "version": "$LATEST", 
       "lastUpdatedDate": timestamp, 
       "createdDate": timestamp, 
       "description": "Types of flowers to pick up"
   }
   ```

## Next Step


[Step 3: Create an Intent (AWS CLI)](gs-cli-create-order-flowers.md)

# FlowerTypes.json


The following code is the JSON data required to create the `FlowerTypes` custom slot type:

```
{
    "enumerationValues": [
        {
            "value": "tulips"
        },
        {
            "value": "lilies"
        },
        {
            "value": "roses"
        }
    ],
    "name": "FlowerTypes",
    "description": "Types of flowers to pick up"
}
```

# Step 3: Create an Intent (AWS CLI)
Step 3: Create an Intent

Create an intent for the `OrderFlowersBot` bot and provide three slots, or parameters. The slots allow the bot to fulfill the intent:
+ `FlowerType` is a custom slot type that specifies which types of flowers can be ordered.
+ `AMAZON.DATE` and `AMAZON.TIME` are built-in slot types used for getting the date and time to deliver the flowers from the user.

To run the commands in this exercise, you need to know the region where the commands will be run. For a list of regions, see [Model Building Quotas](gl-limits.md#gl-limits-model-building).

**To create the `OrderFlowers` intent (AWS CLI)**

1. Create a text file named **OrderFlowers.json**. Copy the JSON code from [OrderFlowers.json](gs-cli-create-order-flowers-json.md) into the text file.

1. In the AWS CLI, call the [PutIntent](API_PutIntent.md) operation to create the intent. The example is formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\$1) Unix continuation character at the end of each line with a caret (^).

   ```
   aws lex-models put-intent \
      --region region \
      --name OrderFlowers \
      --cli-input-json file://OrderFlowers.json
   ```

   The server responds with the following:

## Next Step


[Step 4: Create a Bot (AWS CLI)](gs-cli-create-order-flowers-bot.md)

# OrderFlowers.json


The following code is the JSON data required to create the `OrderFlowers` intent:

```
{
    "confirmationPrompt": {
        "maxAttempts": 2,
        "messages": [
            {
                "content": "Okay, your {FlowerType} will be ready for pickup by {PickupTime} on {PickupDate}.  Does this sound okay?",
                "contentType": "PlainText"
            }
        ]
    },
    "name": "OrderFlowers",
    "rejectionStatement": {
        "messages": [
            {
                "content": "Okay, I will not place your order.",
                "contentType": "PlainText"
            }
        ]
    },
    "sampleUtterances": [
        "I would like to pick up flowers",
        "I would like to order some flowers"
    ],
    "slots": [
        {
            "slotType": "FlowerTypes",
            "name": "FlowerType",
            "slotConstraint": "Required",
            "valueElicitationPrompt": {
                "maxAttempts": 2,
                "messages": [
                    {
                        "content": "What type of flowers would you like to order?",
                        "contentType": "PlainText"
                    }
                ]
            },
            "priority": 1,
            "slotTypeVersion": "$LATEST",
            "sampleUtterances": [
                "I would like to order {FlowerType}"
            ],
            "description": "The type of flowers to pick up"
        },
        {
            "slotType": "AMAZON.DATE",
            "name": "PickupDate",
            "slotConstraint": "Required",
            "valueElicitationPrompt": {
                "maxAttempts": 2,
                "messages": [
                    {
                        "content": "What day do you want the {FlowerType} to be picked up?",
                        "contentType": "PlainText"
                    }
                ]
            },
            "priority": 2,
            "description": "The date to pick up the flowers"
        },
        {
            "slotType": "AMAZON.TIME",
            "name": "PickupTime",
            "slotConstraint": "Required",
            "valueElicitationPrompt": {
                "maxAttempts": 2,
                "messages": [
                    {
                        "content": "Pick up the {FlowerType} at what time on {PickupDate}?",
                        "contentType": "PlainText"
                    }
                ]
            },
            "priority": 3,
            "description": "The time to pick up the flowers"
        }
    ],
    "fulfillmentActivity": {
        "type": "ReturnIntent"
    },
    "description": "Intent to order a bouquet of flowers for pick up"
}
```

# Step 4: Create a Bot (AWS CLI)
Step 4: Create a Bot

The `OrderFlowersBot` bot has one intent, the `OrderFlowers` intent that you created in the previous step. To run the commands in this exercise, you need to know the region where the commands will be run. For a list of regions, see [Model Building Quotas](gl-limits.md#gl-limits-model-building).

**Note**  
The following AWS CLI example is formatted for Unix, Linux, and macOS. For Windows, change `"\$LATEST"` to `$LATEST`.

**To create the `OrderFlowersBot` bot (AWS CLI)**

1. Create a text file named **OrderFlowersBot.json**. Copy the JSON code from [OrderFlowersBot.json](gs-cli-create-order-flowers-bot-json.md) into the text file.

1. In the AWS CLI, call the [PutBot](API_PutBot.md) operation to create the bot. The example is formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\$1) Unix continuation character at the end of each line with a caret (^).

   ```
   aws lex-models put-bot \
       --region region \
       --name OrderFlowersBot \
       --cli-input-json file://OrderFlowersBot.json
   ```

   The response from the server follows. When you create or update bot, the `status` field is set to `BUILDING`. This indicates that the bot isn't ready to use. To determine when the bot is ready for use, use the [GetBot](API_GetBot.md) operation in the next step . 

   

1. To determine if your new bot is ready for use, run the following command. Repeat this command until the `status` field returns `READY`. The example is formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\$1) Unix continuation character at the end of each line with a caret (^).

   ```
   aws lex-models get-bot \
       --region region \
       --name OrderFlowersBot \
       --version-or-alias "\$LATEST"
   ```

   Look for the `status` field in the response:

   ```
   {
       "status": "READY", 
       
       ...
       
   }
   ```

## Next Step


[Step 5: Test a Bot (AWS CLI)](gs-create-test.md)

# OrderFlowersBot.json


The following code provides the JSON data required to build the `OrderFlowers` Amazon Lex bot:

```
{
    "intents": [
        {
            "intentVersion": "$LATEST",
            "intentName": "OrderFlowers"
        }
    ],
    "name": "OrderFlowersBot",
    "locale": "en-US",
    "abortStatement": {
        "messages": [
            {
                "content": "Sorry, I'm not able to assist at this time",
                "contentType": "PlainText"
            }
        ]
    },
    "clarificationPrompt": {
        "maxAttempts": 2,
        "messages": [
            {
                "content": "I didn't understand you, what would you like to do?",
                "contentType": "PlainText"
            }
        ]
    },
    "voiceId": "Salli",
    "childDirected": false,
    "idleSessionTTLInSeconds": 600,
    "description": "Bot to order flowers on the behalf of a user"
}
```

# Step 5: Test a Bot (AWS CLI)
Step 5: Test a Bot

To test the bot,you can use either a text-based or a speech-based test.

**Topics**
+ [

# Test the Bot Using Text Input (AWS CLI)
](gs-create-test-text.md)
+ [

# Test the Bot Using Speech Input (AWS CLI)
](gs-create-test-speech.md)

# Test the Bot Using Text Input (AWS CLI)


 To verify that the bot works correctly with text input, use the [PostText](API_runtime_PostText.md) operation. To run the commands in this exercise, you need to know the region where the commands will be run. For a list of regions, see [Runtime Service Quotas](gl-limits.md#gl-limits-runtime).

**Note**  
The following AWS CLI example is formatted for Unix, Linux, and macOS. For Windows, change `"\$LATEST"` to `$LATEST` and replace the backslash (\$1) continuation character at the end of each line with a caret (^).

**To use text to test the bot (AWS CLI)**

1. In the AWS CLI, start a conversation with the `OrderFlowersBot` bot. The example is formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\$1) Unix continuation character at the end of each line with a caret (^).

   ```
   aws lex-runtime post-text \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "i would like to order flowers"
   ```

   Amazon Lex recognizes the user's intent and starts a conversation by returning the following response:

   ```
   {
       "slotToElicit": "FlowerType", 
       "slots": {
           "PickupDate": null, 
           "PickupTime": null, 
           "FlowerType": null
       }, 
       "dialogState": "ElicitSlot", 
       "message": "What type of flowers would you like to order?", 
       "intentName": "OrderFlowers"
   }
   ```

1. Run the following commands to finish the conversation with the bot.

   ```
   aws lex-runtime post-text \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "roses"
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "tuesday"
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "10:00 a.m."
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "yes"
   ```

    After you confirm the order, Amazon Lex sends a fulfillment response to complete the conversation: 

   ```
   {
       "slots": {
           "PickupDate": "2017-05-16", 
           "PickupTime": "10:00", 
           "FlowerType": "roses"
       }, 
       "dialogState": "ReadyForFulfillment", 
       "intentName": "OrderFlowers"
   }
   ```

## Next Step


[Test the Bot Using Speech Input (AWS CLI)](gs-create-test-speech.md)

# Test the Bot Using Speech Input (AWS CLI)


To test the bot using audio files, use the [PostContent](API_runtime_PostContent.md) operation. You generate the audio files using Amazon Polly text-to-speech operations.

To run the commands in this exercise, you need to know the region the Amazon Lex and Amazon Polly commands will be run. For a list of regions for Amazon Lex, see [Runtime Service Quotas](gl-limits.md#gl-limits-runtime). For a list of regions for Amazon Polly see [AWS Regions and Endpoints ](https://docs.aws.amazon.com/general/latest/gr/rande.html#pol_region) in the *Amazon Web Services General Reference*.

**Note**  
The following AWS CLI example is formatted for Unix, Linux, and macOS. For Windows, change `"\$LATEST"` to `$LATEST` and replace the backslash (\$1) continuation character at the end of each line with a caret (^).

**To use a speech input to test the bot (AWS CLI)**

1. In the AWS CLI, create an audio file using Amazon Polly. The example is formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\$1) Unix continuation character at the end of each line with a caret (^).

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "i would like to order flowers" \
       --voice-id "Salli" \
       IntentSpeech.mpg
   ```

1. To send the audio file to Amazon Lex, run the following command. Amazon Lex saves the audio from the response in the specified output file. 

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream IntentSpeech.mpg \
       IntentOutputSpeech.mpg
   ```

   Amazon Lex responds with a request for the first slot. It saves the audio response in the specified output file.

   ```
   {
       "contentType": "audio/mpeg", 
       "slotToElicit": "FlowerType", 
       "dialogState": "ElicitSlot", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "i would like to order some flowers", 
       "slots": {
           "PickupDate": null, 
           "PickupTime": null, 
           "FlowerType": null
       }, 
       "message": "What type of flowers would you like to order?"
   }
   ```

1. To order roses, create the following audio file and send it to Amazon Lex :

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "roses" \
       --voice-id "Salli" \ 
       FlowerTypeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream FlowerTypeSpeech.mpg \
       FlowerTypeOutputSpeech.mpg
   ```

1. To set the delivery date, create the following audio file and send it to Amazon Lex:

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "tuesday" \
       --voice-id "Salli" \ 
       DateSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream DateSpeech.mpg \
       DateOutputSpeech.mpg
   ```

1. To set the delivery time, create the following audio file and send it to Amazon Lex:

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "10:00 a.m." \
       --voice-id "Salli" \
       TimeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream TimeSpeech.mpg \
       TimeOutputSpeech.mpg
   ```

1. To confirm the delivery, create the following audio file and send it to Amazon Lex:

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "yes" \
       --voice-id "Salli" \
       ConfirmSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream ConfirmSpeech.mpg \
       ConfirmOutputSpeech.mpg
   ```

   After you confirm the delivery, Amazon Lex sends a response that confirms fulfillment of the intent: 

   ```
   {
       "contentType": "text/plain;charset=utf-8", 
       "dialogState": "ReadyForFulfillment", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "yes", 
       "slots": {
           "PickupDate": "2017-05-16", 
           "PickupTime": "10:00", 
           "FlowerType": "roses"
       }
   }
   ```

## Next Step


[Exercise 2: Add a New Utterance (AWS CLI)](gs-cli-update-utterance.md)