Building Lambda functions with Java
You can run Java code in Amazon Lambda. Lambda provides runtimes for Java that run your code to process events. Your code runs in an Amazon Linux environment that includes Amazon credentials from an Amazon Identity and Access Management (IAM) role that you manage.
Lambda supports the following Java runtimes.
Java | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Name | Identifier | Operating system | Architectures | Deprecation (Phase 1) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Java 17 |
|
Amazon Linux 2 |
x86_64, arm64 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Java 11 |
|
Amazon Linux 2 |
x86_64, arm64 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Java 8 |
|
Amazon Linux 2 |
x86_64, arm64 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Java 8 |
|
Amazon Linux |
x86_64 |
Dec 31, 2023 |
Lambda provides the following libraries for Java functions:
-
com.amazonaws:aws-lambda-java-core
(required) – Defines handler method interfaces and the context object that the runtime passes to the handler. If you define your own input types, this is the only library that you need. -
com.amazonaws:aws-lambda-java-events
– Input types for events from services that invoke Lambda functions. -
com.amazonaws:aws-lambda-java-log4j2
– An appender library for Apache Log4j 2 that you can use to add the request ID for the current invocation to your function logs. -
Amazon SDK for Java 2.0
– The official Amazon SDK for the Java programming language.
To create a Java function
-
Open the Lambda console
. -
Choose Create function.
-
Configure the following settings:
-
Function name: Enter a name for the function.
-
Runtime: Choose Java 17.
-
-
Choose Create function.
-
To configure a test event, choose Test.
-
For Event name, enter
test
. -
Choose Save changes.
-
To invoke the function, choose Test.
The console creates a Lambda function with a handler class named Hello
.
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.
Note
To get started with application development in your local environment, deploy one of the sample applications available in this guide's GitHub repository.
The Hello
class has a function named handleRequest
that takes an event object and
a context object. This is the handler function that Lambda calls when the
function is invoked. The Java function runtime gets invocation events from Lambda and passes them to the
handler. In the function configuration, the handler value is example.Hello::handleRequest
.
To update the function's code, you create a deployment package, which is a .zip file archive that contains your function code. As your function development progresses, you will want to store your function code in source control, add libraries, and automate deployments. Start by creating a deployment package and updating your code at the command line.
The function runtime passes a context object to the handler, in addition to the invocation event. The context object contains additional information about the invocation, the function, and the execution environment. More information is available from environment variables.
Your Lambda function comes with a CloudWatch Logs log group. The function runtime sends details about each invocation to CloudWatch Logs. It relays any logs that your function outputs during invocation. If your function returns an error, Lambda formats the error and returns it to the invoker.
Topics
- Amazon Lambda function handler in Java
- Deploy Java Lambda functions with .zip or JAR file archives
- Deploy Java Lambda functions with container images
- Java Lambda function customization settings
- Amazon Lambda context object in Java
- Amazon Lambda function logging in Java
- Amazon Lambda function errors in Java
- Instrumenting Java code in Amazon Lambda
- Creating a deployment package using Eclipse
- Java sample applications for Amazon Lambda