Using AWS Lambda with Amazon Lex - AWS Lambda

Using AWS Lambda with Amazon Lex

You can use Amazon Lex to integrate a conversation bot into your application. The Amazon Lex bot provides a conversational interface with your users. Amazon Lex provides prebuilt integration with Lambda, which enables you to use a Lambda function with your Amazon Lex bot.

When you configure an Amazon Lex bot, you can specify a Lambda function to perform validation, fulfillment, or both. For validation, Amazon Lex invokes the Lambda function after each response from the user. The Lambda function can validate the response and provide corrective feedback to the user, if necessary. For fulfillment, Amazon Lex invokes the Lambda function to fulfill the user request after the bot successfully collects all of the required information and receives confirmation from the user.

You can manage the concurrency of your Lambda function to control the maximum number of simultaneous bot conversations that you serve. The Amazon Lex API returns an HTTP 429 status code (Too Many Requests) if the function is at maximum concurrency.

The API returns an HTTP 424 status code (Dependency Failed Exception) if the Lambda function throws an exception.

The Amazon Lex bot invokes your Lambda function synchronously. The event parameter contains information about the bot and the value of each slot in the dialog. For definitions of the event and response fields, see Lambda event and response format in the Amazon Lex Developer Guide. The invocationSource parameter in the Amazon Lex message event indicates whether the Lambda function should validate the inputs (DialogCodeHook) or fulfill the intent (FulfillmentCodeHook).

For an example tutorial that shows how to use Lambda with Amazon Lex, see Exercise 1: Create Amazon Lex bot using a blueprint in the Amazon Lex Developer Guide.

Roles and permissions

You need to configure a service-linked role as your function's execution role. Amazon Lex defines the service-linked role with predefined permissions. When you create an Amazon Lex bot using the console, the service-linked role is created automatically. To create a service-linked role with the AWS CLI, use the create-service-linked-role command.

aws iam create-service-linked-role --aws-service-name

This command creates the following role.

{ "Role": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": "" } } ] }, "RoleName": "AWSServiceRoleForLexBots", "Path": "/aws-service-role/", "Arn": "arn:aws:iam::account-id:role/aws-service-role/" }

If your Lambda function uses other AWS services, you need to add the corresponding permissions to the service-linked role.

You use a resource-based permissions policy to allow the Amazon Lex intent to invoke your Lambda function. If you use the Amazon Lex console, the permissions policy is created automatically. From the AWS CLI, use the Lambda add-permission command to set the permission. The following example sets permission for the OrderFlowers intent.

aws lambda add-permission \ --function-name OrderFlowersCodeHook \ --statement-id LexGettingStarted-OrderFlowersBot \ --action lambda:InvokeFunction \ --principal \ --source-arn "arn:aws:lex:us-east-1:123456789012 ID:intent:OrderFlowers:*"