Serverless on AWS Lambda using Java


Preface

Serverless is now one of the hottest trend in IT world. It allows you to build, test and deploy any API without any server configuration. Great, isn't? Apart from this important point, it could save you a lot of money because you pay only for the compute time you consume – there is no charge when your code is not running.


How it works

One of the multiple alternatives is this one attached.

1. API Gateway

It defines all the API (PUT, GET, POST, etc).

2. Amazon Lambda

It's a function that receives the request throw the API Gateway and connects it to your Java Handler implementation. We will see how simple it is later on.

3. Java

A very simple implementation of RequestHandler from Amazon. It receives the request and persist it into DynamoDB.

4. DynamoDB

DB to store our values.


Preparation

Let's assume you already have an AWS account, if not, you can create it here .

To make this setup easier, let's split the configuration into:
- API Gateway - Lambda - Java - DynamoDB

We will start from the DynamoDB in reverse mode so you can see how to tide up all together and test it.

1. Create a simple table on DynamoDB.

Access to DynamoDB here and create a simple table: Customer

2. Clone Java from GitHub

2.1. Clone it from here:

https://github.com/jmazzetti/simple-lambda-serverless

2.2. Run mvn clean install and copy the PATH of the created JAR (simple-lambda-serverless-1.0.0-SNAPSHOT.jar)

3. Create a Lambda Function

3.1. Create an new Lambda Function (Author from Scratch)

3.2 Click Next

3.3. Configure Function

Basic Information

  • Name: A simple name for our lambda function
  • Description: optional.
  • Runtime: Java8

Lambda function code

  • Upload the JAR file we did in point 2.
  • Environment variables: AWSEXECUTIONENV -> AWSLambdajava8

This is an important point. We need to specify the Amazon SDK in order to use all the features from Amazon (e.g. DynamoDB connection).

Lambda function handler and role

  • Handler: That's our Handler implementation, it means where the lambda will call when we execute it. For our case is: com.jmazzetti.lambda.dynamodb.SaveCustomerHandler

  • Role / Existing Role: choose an existing role in order to execute this lambda (I recommend for this test purpose LambdaFullAccessRole.

3.4. Review

Check all the params.

3.5. Configure Event Test

Click on configure Event Test and let's pass our JSON to create a new entry on DynamoDB

{
  "id": "2",
  "firstName": "Jhon",
  "lastName": "Smith",
  "age": 20,
  "address": "United Kingdom"
}

3.6. Test

Click on Test.

4. Create an API Gateway

4.1 Click on new API -> POST and select: Integration Type: Lambda
LambdaRegion: our AWS Region, in this case us-east-1

4.2. Click on Action and Test

{
  "id": "2",
  "firstName": "Jhon",
  "lastName": "Smith",
  "age": 20,
  "address": "United Kingdom"
}

DONE!


Troubleshooting

  • Bear in mind that the Region 2 is not present in the default SDK, I think it's easier to pick an existing region, rather than change the SDK.

  • Remember to setup Java8 in your IDE while building the project

  • It's also a good option to upload everything on S3 to keep track of your work and versions. Amazon gives you the option to upload the composed JAR from S3.