simon
simon

Reputation: 559

How to create a Lambda function using AWS SDK for Java 2.0

How do one create a Lambda function using AWS SDK for Java 2.0? Usinx SDK 1.x, I can do so using the following:

public String handleRequest(S3Event s3event, Context context) {
System.out.println("do stuff");
return "success";
}

Using java SDK 2.x, I can't seem to find the equivalent dependencies for S3Event and Context object? I would really appreciate if someone could point me to examples. Or should I just stick to using SDK 1.x if 2.x is not mature enough for handling lambda?

Upvotes: 7

Views: 5051

Answers (1)

stdunbar
stdunbar

Reputation: 17455

S3Event is part of the AWS Lambda Java Events library where Context is part of the AWS Lambda Java Core. When you include the events library you do pull in the 1.x Java SDK. However, if you use the Java Stream version of the Lambda handler you can remove the dependency to the events library and thus remove your need for the 1.x SDK. Your code would look something like:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.jayway.jsonpath.JsonPath;


public class S3EventLambdaHandler implements RequestStreamHandler {
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {

        try {
            List<String> keys = JsonPath.read(inputStream, "$.Records[*].s3.object.key");

            for( String nextKey: keys )
                System.out.println(nextKey);
        }
        catch( IOException ioe ) {
            context.getLogger().log("caught IOException reading input stream");
        }
    }
}

then you can read the JSON that is in the S3Event yourself. In effect, you're doing the serialization of the S3Event in your code instead of having the Amazon library do it.

Obviously the biggest downside is that you have to serialize the event yourself. An example S3 event in JSON can be found here.

The above example uses JsonPath to pull out, in this case, the keys from the event. You'll be amazed how much smaller your Lambda code is after you remove the Lambda Events library.

Edit - September 2022

I know it's not clear sometimes but the AWS Lambda Core Library for Java does not have a version 2 - even 4 years after this post. The Lambda Core library defines things like Context and RequestStreamHandler as shown in my code above. As of this writing, there are still 3 libraries that can be used for writing the server side of a Lambda in Java and they are all still in the com.amazonaws package. These are not the libraries for accessing, as a client, the rest of AWS for which there are the V2 (in the software.amazon package) libraries. The answer to the OP's question is still the same - a V2 library for writing the server side of a Lambda in Java does not exist. Interacting, as a client, with the rest of the AWS environments - sure, there has been a V2 for quite a while. But not the "server" side of a Lambda.

The AWS Lambda Java Events library is now on V3 and yet it still uses the com.amazonaws package. For reference, I would write the code above without using a RequestStreamHandler as the events library is much lighter than it was 4 years ago but that doesn't change the package name.

Upvotes: 8

Related Questions