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.
amazon web services - How to create a Lambda function using AWS SDK for Java 2.0 - Stack Overflow
amazon web services - AWS Java SDK Version For Creating a Lambda - Stack Overflow
In what scenario does using Java in Lambda make sense?
Why has Java fallen out of favor for use in lambdas?
Videos
However I am trying to use the latest SDK as recommended here but this is completely different and the
RequestHandlerinterface doesn't appear to exist anymore.
You're using wrong a dependency. This is an SDK for using AWS Services via its REST API, like:
- Putting an object to S3
- Listing EC2 instances
- Deleting an item from AWS DynamoDB
- Invoking a Lambda
- …
I.e. this is an SDK for working with various AWS services. It consists of many libraries, like aws-java-sdk-s3, aws-java-sdk-dynamodb. aws-java-sdk-lambda is one of them, but it is for interacting with Lambda API and not for authoring Lambdas.
The libraries you need for authoring Lambdas are:
com.amazonaws:aws-lambda-java-corecom.amazonaws:aws-lambda-java-events
As you see, those are different. First provides Handler interfaces you're looking for and second contains various events Lambda can accept as input: SNS events, CloudWatch timers and so on.
From here:
Lambda supports two approaches for creating a handler:
Loading the handler method directly without having to implement an interface. This section describes this approach.
Implementing standard interfaces provided as part of aws-lambda-java-core library (interface approach). For more information, see Leveraging Predefined Interfaces for Creating Handler (Java).
Here is aws-lambda-java-core