Hey NetHeads!! off-late, I have been working on AWS Sagemaker to deploy and serve Machine Learning models. While scratching my head to understand AWS Sagemaker, deploy the models, and invoke the endpoint via AWS CLI, I was keen to experiment on calling the Sagemaker endpoint from Spring Rest Controller.
I have gone through forums and docs to implement the same but found the solution in bits and pieces from multiple sources.
This story is a one-stop destination for Sagemaker beginners like me.
Before you start
Make sure you have the below setup ready
- AWS Account
- AWS
credentials
file in local machine (default location is~/.aws/credentials
`) - AWS Sagemaker Service is up and running.
- IDE (IntelliJ/Eclipse)
Create a SpringBoot Application
let’s quickly create a Spring Boot application using Spring Initializr.
Add Spring Web as a dependency and click on Generate
to download a Spring boot project with a basic structure.
Unzip the file and open it as a Maven project in IntelliJ.
Add the following dependencies into your pom file
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sagemakerruntime</artifactId>
<version>1.11.979</version>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
</exclusion>
</exclusions>
</dependency><dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.979</version>
</dependency>
The Spring Initializr creates a simple application class for you. Create a AwsSageMakerRuntime
bean.
Create a new Java Pojo class to de-serialize input data passed to the Sagemaker endpoint service.
Add the following code for the controller:
Now run the application and post the prediction request on http://localhost:8080/invoke-sagemaker
Use the sample data to verify the predict
result from the Sagemaker deployment
[[5,4,3,2]]
Conclusion
This way, one can build a Rest Controller to invoke AWS SageMaker Runtime Endpoints using the Java language with Spring Boot platform.
Stay tuned for more productionised solutions!
Thank you for reading!!