Menu
Amazon Lex
Developer Guide

Exercise 3: Add a Lambda Function (AWS CLI)

Add a Lambda function that validates user input and fulfills the user's intent to the bot.

Adding a Lambda expression is a five-step process.

  1. Use the Lambda AddPermission function to enable the OrderFlowers intent to call the Lambda Invoke operation.

  2. Use the GetIntent operation to get the intent from Amazon Lex.

  3. Update the intent to add the Lambda function.

  4. Use the PutIntent operation to send the updated intent back to Amazon Lex.

  5. Use the GetBot and GetBot operations to rebuild any bot that uses the intent.

If you add a Lambda function to an intent before you add the InvokeFunction permission, you get the following error message:

            An error occurred (BadRequestException) when calling the 
            PutIntent operation: Lex is unable to access the Lambda 
            function Lambda function ARN in the context of intent 
            intent ARN.  Please check the resource-based policy on 
            the function.
        

The response from the GetIntent operation contains a field called checksum that identifies a specific revision of the intent. When you use the PutIntent operation to update an intent, you must provide the checksum value. If you don't, you get the following error message:

            An error occurred (PreconditionFailedException) when calling 
            the PutIntent operation: Intent intent name already exists. 
            If you are trying to update intent name you must specify the 
            checksum.
        

This exercise uses the Lambda function from Example Bot: ScheduleAppointment. For instructions to create the Lambda function, see Step 2: Create a Lambda Function.

Note

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

To add a Lambda function to an intent

  1. In the AWS CLI, add the InvokeFunction permission for the OrderFlowers intent:

    Copy
    aws lambda add-permission --function-name OrderFlowersCodeHook --statement-id LexGettingStarted-OrderFlowersBot --action lambda:InvokeFunction --principal lex.amazonaws.com --source-arn "arn:aws:lex:region:account ID:intent:OrderFlowers:*"

    Lambda sends the following response:

    {
        "Statement": "{\"Sid\":\"LexGettingStarted-OrderFlowersBot\",
          \"Resource\":\"arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook\",
          \"Effect\":\"Allow\",
          \"Principal\":{\"Service\":\"lex.amazonaws.com\"},
          \"Action\":[\"lambda:InvokeFunction\"],
          \"Condition\":{\"ArnLike\":
            {\"AWS:SourceArn\":
              \"arn:aws:lex:region:account ID:intent:OrderFlowers:*\"}}}"
    }
  2. Get the intent from Amazon Lex. Amazon Lex sends the output to a file called OrderFlowers-V3.json.

    Copy
    aws lex-models get-intent --name OrderFlowers --intent-version "\$LATEST" > OrderFlowers-V3.json
  3. In a text editor, open the OrderFlowers-V3.json.

    1. Find and delete the createdDate, lastUpdatedDate, and version fields.

    2. Update the fulfillmentActivity field :

      Copy
      "fulfillmentActivity": { "type": "CodeHook", "codeHook": { "uri": "arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook", "messageVersion": "1.0" },
    3. Save the file.

  4. In the AWS CLI, send the updated intent to Amazon Lex:

    Copy
    aws lex-models put-intent --name OrderFlowers --cli-input-json file://OrderFlowers-V3.json

Now that you have updated the intent, rebuild the bot.

To rebuild the OrderFlowersBot bot

  1. In the AWS CLI, get the definition of the OrderFlowersBot bot and save it to a file:

    Copy
    aws lex-models get-bot --name OrderFlowersBot --version-or-alias "\$LATEST" > OrderFlowersBot-V3.json
  2. In a text editor,open OrderFlowersBot-V3.json. Remove the createdDate, lastUpdatedDate, status, and version fields.

  3. In the text editor, add the following line to the definition of the bot:

    Copy
    "processBehavior": "BUILD",
  4. In the AWS CLI, build a new revision of the bot:

    Copy
    aws lex-models put-bot --name OrderFlowersBot --cli-input-json file://OrderFlowersBot-V3.json

    The response from the server is:

    {
        "status": "READY", 
        "intents": [
            {
                "intentVersion": "$LATEST", 
                "intentName": "OrderFlowers"
            }
        ], 
        "name": "OrderFlowersBot", 
        "locale": "en-US", 
        "checksum": "checksum", 
        "abortStatement": {
            "messages": [
                {
                    "content": "Sorry, I'm not able to assist at this time", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "version": "$LATEST", 
        "lastUpdatedDate": timestamp, 
        "createdDate": timestamp, 
        "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"
    }
    

Next Step

Exercise 4: Publish a Version (AWS CLI)

On this page: