Skip to content

Commit

Permalink
Merge pull request #1919 from kozub/kozub-feature-apigw-lambda-bedroc…
Browse files Browse the repository at this point in the history
…k-java

New serverless pattern: ApiGateway-Lambda-Bedrock using AWS SDK for Java
  • Loading branch information
ellisms authored Jan 4, 2024
2 parents ab27cd3 + 93de3ce commit 814035b
Show file tree
Hide file tree
Showing 15 changed files with 622 additions and 0 deletions.
92 changes: 92 additions & 0 deletions apigw-lambda-bedrock-cdk-java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Amazon API Gateway to AWS Lambda to Amazon Bedrock using AWS SDK for Java and CDK

This sample project deploys an Amazon API Gateway REST API with an AWS Lambda integration. The Lambda function is written in Java, calls the Amazon Bedrock API for Anthropic Claude-v2 model and returns a response containing the generated content.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-lambda-bedrock-cdk-java

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

- [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- `Anthropic Claude` fundamental model access requested (check [Prerequisite](#Prerequisite) to see details).
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
- [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- [Java 21](https://docs.aws.amazon.com/corretto/latest/corretto-21-ug/downloads-list.html)
- [Maven 3](https://maven.apache.org/)
- [AWS Cloud Development Kit](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) (AWS CDK)


## Prerequisite
This pattern uses `Anthropic Claude` fundamental model provided by Amazon Bedrock. It is required to request access to the model before starting using the pattern. Please refer to the link below for an instruction:
[Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```bash
git clone https://github.com/aws-samples/serverless-patterns
```
2. Change directory to the pattern directory:
```bash
cd apigw-lambda-bedrock-cdk-java/infrastructure
```

3. From the command line, configure AWS CDK:
```bash
cdk bootstrap ACCOUNT-NUMBER/REGION # e.g.
cdk bootstrap 1111111111/us-east-1
cdk bootstrap --profile test 1111111111/us-east-1
```
4. From the command line, use AWS CDK to deploy the AWS resources for the pattern as specified in the `lib/cdk-stack.ts` file:
```bash
deploy.sh
```

## How it works

This CDK project uses the Amazon Bedrock API for Anthropic Claude-v2 model to generate content based on given prompt. This is exposed through a serverless REST API. Please refer to the architecture diagram below:
![End to End Architecture](images/architecture.png)


Here's a breakdown of the steps:

1. **Amazon API Gateway**: Receives the HTTP POST request containing the prompt.

2. **AWS Lambda**: Triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using Bedrock SDK. It uses Anthropic Claude-v2 model and sets other required parameters to fixed values for simplicity.

3. **Amazon Bedrock**: Based on the given prompt, using Anthropic Claude-v2 model generates the content and returns the response to Lambda.

4. **Response**: Lambda processes the Bedrock output and sends it back to the user via the API Gateway.

## Testing

Test the deployed content generation API by providing a prompt. You can use [curl](https://curl.se/) to send a HTTP POST request to the API. Replace `BedrockRestApi` with the API URL in the CDK output, which has a name beginning with `ApiGatewayLambdaBedrockStack.LambdaBedrockAPIEncpoind` :

```bash
curl -d '{"prompt": "Please calculate with step-by-step explanation: 2+2*2"}' -H 'Content-Type: application/json' <BedrockRestApi>
```

The API returns a response with generated content. Your output may vary:

```
{
"response": " Okay, let's solve this step-by-step:\n2 + 2 * 2\nStep 1) Perform the multiplication first: 2 * 2 = 4\nStep 2) Now perform the addition: 2 + 4 = 6\n\nTherefore, the final answer is:\n2 + 2 * 2 = 6"
}
```


## Cleanup

1. To delete the resources deployed to your AWS account via AWS CDK, run the following command:

```bash
cdk destroy
```


---

Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
84 changes: 84 additions & 0 deletions apigw-lambda-bedrock-cdk-java/apigw-lambda-bedrock-java.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"title": "Amazon API Gateway to AWS Lambda to Amazon Bedrock",
"description": "Make a request to Bedrock via API Gateway using Java and CDK",
"language": "Java",
"level": "200",
"framework": "CDK",
"introBox": {
"headline": "How it works",
"text": [
"Amazon API Gateway receives the HTTP POST request containing the prompt.",
"AWS Lambda is triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using AWS SDK for Java. It uses Anthropic Claude-v2 model and sets other required parameters to fixed values for simplicity.",
"Based on the given prompt, using Anthropic Claude-v2 model, Amazon Bedrock generates the content and returns the response to Lambda.",
"Lambda processes the Bedrock output and sends it back to the user via the API Gateway."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-bedrock-cdk-java",
"templateURL": "apigw-lambda-bedrock-cdk-java",
"projectFolder": "apigw-lambda-bedrock-cdk-java",
"templateFile": "infrastructure/src/main/java/com/example/InfrastructureApp.java"
}
},
"resources": {
"bullets": [
{
"text": "Amazon Bedrock Model Access",
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html"
}
]
},
"deploy": {
"text": [
"cdk deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>cdk delete</code>."
]
},
"authors": [
{
"name": "Grzegorz Kozub",
"image": "https://drive.google.com/file/d/1eo1_Zx4gIgmQO1cupdfUJbkxjEpC7NYJ/view?usp=sharing",
"bio": "Cloud Application Architect working at AWS Professional Services.",
"linkedin": "kozubgrzegorz",
"twitter": "kozubg"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "apigw",
"label": "API Gateway REST API"
},
"icon2": {
"x": 50,
"y": 50,
"service": "lambda",
"label": "AWS Lambda"
},
"icon3": {
"x": 80,
"y": 50,
"service": "bedrock",
"label": "Amazon Bedrock"
},
"line1": {
"from": "icon1",
"to": "icon2"
},
"line2": {
"from": "icon2",
"to": "icon3"
}
}
}
56 changes: 56 additions & 0 deletions apigw-lambda-bedrock-cdk-java/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"title": "Amazon API Gateway to AWS Lambda to Amazon Bedrock",
"description": "Make a request to Bedrock via API Gateway using Java and CDK",
"language": "Java",
"level": "200",
"framework": "CDK",
"introBox": {
"headline": "How it works",
"text": [
"Amazon API Gateway receives the HTTP POST request containing the prompt.",
"AWS Lambda is triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using AWS SDK for Java. It uses Anthropic Claude-v2 model and sets other required parameters to fixed values for simplicity.",
"Based on the given prompt, using Anthropic Claude-v2 model, Amazon Bedrock generates the content and returns the response to Lambda.",
"Lambda processes the Bedrock output and sends it back to the user via the API Gateway."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-bedrock-cdk-java",
"templateURL": "apigw-lambda-bedrock-cdk-java",
"projectFolder": "apigw-lambda-bedrock-cdk-java",
"templateFile": "infrastructure/src/main/java/com/example/InfrastructureApp.java"
}
},
"resources": {
"bullets": [
{
"text": "Amazon Bedrock Model Access",
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html"
}
]
},
"deploy": {
"text": [
"cdk deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>cdk delete</code>."
]
},
"authors": [
{
"name": "Grzegorz Kozub",
"image": "https://drive.google.com/file/d/1eo1_Zx4gIgmQO1cupdfUJbkxjEpC7NYJ/view?usp=sharing",
"bio": "Cloud Application Architect working at AWS Professional Services.",
"linkedin": "kozubgrzegorz",
"twitter": "kozubg"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions apigw-lambda-bedrock-cdk-java/infrastructure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.classpath.txt
target
.classpath
.project
.idea
.settings
.vscode
*.iml

# CDK asset staging directory
.cdk.staging
cdk.out

5 changes: 5 additions & 0 deletions apigw-lambda-bedrock-cdk-java/infrastructure/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"app": "mvn -e -q compile exec:java",
"context": {
}
}
6 changes: 6 additions & 0 deletions apigw-lambda-bedrock-cdk-java/infrastructure/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cd ../software/
mvn clean package
cd ../infrastructure
mvn clean compile
cdk synth
cdk deploy
61 changes: 61 additions & 0 deletions apigw-lambda-bedrock-cdk-java/infrastructure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>infrastructure</artifactId>
<version>0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cdk.version>2.110.0</cdk.version>
<junit.version>5.9.1</junit.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>com.example.InfrastructureApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- AWS Cloud Development Kit -->
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>aws-cdk-lib</artifactId>
<version>${cdk.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example;

import software.amazon.awscdk.App;

public final class InfrastructureApp {
public static void main(final String[] args) {
App app = new App();

new InfrastructureStack(app, "ApiGatewayLambdaBedrockStack");

app.synth();
}
}
Loading

0 comments on commit 814035b

Please sign in to comment.