AWS Logo
Menu

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:
  1. Setting up the Project
  2. Writing the Lambda Function Code
  3. Packaging the Java Application
  4. Deploying the Lambda Function
  5. Setting up Environment Variables with the Model Id to use
  6. Testing the Lambda Function

Prerequisites

Before you begin, ensure you have the following:
  1. Basic knowledge of Java and AWS Lambda
  2. AWS account with necessary permissions
  3. Java Development Kit (JDK) 11 or later installed
  4. Maven configured

1. Setting Up the Project

First, create a new Maven project. You can use your favorite IDE or create it manually.
pom.xml:

2. Writing the Lambda Function Code

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.

3. Packaging the Java Application

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.

4. Deploying the Lambda Function

Create the Lambda Function:

  1. Go to the AWS Lambda console.
  2. Click on “Create function”.
  3. Choose “Author from scratch”.
  4. Enter a function name, e.g., BedrockConverseFunction.
  5. Choose the runtime as Java 21.
  6. Click “Create function”.

Upload the JAR File:

  1. In the function’s configuration, go to the “Code” section.
  2. Click on “Upload from” and select “Upload a .zip or .jar file”.
  3. Upload the JAR file created in the previous step.

Set the Handler:

Set the handler to BedrockConverseAPILambda::handleRequest.

Add Necessary Permissions:

  1. Go to the “Configuration” tab and then to “Permissions”.
  2. Click on the role name hyperlink for the Lambda.
  3. Attach the AmazonBedrockFullAccess policy to the role.

5. Setting Up Environment Variables

Add Environment Variable:

  1. In the Lambda function configuration, go to the “Configuration” tab.
  2. Select “Environment variables”.
  3. Add a new environment variable with the key MODEL_ID and the value anthropic.claude-3-haiku-20240307-v1:0. You can change the MODEL_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.

6. Testing the Lambda Function

Create a Test Event:

  1. In the Lambda console, go to the “Test” tab.
  2. Create a new test event with the following JSON:

Invoke the lambda with the Test Event:

  1. Save the test event and click “Test”.
  2. Check the response and logs to ensure the function is working correctly.

Conclusion

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.

Comments