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 lex.amazonaws.com

This command creates the following role.

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

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 bot 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.

For Amazon Lex V2, run the following command. In the source ARN, replace us-east-1 with the AWS Region your Amazon Lex bot is in, and use your own AWS account number and bot alias.

aws lambda add-permission \ --function-name LexCodeHook \ --statement-id LexInvoke-MyBot \ --action lambda:InvokeFunction \ --principal lex.amazonaws.com \ --source-arn "arn:aws:lex:us-east-1:123456789012:bot-alias/MYBOT/MYBOTALIAS"

You can also use Amazon Lex V1 to invoke a Lambda function. For Amazon Lex V1, run the following command. In the source ARN , replace us-east-1 with the AWS Region your Amazon Lex intent is in and use your own AWS account number and intent name.

aws lambda add-permission \ --function-name LexCodeHook \ --statement-id LexInvoke-MyIntent \ --action lambda:InvokeFunction \ --principal lex.amazonaws.com \ --source-arn "arn:aws:lex:us-east-1:123456789012 ID:intent:MYINTENT:*"

Note that Amazon Lex V1 is no longer maintained. We recommend that you use Amazon Lex V2.