service: claude-lex-bedrock-bot
provider:
name: aws
runtime: nodejs22.x
region: eu-west-2
environment:
KNOWLEDGE_BASE_ID: ${env:KNOWLEDGE_BASE_ID, 'M86AHPZ6CL'}
iam:
role:
statements:
- Effect: Allow
Action:
- bedrock:InvokeModel
- bedrock:Retrieve
- bedrock:RetrieveAndGenerate
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- lambda:InvokeFunction
- lex:*
Resource: "*"
functions:
LexBedrockHandler:
handler: handler.handler
name: claude-lex-bedrock-bot-dev-LexBedrockHandler
events:
- httpApi:
path: /chat
method: post
BotInputHandler:
handler: lexHandler.handler
name: lex-chat
environment:
BOT_ID: !Ref MyLexBot
BOT_ALIAS_ID: !GetAtt MyLexBotAlias.BotAliasId
LOCALE_ID: "en_US"
events:
- httpApi:
path: /bot
method: post
resources:
Resources:
MyLexBot:
Type: AWS::Lex::Bot
Properties:
Name: MyBot
DataPrivacy:
ChildDirected: false
IdleSessionTTLInSeconds: 300
RoleArn: !GetAtt LexBotRole.Arn
BotLocales:
- LocaleId: en_US
NluConfidenceThreshold: 0.40
Intents:
- Name: GreetingIntent
SampleUtterances:
- Utterance: "Hi"
- Utterance: "Hello"
- Utterance: "Good morning"
- Utterance: "Hey"
IntentClosingSetting:
ClosingResponse:
MessageGroupsList:
- Message:
PlainTextMessage:
Value: "Hello! How can I assist you today?"
- Name: FallbackIntent
ParentIntentSignature: AMAZON.FallbackIntent
FulfillmentCodeHook:
Enabled: true # Invoke Lambda for dynamic response
MyLexBotVersion:
Type: AWS::Lex::BotVersion
Properties:
BotId: !Ref MyLexBot
BotVersionLocaleSpecification:
- LocaleId: en_US
BotVersionLocaleDetails:
SourceBotVersion: "DRAFT"
DependsOn: MyLexBot
MyLexBotAlias:
Type: AWS::Lex::BotAlias
Properties:
BotAliasName: prod
BotId: !Ref MyLexBot
BotVersion: "1"
BotAliasLocaleSettings:
- LocaleId: en_US
BotAliasLocaleSetting:
Enabled: true
CodeHookSpecification:
LambdaCodeHook:
CodeHookInterfaceVersion: "1.0"
LambdaArn: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:claude-lex-bedrock-bot-dev-LexBedrockHandler"
DependsOn: MyLexBotVersion
LexBotRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lexv2.amazonaws
Action: sts:AssumeRole
Policies:
- PolicyName: LexBotAccessPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- lambda:InvokeFunction
- bedrock:InvokeModel
- bedrock:Retrieve
- bedrock:RetrieveAndGenerate
Resource: "*"
LexLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: claude-lex-bedrock-bot-dev-LexBedrockHandler
Principal: lexv2.amazonaws
SourceArn: !Sub "arn:aws:lex:${AWS::Region}:${AWS::AccountId}:bot-alias/${MyLexBot}/${MyLexBotAlias.BotAliasId}"
Outputs:
BotId:
Description: The ID of the Lex Bot
Value: !Ref MyLexBot
AliasId:
Description: The ID of the Lex Bot Alias
Value: !GetAtt MyLexBotAlias.BotAliasId
plugins:
- serverless-api-gateway-throttling
custom:
apiGatewayThrottling:
maxRequestsPerSecond: 10
maxConcurrentRequests: 5
package:
exclude:
- node_modules/**
- .git/**
- .gitignore
- test/**
- "*.md"
My use case is to create a bot using the YAML file. When a user asks questions related to my knowledge base, I need the bot to provide answers. I tried using the QnA intent in the AWS console, but it’s not available in CloudFormation. So, I created a Lambda function and added it as a fulfillment hook to the intent. However, my problem is that the user can ask anything about the intents, and I can’t predefine all possible utterances. How can I capture the user’s questions and pass them to the Lambda function?