AWS Lambda Function Hello World! in Java

The below is literally all the code you have to do in order to have a functioning AWS Lamda function:

package com.wordpress.vrsbrazil.lambda.functions;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

/**
 * Created by vrsbrazil on 02/03/2017.
 */
public class TestFunctionHandler implements RequestHandler<Object, Object> {

    public Object handleRequest(Object o, Context context) {
        return ("Hello, " + o + "!");
    }

}

Here is the dependency you need:

<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.0.0</version>
        </dependency>

Now to test it, you will need some simple JUnit coding, for this test coding here is the dependencies you will need:

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

When you test a lambda function you will need a Lamda context, as you are making an offline test you will need to make you own, this is dead simple, you just need a class that implements com.amazonaws.services.lambda.runtime.Context, so here is the context I created:

import com.amazonaws.services.lambda.runtime.ClientContext;
import com.amazonaws.services.lambda.runtime.CognitoIdentity;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;

/**
 * Created by vrsbrazil on 02/03/2017.
 */
public class LamdaTestContext implements Context{
    public String getAwsRequestId() {
        return null;
    }

    public String getLogGroupName() {
        return null;
    }

    public String getLogStreamName() {
        return null;
    }

    public String getFunctionName() {
        return null;
    }

    public CognitoIdentity getIdentity() {
        return null;
    }

    public ClientContext getClientContext() {
        return null;
    }

    public int getRemainingTimeInMillis() {
        return 0;
    }

    public int getMemoryLimitInMB() {
        return 0;
    }

    public LambdaLogger getLogger() {
        return null;
    }
}

Finally, here is the test code:

import com.wordpress.vrsbrazil.lambda.functions.TestFunctionHandler;
import com.amazonaws.services.lambda.runtime.Context;
import org.junit.Assert;
import org.junit.Test;

/**
 * Created by vrsbrazil on 02/03/2017.
 */
public class TesteTestFunction {

    @Test
    public void testLambdaFunctionHandler() {

        TestFunctionHandler function = new TestFunctionHandler();
        Context ctx = new LamdaTestContext();
        String s = "Teste";

        Object output = function.handleRequest(s, ctx);

        if (output != null) {
            System.out.println(output.toString());
        }
        Assert.assertEquals("Hello, Teste!", output);


    }

}

Here is a git project for you: https://github.com/vrsbrazil/awslambdajavahelloworld

Thank you!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s