Serverless Intro: Using Java with AWS Lambda

1056 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

     ./aws-lambda-java-hello/pom.xml
    ./aws-lambda-java-hello/src/test/java/sh/power/TestHello.java
    ./aws-lambda-java-hello/src/main/java/sh/power/Hello.java

    Contents of pom.xml

     
      4.0.0
      sh.power
      aws-lambda-java-hello
      jar
      1.0-SNAPSHOT
      aws-lambda-java-hello
      
        
          com.amazonaws
          aws-lambda-java-core
          1.2.0
        
        
          junit
          junit
          4.12
        
      
      
        
          
            org.apache.maven.plugins
            maven-shade-plugin
            2.3
            
              false
            
            
              
                package
                
                  shade
                
              
            
          
        
      
    

    Contents of Hello.java

     package sh.power;
    /** 
     * @author vincepower
     */
    public class Hello {
      public String myHandler() {
        return "namaste";
      }
    }

    Contents of TestHello.java

     package sh.power;
    import org.junit.Test;
    import static org.junit.Assert.*;
    public class TestHello {
      @Test
      public void testMyHandler() {
        Hello h = new Hello();
        String result = h.myHandler();
        assertEquals("namaste", result);
      }
    }

    Executing the Build

     $ mvn clean package
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] ------------------------------------------------------------------------
    [INFO] Building aws-lambda-java-hello 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    . . .
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running sh.power.TestHello
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.096 sec
    Results :
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    . . .
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 4.878 s
    [INFO] Finished at: 2017-12-21T20:46:22-04:00
    [INFO] Final Memory: 18M/165M
    [INFO] ------------------------------------------------------------------------
    $ ls -l target/aws-lambda-java-hello-1.0-SNAPSHOT.jar 
    -rw-r--r--  1 user  staff  374769 21 Dec 20:46 target/aws-lambda-java-hello-1.0-SNAPSHOT.jar

    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.


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