Event handler is a Powertools for AWS feature that processes an event, runs data parsing and validation, routes the request to a specific function, and returns a response to the caller in the proper format.
Function details consist of a list of parameters, defined by their name, data type, and whether or not they are required. The agent uses these configurations to determine what information it needs to elicit from the user.
Action group is a collection of two resources where you define the actions that the agent should carry out: an OpenAPI schema to define the APIs that the agent can invoke to carry out its tasks, and a Lambda function to execute those actions.
Large Language Models (LLM) are very large deep learning models that are pre-trained on vast amounts of data, capable of extracting meanings from a sequence of text and understanding the relationship between words and phrases within that text.
Amazon Bedrock Agent is an Amazon Bedrock feature to build and deploy conversational agents that can interact with your customers using Large Language Models (LLM) and AWS Lambda functions.
You must create an Amazon Bedrock Agent with at least one action group. Each action group can contain up to 5 tools, which in turn need to match the ones defined in your Lambda function. Bedrock must have permission to invoke your Lambda function.
AWSTemplateFormatVersion:'2010-09-09'Transform:AWS::Serverless-2016-10-31Globals:Function:Timeout:30MemorySize:256Runtime:nodejs22.xResources:HelloWorldFunction:Type:AWS::Serverless::FunctionProperties:Handler:index.handlerCodeUri:hello_worldAirlineAgentRole:Type:AWS::IAM::RoleProperties:RoleName:!Sub'${AWS::StackName}-AirlineAgentRole'Description:'RoleforBedrockAirlineagent'AssumeRolePolicyDocument:Version:'2012-10-17'Statement:-Effect:AllowPrincipal:Service:bedrock.amazonaws.comAction:sts:AssumeRolePolicies:-PolicyName:bedrockPolicyDocument:Version:'2012-10-17'Statement:-Effect:AllowAction:'bedrock:*'Resource:-!Sub'arn:aws:bedrock:us-*::foundation-model/*'-!Sub'arn:aws:bedrock:us-*:*:inference-profile/*'BedrockAgentInvokePermission:Type:AWS::Lambda::PermissionProperties:FunctionName:!RefHelloWorldFunctionAction:lambda:InvokeFunctionPrincipal:bedrock.amazonaws.comSourceAccount:!Ref'AWS::AccountId'SourceArn:!Sub'arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/${AirlineAgent}'# Bedrock AgentAirlineAgent:Type:AWS::Bedrock::AgentProperties:AgentName:AirlineAgentDescription:'AsimpleAirlineagent'FoundationModel:!Sub'arn:aws:bedrock:us-west-2:${AWS::AccountId}:inference-profile/us.amazon.nova-pro-v1:0'Instruction:|You are an airport traffic control agent. You will be given a city name and you will return the airport code for that city.AgentResourceRoleArn:!GetAttAirlineAgentRole.ArnAutoPrepare:trueActionGroups:-ActionGroupName:AirlineActionGroupActionGroupExecutor:Lambda:!GetAttAirlineAgentFunction.ArnFunctionSchema:Functions:-Name:getAirportCodeForCityDescription:'Gettheairportcodeforagivencity'Parameters:city:Type:stringDescription:'Thenameofthecitytogettheairportcodefor'Required:true
import{Arn,RemovalPolicy,Stack,typeStackProps}from'aws-cdk-lib';import{CfnAgent}from'aws-cdk-lib/aws-bedrock';import{PolicyDocument,PolicyStatement,Role,ServicePrincipal,}from'aws-cdk-lib/aws-iam';import{Runtime}from'aws-cdk-lib/aws-lambda';import{NodejsFunction,OutputFormat}from'aws-cdk-lib/aws-lambda-nodejs';import{LogGroup,RetentionDays}from'aws-cdk-lib/aws-logs';importtype{Construct}from'constructs';exportclassBedrockAgentsStackextendsStack{constructor(scope:Construct,id:string,props?:StackProps){super(scope,id,props);constfnName='BedrockAgentsFn';constlogGroup=newLogGroup(this,'AirlineAgentLogGroup',{logGroupName:`/aws/lambda/${fnName}`,removalPolicy:RemovalPolicy.DESTROY,retention:RetentionDays.ONE_DAY,});constfn=newNodejsFunction(this,'AirlineAgentFunction',{functionName:fnName,logGroup,runtime:Runtime.NODEJS_22_X,entry:'./src/index.ts',handler:'handler',bundling:{minify:true,mainFields:['module','main'],sourceMap:true,format:OutputFormat.ESM,},});constagentRole=newRole(this,'AirlineAgentRole',{assumedBy:newServicePrincipal('bedrock.amazonaws.com'),description:'Role for Bedrock Airline agent',inlinePolicies:{bedrock:newPolicyDocument({statements:[newPolicyStatement({actions:['bedrock:*'],resources:[Arn.format({service:'bedrock',resource:'foundation-model/*',region:'us-*',account:'',},Stack.of(this)),Arn.format({service:'bedrock',resource:'inference-profile/*',region:'us-*',account:'*',},Stack.of(this)),],}),],}),},});constagent=newCfnAgent(this,'AirlineAgent',{agentName:'AirlineAgent',actionGroups:[{actionGroupName:'AirlineActionGroup',actionGroupExecutor:{lambda:fn.functionArn,},functionSchema:{functions:[{name:'getAirportCodeForCity',description:'Get the airport code for a given city',parameters:{city:{type:'string',description:'The name of the city to get the airport code for',required:true,},},},],},},],agentResourceRoleArn:agentRole.roleArn,autoPrepare:true,description:'A simple Airline agent',foundationModel:`arn:aws:bedrock:us-west-2:${Stack.of(this).account}:inference-profile/us.amazon.nova-pro-v1:0`,instruction:'You are an airport traffic control agent. You will be given a city name and you will return the airport code for that city.',});fn.addPermission('BedrockAgentInvokePermission',{principal:newServicePrincipal('bedrock.amazonaws.com'),action:'lambda:InvokeFunction',sourceAccount:this.account,sourceArn:`arn:aws:bedrock:${this.region}:${this.account}:agent/${agent.attrAgentId}`,});}}
Use the BedrockAgentFunctionResolver to register your tools and handle the requests to your Lambda function. The resolver will automatically parse the request, route it to the appropriate function, and return a well-formed response that includes the tool's output and any existing session attributes.
When passing the tool parameters to your handler, we will automatically cast them to the appropriate type based on the type field defined in the action group. This means you can use native JavaScript types like string, number, boolean without worrying about parsing them yourself.
Currently, we don't support parsing array types, so you will receive them as strings.
1 2 3 4 5 6 7 8 91011121314151617181920
import{BedrockAgentFunctionResolver}from'@aws-lambda-powertools/event-handler/bedrock-agent';importtype{Context}from'aws-lambda';constapp=newBedrockAgentFunctionResolver();app.tool<{city:string}>(async({city})=>{return{city,airportCode:'XYZ',};},{name:'getAirportCodeForCity',description:'Get the airport code for a given city',// (1)!});exportconsthandler=async(event:unknown,context:Context)=>app.resolve(event,context);
The description field is optional, but highly recommended in the action group definition so that the LLM can understand what the tool does and how to use it.
By default, we will handle errors gracefully and return a well-formed response to the agent so that it can continue the conversation with the user.
When an error occurs, we send back an error message in the response body that includes the error type and message. The agent will then use this information to let the user know that something went wrong.
If you want to handle errors differently, you can return a BedrockFunctionResponse with a custom body and responseState set to FAILURE. This is useful when you want to abort the conversation.
Tip
You can use the same technique to reprompt the user for missing information or for them to correct their input. Just return a BedrockFunctionResponse with a custom message and responseState set to REPROMPT.
import{BedrockAgentFunctionResolver,BedrockFunctionResponse,}from'@aws-lambda-powertools/event-handler/bedrock-agent';importtype{Context}from'aws-lambda';constapp=newBedrockAgentFunctionResolver();app.tool<{city:string}>(async({city},{event})=>{try{thrownewError('Simulated error for demonstration purposes');}catch(error){const{sessionAttributes,promptSessionAttributes,knowledgeBasesConfiguration,}=event;returnnewBedrockFunctionResponse({body:`An error occurred while fetching the airport code for ${city}`,responseState:'FAILURE',sessionAttributes,promptSessionAttributes,knowledgeBasesConfiguration,});}},{name:'getAirportCodeForCity',description:'Get the airport code for a given city',});exportconsthandler=async(event:unknown,context:Context)=>app.resolve(event,context);
You can access to the original Lambda event or context for additional information. These are passed to the handler function as optional arguments.
1 2 3 4 5 6 7 8 91011121314151617181920212223
import{BedrockAgentFunctionResolver}from'@aws-lambda-powertools/event-handler/bedrock-agent';importtype{Context}from'aws-lambda';constapp=newBedrockAgentFunctionResolver();app.tool<{city:string}>(async({city},{event,context})=>{const{sessionAttributes}=event;sessionAttributes.requestId=context.awsRequestId;return{city,airportCode:'XYZ',// Simulated airport code for the city};},{name:'getAirportCodeForCity',description:'Get the airport code for a given city',});exportconsthandler=async(event:unknown,context:Context)=>app.resolve(event,context);
When Bedrock Agents invoke your Lambda function, it can pass session attributes that you can use to store information across multiple interactions with the user. You can access these attributes in your handler function and modify them as needed.
import{BedrockAgentFunctionResolver,BedrockFunctionResponse,}from'@aws-lambda-powertools/event-handler/bedrock-agent';importtype{Context}from'aws-lambda';constapp=newBedrockAgentFunctionResolver();app.tool<{city:string}>(async({city},{event})=>{const{sessionAttributes,promptSessionAttributes,knowledgeBasesConfiguration,}=event;// your logic to fetch airport code for the cityreturnnewBedrockFunctionResponse({body:JSON.stringify({city,airportCode:'XYZ',}),sessionAttributes:{...sessionAttributes,isCommercialAirport:true,},promptSessionAttributes,knowledgeBasesConfiguration,});},{name:'getAirportCodeForCity',description:'Get the airport code for a given city',});exportconsthandler=async(event:unknown,context:Context)=>app.resolve(event,context);
By default, the BedrockAgentFunctionResolver uses the global console logger and emits only warnings and errors.
You can change this behavior by passing a custom logger instance to the BedrockAgentFunctionResolver constructor and setting its log level. Alternatively, you can also enable Lambda Advanced Logging Controls and setting the log level to DEBUG.
When debug logging is enabled, the resolver will emit logs that show the underlying handler registration and the routing process. This is useful for understanding how the agent resolves the tools and routes the requests.
For example, when using the Powertools for AWS Lambda logger, you can set the LOG_LEVEL to DEBUG in your environment variables or at the logger level and pass the logger instance to the BedrockAgentFunctionResolver constructor to enable debug logging.
1 2 3 4 5 6 7 8 910111213141516171819202122232425
import{BedrockAgentFunctionResolver}from'@aws-lambda-powertools/event-handler/bedrock-agent';import{Logger}from'@aws-lambda-powertools/logger';importtype{Context}from'aws-lambda';constlogger=newLogger({serviceName:'serverlessAirline',logLevel:'DEBUG',});constapp=newBedrockAgentFunctionResolver({logger});app.tool<{city:string}>(async({city})=>{return{city,airportCode:'XYZ',// Simulated airport code for the city};},{name:'getAirportCodeForCity',description:'Get the airport code for a given city',});exportconsthandler=async(event:unknown,context:Context)=>app.resolve(event,context);