Member-only story
Going Serverless With Spring Cloud Function, AWS Lambda Java 17 Support, and SnapStart
Develop and deploy a Java serverless function that invokes OpenAI API

AWS Lambda added Java 17 support on April 27, 2023, a much anticipated and awaited feature! With this new feature, Spring Boot 3 developers now have a new option to deploy their Java Lambda functions, in addition to the native image option coupled with the Lambda custom runtime.
In this article, let’s explore the details of developing and deploying a Java Lambda function, built on Spring Cloud Function, with Lambda Java 17 support and SnapStart turned on. This function will be fronted by an HTTP API gateway.
For our Java Lambda function, we will expand on my previous article Integrating ChatGPT and Whisper APIs Into Spring Boot Microservice, turn it into a Lambda function with a chat feature, calling OpenAI API /chat/completions
endpoint.

Spring Cloud Function
Spring Cloud Function supports a uniform programming model across serverless providers. It enables Spring Boot features (auto-configuration, dependency injection, metrics) on serverless providers.
Spring Cloud Function abstracts away all of the transport details and infrastructure, allowing the developer to keep all the familiar tools and processes, and focus firmly on business logic.
Let’s look at the implementation details for our OpenAI API chat function.
Add dependencies in pom.xml
We first need to add Spring Cloud Function related dependencies to our pom.xml
. While at it, let’s also add a few AWS dependencies as that’s where we will be deploying our Lambda function to.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.0.2</version>
</dependency>…