Serverless Intro: Using Java with AWS Lambda

710 VIEWS

· ·

Serverless technology is the latest trend in enabling developers to build pieces of functionality by providing a clean interface to build against and a seamless way to deploy without needing to involve the Operations or Infrastructure teams. This article shows how to deploy a working Java app on the AWS Lambda platform.

Introduction to Serverless Apps on AWS Lambda

For the purposes of this post, I’m assuming you know the basics of serverless architecture, have an AWS account, and are interested in seeing how much actual effort is involved to stand up a basic serverless app.

In this article we will interchange the terms “serverless app” and “function,” because the current generation of serverless architecture is geared towards apps only being a single specific function that are event-based and stateless.

Why use AWS Lambda? Well, it is one of the most mature publicly available serverless platforms from a major public cloud provider. It is both well documented and supported. And Lambda is one of the providers that only bills serverless functions when they are active, not idle, with a generous free tier, so it is very cost-effective. The first one million requests are basically free with a couple conditions.

Supported Incoming Events Sources

Lambda functions are activated by an incoming event. There is a full list of supported incoming event sources types, but some examples are:

  1. Amazon Alexa
  2. Amazon API Gateway
  3. Amazon Lex
  4. Scheduled Events (via CloudWatch)
  5. Amazon IoT Button (based on Amazon Dash Buttons)
  6. For this simple app, we are not going to be connecting to an event source. The app will be a self-contained function. There are lots of articles available to show how to interconnect a source to a function, and we want to show how easy it is to start to use serverless. What you can do with it is completely up to you.

    Building a Function (App) in Java 8 for Lambda

    The function we are using is available on GitHub as aws-lambda-java-hello, and is built using Maven.

    The project actually only requires two files, but I have included a test case for validation.

    The biggest point for the base project is in the maven pom file. You need to have the packaging set to jar and add the aws-lambda-java-core dependency to access all the AWS features from your code.

    Project Files

    Contents of pom.xml

    Contents of Hello.java

    Contents of TestHello.java

    Executing the Build

    Now you have successfully built a Java app that can run as a Lambda function.

    Note that in the unit test, the import for Asserts needs to be static, or it won’t successfully compile and run.

    Creating a Lambda Function in AWS

    1. Open Lambda in the AWS Console. If this is your first function, simply click “Create a function.”

    Fig. 1

    2. Next up is selecting to build a function from scratch, and to provide a name and a role name, and on the bottom of the screen, select the “Simple Microservice” policy template. Then click Create function.

    Fig. 2

    Fig. 3
    3. You now have a function created.

    Fig. 4

    4. Time to upload the Java code, and save it.

    Fig. 5

    5. Create a test case.

    Fig. 6

    6. Run the test, and SUCCESS!!!

    Fig. 7

    Conclusion

    As you can see, it’s simple to get a basic serverless app up and running. Once the function is running, incoming triggers and resources can be dragged and dropped within the Lambda console to integrate the function with other processes and features running in AWS.

    Do you think you can beat this Sweet post?

    If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Vince Power is an Enterprise Architect at Medavie Blue Cross. His focus is on cloud adoption and technology planning in key areas like core computing (IaaS), identity and access management, application platforms (PaaS), and continuous delivery.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu