Amazon Bedrock Converse API with Java AWS Lambda Function
The Converse API provides a unified experience with Amazon Bedrock models, eliminating the need for model-specific implementations. This tutorial will guide you in creating a Java AWS Lambda function that interacts with Amazon Bedrock using the Converse API.
Kaustav
Amazon Employee
Published Jun 4, 2024
Last Modified Jun 12, 2024
The Converse API provides a consistent experience that works with Amazon Bedrock models, removing the need for developers to manage any model-specific implementation. With this API, you can write a code once and use it seamlessly with different models on Amazon Bedrock.
In this tutorial, we will walk through the steps to create a Java AWS Lambda function that interacts with the Amazon Bedrock Converse API. This function will be able to send and receive messages to and from an Amazon Bedrock model, such as the Claude Haiku model. We will cover the following steps:
- Setting up the Project
- Writing the Lambda Function Code
- Packaging the Java Application
- Deploying the Lambda Function
- Setting up Environment Variables with the Model Id to use
- Testing the Lambda Function
Before you begin, ensure you have the following:
- Basic knowledge of Java and AWS Lambda
- AWS account with necessary permissions
- Java Development Kit (JDK) 11 or later installed
- Maven configured
First, create a new Maven project. You can use your favorite IDE or create it manually.
pom.xml:
Create a Java class for your Lambda.
BedrockConverseAPILambda.java:
In this code, we hardcode a
systemMessage
instructing the model to always respond in rhymes. You can move the systemMessage
to an environment variable if you want more flexibility. You can also alter the inference parameters above to obtain the optimal response from the model.Package your application into a JAR file using Maven. The
maven-shade-plugin
is used to create an uber JAR that contains all the dependencies required to run the Lambda function.This will create a shaded JAR file in the
target
directory.- Go to the AWS Lambda console.
- Click on “Create function”.
- Choose “Author from scratch”.
- Enter a function name, e.g.,
BedrockConverseFunction
. - Choose the runtime as Java 21.
- Click “Create function”.
- In the function’s configuration, go to the “Code” section.
- Click on “Upload from” and select “Upload a .zip or .jar file”.
- Upload the JAR file created in the previous step.
Set the handler to
BedrockConverseAPILambda::handleRequest
.- Go to the “Configuration” tab and then to “Permissions”.
- Click on the role name hyperlink for the Lambda.
- Attach the
AmazonBedrockFullAccess
policy to the role.
- In the Lambda function configuration, go to the “Configuration” tab.
- Select “Environment variables”.
- Add a new environment variable with the key
MODEL_ID
and the valueanthropic.claude-3-haiku-20240307-v1:0
. You can change theMODEL_ID
environment variable to test out different language models supported by Bedrock. Make sure you have access to the model you are specifying.
Note: You can find the list of models supporting the Converse API and a list of all model IDs in the documentation.
- In the Lambda console, go to the “Test” tab.
- Create a new test event with the following JSON:
- Save the test event and click “Test”.
- Check the response and logs to ensure the function is working correctly.
By following these steps, you have successfully created a Java AWS Lambda function that interacts with the Amazon Bedrock Converse API. This function can send and receive messages to and from an Amazon Bedrock model, allowing you to build conversational applications with ease. You can change the Model ID without worrying about the complexity to adjust for model-specific parameters.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.