How to Build a Serverless Application using Serverless Framework

How to Build a Serverless Application using Serverless Framework

Build and deploy a Java based serverless REST API in no time

Introduction

In my previous articles, I talked about building and deploying serverless applications using AWS Chalice and AWS SAM.

In this article, I will walk you through the steps required to build and deploy the same serverless application that gets the latest news from Google News. But this time, we will use the Serverless Framework and Java for our development. Like Chalice and SAM, Serverless offers a rich set of tools that enable developers to build serverless applications quickly in your favorite programming language.

SLS.png

Prerequisites

This tutorial requires an AWS account. If you don’t have one already, go ahead and create one. Our application is going to use only the free-tier resources, so cost shouldn’t be an issue.

You also need to configure security and create users and roles for your access.

How to Configure AWS Credentials

SAM uses the AWS Command Line Interface (CLI) behind the scenes to deploy the project. If you haven’t used AWS’s CLI before to work with AWS resources, you can install it by following the guidelines here.

Once installed, you need to configure your AWS CLI to use the credentials from your AWS account.

aws_configure.png

How to Install Serverless

Next, you need to install Serverless. We will be using Java in this tutorial, but you can use any language runtime supported by AWS Lambda.

Verify Java Installation

❯ java --version
openjdk 17.0.4.1 2022-08-12
OpenJDK Runtime Environment Temurin-17.0.4.1+1 (build 17.0.4.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.4.1+1 (build 17.0.4.1+1, mixed mode)

Install Serverless

The recommended approach for installing Serverless is to use the Node Package Manager.

Verify that if you have NPM installed.

❯ npm --version
8.19.2

Next, install Serverless globally using the following command

❯ npm install -g serverless

Verify Serverless Installation

❯ serverless -v
Framework Core: 3.22.0
Plugin: 6.2.2
SDK: 4.3.2

How to Create a Project

Next, create a new folder for our project.

❯ serverless create --template aws-java-maven --path daily-news-java-sls

Since we want to create a Java project, we will need to pass some additional parameters.

Parameters:

  • --template aws-java-maven: use Java as runtime and Maven as the dependency manager

  • --path daily-news-java-sls: the name of our project

This will create a daily-news-java-sls folder in your current directory. You can see that Serverless has created several files in this folder.

❯ ls -la
total 24
drwxr-xr-x   6   192 Oct  3 11:44 ./
drwxr-xr-x  11   352 Oct  3 11:44 ../
-rw-r--r--   1   125 Sep 28 12:10 .gitignore
-rw-r--r--   1  3076 Sep 28 12:10 pom.xml
-rw-r--r--   1  3189 Oct  3 11:44 serverless.yml
drwxr-xr-x   3    96 Sep 28 12:10 src/

Let’s take a look at the Handler.java file.

The serverless create command created a simple Lambda function that returns the JSON body with a message and status code. We can now change this template and add more code to read news from Google.

Now let’s take a look at the serverless.yml file.

This contains the CloudFormation template that creates our AWS Lambda resource. We will later update this to add our API Gateway endpoint in front of the Lambda.

We will be using Java’s internal HTTP and XML parsing libraries, so we don’t need to add any dependencies to our pom.xml file. Note that the default boilerplate code comes with compiler source set to 1.8. We will need to update that to 11 so that we can use the new HTTP library that is part of Java 11.

We also need to update the serverless.yml file to use Java 11 runtime.

Going by the object-oriented way of Java, let’s also create a NewsItem class that contains the title and publication date of a news item.

Note that we have overridden the toString method. This is to create a JSON representation of the object and avoid usage of any JSON parsing libraries.

Next, you need to add a method to fetch the RSS feed from Google, parse it to extract the news title and publication date, and create a list of news items. To do this, add the following code to your Handler.java

Now let’s update the handleRequest method in Handler.java to invoke this method and return the list of news items as a result.

Let's add an API Gateway HTTP endpoint for our function in serverless.yml

This creates a new API /news and attaches it to our Lambda function.

How to Build

From the daily-news-java-sls folder, run the mvn clean install command.

This compiles your source code and builds any dependencies that you have in the application. After a successful build, we should have an artifact at daily-news-java-sls/target/daily-news-java-sls-dev.jar that we will use in our deployment step.

❯ ls -la
total 8136
drwxr-xr-x  8      256 Oct  5 16:33 ./
drwxr-xr-x  9      288 Oct  5 16:41 ../
drwxr-xr-x  4      128 Oct  5 16:33 classes/
-rw-r--r--  1  4152670 Oct  5 16:33 daily-news-java-sls-dev.jar
drwxr-xr-x  3       96 Oct  5 16:33 generated-sources/
drwxr-xr-x  3       96 Oct  5 16:33 maven-archiver/
drwxr-xr-x  3       96 Oct  5 16:33 maven-status/
-rw-r--r--  1     9700 Oct  5 16:33 original-daily-news-java-sls-dev.jar

How to Deploy the Project

Before you can deploy the application to AWS, you need to configure Serverless to use your AWS credentials. You can choose from one of the many ways specified here.

Let’s deploy the application now. From the daily-news-java-sls folder, run the serverless deploy command.

❯ sls deploy

Deploying daily-news-java-sls to stage dev (us-west-2)

✔ Service deployed to stack daily-news-java-sls-dev (142s)

endpoint: GET - https://xxxxxxxxxxxx.execute-api.us-west-2.amazonaws.com/news
functions:
  news: daily-news-java-sls-dev-news (4.2 MB)

This deploys our application on AWS using Amazon API Gateway and AWS Lambda. It takes the deployment artifacts that we built with the mvn clean install command, packages and uploads them to an Amazon S3 bucket, and deploys the application using AWS CloudFormation.

Screenshot 2022-10-05 at 6.44.20 PM.png

Screenshot 2022-10-05 at 6.44.34 PM.png

We can now try accessing the API using the endpoint URL provided above.

How to Clean Up Resources

We can use the serverless remove command to delete the AWS CloudFormation stack along with all the resources it created when we ran the serverless deploy command.

❯ sls remove
Removing daily-news-java-sls from stage dev (us-west-2)

✔ Service daily-news-java-sls has been successfully removed (31s)

Conclusion

Congratulations!! You just deployed a serverless application on AWS using the Serverless framework. It did involve a bit more work than earlier, but it wasn’t too hard either.

You can now go ahead and make any modifications to your Hanlder.java file and rerun serverless deploy to redeploy your changes.

The full source code for this tutorial can be found here.

Thank you for staying with me so far. Hope you liked the article. You can connect with me on LinkedIn, where I regularly discuss technology and life. Also take a look at some of my other articles and my YouTube channel. Happy reading. 🙂