Skip to content

Commit 13c2dd6

Browse files
author
Angel Conde Manjon
committed
Adding SpringBoot Demo for Amazon Bedrock using Java
1 parent 971a47f commit 13c2dd6

File tree

14 files changed

+862
-0
lines changed

14 files changed

+862
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
target/
2+
!**/src/main/**/target/
3+
!**/src/test/**/target/
4+
5+
.aws-sam/*
6+
7+
# Compiled class file
8+
*.class
9+
10+
# Log file
11+
*.log
12+
13+
# BlueJ files
14+
*.ctxt
15+
16+
# Mobile Tools for Java (J2ME)
17+
.mtj.tmp/
18+
19+
# Package Files #
20+
*.jar
21+
*.war
22+
*.nar
23+
*.ear
24+
*.zip
25+
*.tar.gz
26+
*.rar
27+
28+
!.mvn/wrapper/maven-wrapper.jar
29+
30+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
31+
hs_err_pid*
32+
replay_pid*
33+
34+
### IntelliJ IDEA ###
35+
.idea/*
36+
*.iws
37+
*.iml
38+
*.ipr
39+
40+
### Eclipse ###
41+
.apt_generated
42+
.classpath
43+
.factorypath
44+
.project
45+
.settings
46+
.springBeans
47+
.sts4-cache
48+
49+
### VS Code ###
50+
.vscode/*
51+
.history/
52+
*.vsix
53+
54+
### Mac OS ###
55+
.DS_Store
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Amazon Bedrock Demo using SpringBoot
2+
3+
This is a sample project, that demonstrates how to access Amazon Bedrock from a SpringBoot application that could deployed on AWS Fargate or on AWS Lambda (via native compilation step for avoid those cold start times).
4+
5+
## Prerequisites
6+
7+
To deploy this demo you need:
8+
- Access to an [AWS account](https://aws.amazon.com/free/)
9+
- [Apache Maven](https://maven.apache.org/)
10+
11+
This demo sent rest calls have signed with AWS credentials that will be mapped to a IAMRole with permissions to access Amazon Bedrock.
12+
13+
This sample code does not secure the Rest API in any way.
14+
15+
In a production environment you could also employ Amazon Cognito or other mechanisms to secure the API and dynamically attach the role to the API depending the logged user following best practices. The project could be integrated with Spring Security in order to gain such features.
16+
17+
## Build the application
18+
To build the application, execute the following command in the `springboot-bedrock-demo` directory:
19+
20+
```bash
21+
mvn package
22+
```
23+
24+
## Access the Application
25+
26+
*Note: Replace `URL_PREFIX.execute-api.us-east-1.amazonaws.com` with the actual URL that has been returned during the deployment.*
27+
28+
### Send a prompt
29+
30+
To send a prompt to the currently active model, use the following endpoint:
31+
32+
```
33+
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/bedrock/prompt?p=YOUR_PROMPT
34+
```
35+
36+
Example:
37+
38+
```
39+
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/bedrock/prompt?p=Hello, how are you?
40+
```
41+
42+
### Check active model
43+
44+
To check which model is currently active, use the following endpoint:
45+
46+
```
47+
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/bedrock/llm
48+
```
49+
50+
### Change active model
51+
52+
To change the active model, use the following endpoint:
53+
54+
```
55+
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/bedrock/llm/set?model=NEW_MODEL
56+
```
57+
58+
Example:
59+
60+
```
61+
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/bedrock/llm/set?model=anthropic.claude-instant-v1
62+
```
63+
64+
65+
The following models are currently available as part of this app:
66+
- `anthropic.claude-v1`
67+
- `anthropic.claude-instant-v1`
68+
- `anthropic.claude-v2`
69+
70+
Provide the following configuration in the application.yml file
71+
72+
aws.os.region=aws-region-you-are-using
73+
aws.os.endpoint=opensearch-domain-endpoint
74+
aws.iamrole=iam-master-role-ARN
75+
This project uses Master IAM Role as it is creating a new index with Fake data. The project is using STS to get credentials for that Role, remember to adapt it for your needs. In the blogpost, the app is launched via Fargate and the Task Role is used for the permission chain.
76+
77+
There are 2 resource flows in this project
78+
79+
CRUD Operation using ElasticsearchRepository - CustomerController
80+
To create,update,delete,retrieve data from the indexes
81+
The retrieve operation is limited
82+
You can use Postman to interact with the API. The API docs can be seen at http://localhost:8080/swagger-ui/index.html
83+
84+
Live demo with URL: http://localhost:8080/search
85+
Start to input some characters in the search box, which will open an auto-complete box of maximum 5 suggestions.
86+
Complete the search text and click search button to see the search results.
87+
88+
89+
90+

demos/springboot-bedrock-demo/pom.xml

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>org.acme</groupId>
7+
<artifactId>springboot-bedrock</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
10+
<properties>
11+
<compiler-plugin.version>3.11.0</compiler-plugin.version>
12+
<maven.compiler.release>17</maven.compiler.release>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<skipITs>true</skipITs>
16+
</properties>
17+
18+
<parent>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-parent</artifactId>
21+
<version>3.1.5</version>
22+
<relativePath/> <!-- lookup parent from repository -->
23+
</parent>
24+
25+
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>software.amazon.awssdk</groupId>
30+
<artifactId>bom</artifactId>
31+
<version>2.21.10</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-dependencies</artifactId>
38+
<version>2022.0.4</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
</dependencies>
43+
</dependencyManagement>
44+
45+
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-web</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-actuator</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-devtools</artifactId>
58+
<scope>runtime</scope>
59+
<optional>true</optional>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
64+
</dependency>
65+
66+
<!-- lombok annotation processing -->
67+
<dependency>
68+
<groupId>org.projectlombok</groupId>
69+
<artifactId>lombok</artifactId>
70+
<scope>provided</scope>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.springdoc</groupId>
75+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
76+
<version>2.2.0</version>
77+
</dependency>
78+
79+
80+
<dependency>
81+
<groupId>software.amazon.awssdk</groupId>
82+
<artifactId>sts</artifactId>
83+
<exclusions>
84+
<exclusion>
85+
<groupId>commons-logging</groupId>
86+
<artifactId>commons-logging</artifactId>
87+
</exclusion>
88+
</exclusions>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>software.amazon.awssdk</groupId>
93+
<artifactId>bedrockruntime</artifactId>
94+
<version>2.20.162</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>software.amazon.awssdk</groupId>
98+
<artifactId>bedrock</artifactId>
99+
<version>2.20.162</version>
100+
</dependency>
101+
<dependency>
102+
<groupId>org.json</groupId>
103+
<artifactId>json</artifactId>
104+
<version>20230618</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>org.testng</groupId>
108+
<artifactId>testng</artifactId>
109+
<version>RELEASE</version>
110+
<scope>compile</scope>
111+
</dependency>
112+
</dependencies>
113+
<build>
114+
<plugins>
115+
<plugin>
116+
<groupId>org.springframework.boot</groupId>
117+
<artifactId>spring-boot-maven-plugin</artifactId>
118+
</plugin>
119+
<plugin>
120+
<groupId>org.graalvm.buildtools</groupId>
121+
<artifactId>native-maven-plugin</artifactId>
122+
</plugin>
123+
<plugin>
124+
<artifactId>maven-compiler-plugin</artifactId>
125+
<version>${compiler-plugin.version}</version>
126+
<configuration>
127+
<compilerArgs>
128+
<arg>-parameters</arg>
129+
</compilerArgs>
130+
</configuration>
131+
</plugin>
132+
</plugins>
133+
</build>
134+
<profiles>
135+
<profile>
136+
<id>native</id>
137+
<activation>
138+
<property>
139+
<name>native</name>
140+
</property>
141+
</activation>
142+
<properties>
143+
<skipITs>false</skipITs>
144+
</properties>
145+
</profile>
146+
</profiles>
147+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.acme;
2+
3+
/*
4+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
7+
* software and associated documentation files (the "Software"), to deal in the Software
8+
* without restriction, including without limitation the rights to use, copy, modify,
9+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
24+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
25+
26+
27+
import lombok.extern.slf4j.Slf4j;
28+
@Slf4j
29+
@SpringBootApplication
30+
public class SpringBootBedrockApplication {
31+
public static void main(String[] args) {
32+
SpringApplication.run(SpringBootBedrockApplication.class, args);
33+
}
34+
35+
@Bean
36+
public WebMvcConfigurer corsConfigurer() {
37+
return new WebMvcConfigurer() {
38+
@Override
39+
public void addCorsMappings(CorsRegistry registry) {
40+
//enable CORS for all domains, remember to adapt this on production scenarios
41+
registry.addMapping("/**").allowedOrigins("*");
42+
}
43+
};
44+
}
45+
46+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.acme.config;
2+
3+
/*
4+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
7+
* software and associated documentation files (the "Software"), to deal in the Software
8+
* without restriction, including without limitation the rights to use, copy, modify,
9+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
25+
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
26+
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
27+
28+
/**
29+
* @author Angel Conde
30+
*
31+
*/
32+
@Slf4j
33+
@Configuration
34+
public class AWSCredentialsConfig {
35+
36+
@Value("${aws.iam.role}")
37+
private String assumeRoleARN="";
38+
39+
@Value("${aws.bedrock.region}")
40+
private String region;
41+
42+
@Bean
43+
public AwsCredentialsProvider customPermissionProvider() {
44+
log.info("Assuming role "+ assumeRoleARN);
45+
return StsAssumeRoleCredentialsProvider
46+
.builder()
47+
.refreshRequest(() -> AssumeRoleRequest
48+
.builder()
49+
.roleArn(assumeRoleARN)
50+
.roleSessionName("bootful")
51+
.build())
52+
.build();
53+
}
54+
}

0 commit comments

Comments
 (0)