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.

Answer from stdunbar on Stack Overflow
🌐
AWS
docs.aws.amazon.com › aws sdk for java › developer guide for version 2.x › sdk for java 2.x code examples › lambda examples using sdk for java 2.x
Lambda examples using SDK for Java 2.x - AWS SDK for Java 2.x
... There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository ... /* * Lambda function names appear as: * * arn:aws:lambda:us-west-2:335556666777:function:HelloFunction * * To find this value, look at the function in the AWS Management Console. * * Before running this Java code example, set up your development environment, including your credentials.
🌐
GitHub
github.com › alfonsof › aws-java-v2-examples
GitHub - alfonsof/aws-java-v2-examples: Java examples on AWS (Amazon Web Services) using AWS SDK for Java (SDK V2). How to manage EC2 instances, Lambda Functions, S3 buckets, etc.
If you have to use AWS SDK for Java (SDK V1) you have the Java code examples on AWS following this link: https://github.com/alfonsof/aws-java-examples/ You must have an AWS (Amazon Web Services) account.
Starred by 2 users
Forked by 4 users
Languages   Java 100.0% | Java 100.0%
🌐
AWS
docs.aws.amazon.com › aws sdk code examples › code library › code examples by sdk using aws sdks › code examples for sdk for java 2.x › lambda examples using sdk for java 2.x
Lambda examples using SDK for Java 2.x - AWS SDK Code Examples
JSONObject jsonObj = new JSONObject(); jsonObj.put("inputValue", "2000"); String json = jsonObj.toString(); SdkBytes payload = SdkBytes.fromUtf8String(json); InvokeRequest request = InvokeRequest.builder() .functionName(functionName) .payload(payload) .build(); res = awsLambda.invoke(request); String value = res.payload().asUtf8String(); System.out.println(value); } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } For API details, see Invoke in AWS SDK for Java 2.x API Reference. The following code example shows how to use UpdateFunctionCode. ... There's more on GitHub.
🌐
GitHub
github.com › aws › aws-sdk-java-v2 › blob › master › archetypes › archetype-lambda › README.md
aws-sdk-java-v2/archetypes/archetype-lambda/README.md at master · aws/aws-sdk-java-v2
mvn archetype:generate \ ... -DinteractiveMode=false ... Specifies the service client to be used in the lambda function, eg: s3, dynamodb. You can find available services here. ... Specifies the http client to be used by the SDK ...
Author   aws
🌐
GitHub
github.com › aws › aws-sdk-java › blob › master › aws-java-sdk-lambda › src › main › java › com › amazonaws › services › lambda › AWSLambda.java
aws-sdk-java/aws-java-sdk-lambda/src/main/java/com/amazonaws/services/lambda/AWSLambda.java at master · aws/aws-sdk-java
The official AWS SDK for Java 1.x (In Maintenance Mode, End-of-Life on 12/31/2025). The AWS SDK for Java 2.x is available here: https://github.com/aws/aws-sdk-java-v2/ - aws-sdk-java/aws-java-sdk-lambda/src/main/java/com/amazonaws/services/lambda/AWSLambda.java at master · aws/aws-sdk-java
Author   aws
🌐
AWS
docs.aws.amazon.com › aws sdk for java › developer guide for version 1.x › aws sdk for java code examples › lambda examples using the aws sdk for java
Lambda Examples Using the AWS SDK for Java - AWS SDK for Java 1.x
We recommend that you migrate to ... the AWS SDK for Java. The examples include only the code needed to demonstrate each technique. The complete example code is available on GitHub......
Top answer
1 of 1
8

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.

Find elsewhere
🌐
GitHub
github.com › aws › aws-lambda-java-libs
GitHub - aws/aws-lambda-java-libs: Official mirror for interface definitions and helper classes for Java code running on the AWS Lambda platform. · GitHub
This package provides helper classes/methods to use alongside aws-lambda-java-events in order to transform Lambda input event model objects into SDK-compatible output model objects.
Starred by 547 users
Forked by 240 users
Languages   C++ 51.2% | Java 46.2% | Shell 2.0% | CMake 0.3% | Makefile 0.2% | C 0.1%
🌐
AWS
docs.aws.amazon.com › aws sdk for java › developer guide for version 2.x › calling aws services from the aws sdk for java 2.x › invoke, list, and delete aws lambda functions
Invoke, list, and delete AWS Lambda functions - AWS SDK for Java 2.x
public static void invokeFunction(LambdaClient awsLambda, String functionName) { InvokeResponse res = null ; try { //Need a SdkBytes instance for the payload String json = "{\"Hello \":\"Paris\"}"; SdkBytes payload = SdkBytes.fromUtf8String(json) ; //Setup an InvokeRequest InvokeRequest request = InvokeRequest.builder() .functionName(functionName) .payload(payload) .build(); res = awsLambda.invoke(request); String value = res.payload().asUtf8String() ; System.out.println(value); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } ... objects. You can iterate through the list to retrieve information about the functions. For example, the following Java code example shows how to get each function name.
🌐
GitHub
github.com › petergu › aws-sqs-lambda-java
GitHub - petergu/aws-sqs-lambda-java: A showcase example that integrates AWS API Gateway, SQS and Lambda, written in Java 8, using Dagger 2 as dependency injection, Terraform as deployment tool.
A showcase example that integrates AWS API Gateway, SQS and Lambda, written in Java 8, using Dagger 2 as dependency injection, Terraform as deployment tool. - petergu/aws-sqs-lambda-java
Starred by 18 users
Forked by 10 users
Languages   Java 46.2% | HCL 45.9% | Groovy 7.9% | Java 46.2% | HCL 45.9% | Groovy 7.9%
🌐
AWS
aws.amazon.com › blogs › developer › tuning-the-aws-java-sdk-2-x-to-reduce-startup-time
Tuning the AWS Java SDK 2.x to reduce startup time | AWS Developer Tools Blog
June 26, 2020 - We also have a Maven archetype that allows you to create a Java Lambda function quickly with best practices incorporated. You can check out this blog post to learn more. With our blog posts, we will educate the AWS developer community on performance-related best practices and how to implement them in your code, For updates to the AWS SDK for Java libraries, check out the aws-java-sdk-v2 repo on GitHub and let us know if you have any feedback/comments/issues via the GitHub issues page.
🌐
GitHub
gist.github.com › MichaelHabermas › 83af63655d801667f6b4389df5c8fba2
aws_lambda_hello_world.md · GitHub
Choose 2 for application · Choose 3 for Java · Choose 1 for Groovy · Project name: hello-world-lambda · Source package: com.example.lambda · Replace the contents of build.gradle with: plugins { id 'java' } repositories { mavenCentral() } dependencies { implementation platform('com.amazonaws:aws-java-sdk-bom:1.12.261') implementation 'com.amazonaws:aws-lambda-java-core:1.2.2' implementation 'com.amazonaws:aws-lambda-java-events:3.11.0' implementation 'com.google.code.gson:gson:2.10.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' } java { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } test { useJUnitPlatform() } jar { manifest { attributes 'Main-Class': 'com.example.lambda.Handler' } from { configurations.runtimeClasspath.collect { it.isDirectory() ?
🌐
Medium
srilalitha1310.medium.com › invoking-an-aws-lambda-in-java-using-aws-sdk-v2-f074f495454a
Invoking an AWS lambda in Java using AWS SDK V2 | by Srilalitha S | Medium
October 27, 2020 - I was working on a task and met a need to invoke an other lambda from a java lambda. Although the documentations and examples from v1 were pretty helpful, I thought a proper step by step example for v2 would be a good to have. Hence, I am writing this. As first step, you will have to add the artifact dependency for lamdba from the sdk.
🌐
GitHub
github.com › aws › aws-lambda-java-libs › issues › 74
update aws lambda java libs to aws sdk java v2 · Issue #74 · aws/aws-lambda-java-libs
February 1, 2019 - update aws lambda java libs to aws sdk java v2#74 · Copy link · SeekerWing · opened · on Feb 1, 2019 · Issue body actions · update aws lambda java libs to aws sdk java v2 (https://github.com/aws/aws-sdk-java-v2) Reactions are currently unavailable · No one assigned ·
Author   SeekerWing
🌐
AWS re:Post
repost.aws › questions › QUya0hrXdeQZihiHFjLoDbyQ › java-2-x-lambda-requesthandler-and-context-documentation
Java 2.X Lambda RequestHandler and Context documentation | AWS re:Post
January 12, 2022 - Would anyone know where this documentation can be found and/or sample code for a Java 2.X lambda function? ... Have you tried this https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/usecases/creating_first_project
🌐
AWS
docs.aws.amazon.com › aws sdk for java › developer guide for version 1.x › aws sdk for java code examples › amazon swf examples using the aws sdk for java › lambda tasks
Lambda Tasks - AWS SDK for Java 1.x
When you receive a LambdaFunct... output of the Lambda function: ... You can browse the complete source :github:`<awsdocs/aws-java-developer-guide/tree/master/doc_source/snippets/helloswf_lambda/> for this example on Github in the aws-java-developer-guide reposit...
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with java › java sample applications for aws lambda
Java sample applications for AWS Lambda - AWS Lambda
Each sample application includes scripts for easy deployment and cleanup, an CloudFormation template, and supporting resources. ... – A Java function that demonstrates how you can use Lambda to process orders. This function illustrates how to define and deserialize a custom input event object, ...
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with java
Building Lambda functions with Java - AWS Lambda
Since Java is a compiled language, you can't view or edit the source code in the Lambda console, but you can modify its configuration, invoke it, and configure triggers. To get started with application development in your local environment, deploy one of the sample applications available in this guide's GitHub repository.
🌐
AWS
docs.aws.amazon.com › sdk-for-java › latest › developer-guide › examples.html
Code examples for the AWS SDK for Java 2.x - AWS SDK for Java 2.x
This section provides programming examples you can use with the AWS SDK for Java 2.x for specific features, use cases, and AWS services. Find the source code for these examples and others in the AWS documentation code examples repository on GitHub