Upstash Documentation

Serverless API with Java and Redis

1 min read

In this tutorial, we will build a stateful serverless API using Java and Redis on AWS Lambda. The API will simply count the page views and return it as HTTP response.

Prerequisites#

  1. Install the Serverless Framework installed with an AWS account set up.
  1. Install JDK and not Java JRE. Set your JAVA_HOME.
  2. Install Apache Maven.
  3. Create a free Serverless Redis database from Upstash as described here.

Project Setup#

Create the project:

Add jedis as dependency to the pom.xml:

pom.xml

Update serverless.yml as below:

serverless.yml

Counter Function Setup#

Update src/main/java/com/serverless/Handler.java as below:

src/main/java/com/serverless/Handler.java

In the above code, you need to replace your Redis endpoint and password. You can copy Jedis connection code from the Upstash Redis Console -> Your Database -> Connect to your database -> Java.

Build and Deploy#

Build your project:

Deploy to AWS:

Visit the output URL to see the counter in action.