If you are using Amazon Lex V2, refer to the Amazon Lex V2 guide instead.
If you are using Amazon Lex V1, we recommend upgrading your bots to Amazon Lex V2. We are no longer adding new features to V1 and strongly recommend using V2 for all new bots.
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.
-
Use the Lambda AddPermission function to enable the
OrderFlowers
intent to call the Lambda Invoke operation. -
Use the GetIntent operation to get the intent from Amazon Lex.
-
Update the intent to add the Lambda function.
-
Use the PutIntent operation to send the updated intent back to Amazon Lex.
-
Use the GetBot and PutBot operations to rebuild any bot that uses 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 .
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 functionLambda function ARN
in the context of intentintent 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: Intentintent name
already exists. If you are trying to updateintent name
you must specify the checksum.
This exercise uses the Lambda function from Exercise 1: Create an Amazon Lex Bot Using a Blueprint (Console). For instructions to create the Lambda function, see Step 3: Create a Lambda Function (Console).
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
-
In the AWS CLI, add the
InvokeFunction
permission for theOrderFlowers
intent:aws lambda add-permission \ --region
region
\ --function-name OrderFlowersCodeHook \ --statement-id LexGettingStarted-OrderFlowersBot \ --action lambda:InvokeFunction \ --principal lex.amazonaws.com \ --source-arn "arn:aws:lex:region
:account ID
:intent:OrderFlowers:*" --source-accountaccount ID
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\":{\"StringEquals\": {\"AWS:SourceAccount\": \"account ID
\"}, {\"AWS:SourceArn\": \"arn:aws:lex:region
:account ID
:intent:OrderFlowers:*\"}}}" } -
Get the intent from Amazon Lex. Amazon Lex sends the output to a file called
OrderFlowers-V3.json
.aws lex-models get-intent \ --region
region
\ --name OrderFlowers \ --intent-version "\$LATEST" > OrderFlowers-V3.json -
In a text editor, open the
OrderFlowers-V3.json
.-
Find and delete the
createdDate
,lastUpdatedDate
, andversion
fields. -
Update the
fulfillmentActivity
field :"fulfillmentActivity": { "type": "CodeHook", "codeHook": { "uri": "arn:aws:lambda:
region
:account ID
:function:OrderFlowersCodeHook", "messageVersion": "1.0" } } -
Save the file.
-
-
In the AWS CLI, send the updated intent to Amazon Lex:
aws lex-models put-intent \ --region
region
\ --name OrderFlowers \ --cli-input-json file://OrderFlowers-V3.json
Now that you have updated the intent, rebuild the bot.
To rebuild the OrderFlowersBot
bot
-
In the AWS CLI, get the definition of the
OrderFlowersBot
bot and save it to a file:aws lex-models get-bot \ --region
region
\ --name OrderFlowersBot \ --version-or-alias "\$LATEST" > OrderFlowersBot-V3.json -
In a text editor,open
OrderFlowersBot-V3.json
. Remove thecreatedDate
,lastUpdatedDate
,status
, andversion
fields. -
In the text editor, add the following line to the definition of the bot:
"processBehavior": "BUILD",
-
In the AWS CLI, build a new revision of the bot:
aws lex-models put-bot \ --region
region
\ --name OrderFlowersBot \ --cli-input-json file://OrderFlowersBot-V3.jsonThe 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)