Building Lambda functions with Java
You can run Java code in AWS Lambda. Lambda provides runtimes for Java that run your code to process events. Your code runs in an Amazon Linux environment that includes AWS credentials from an AWS Identity and Access Management (IAM) role that you manage.
Lambda supports the following Java runtimes.
Name | Identifier | Operating system | Deprecation date | Block function create | Block function update |
---|---|---|---|---|---|
Java 21 |
|
Amazon Linux 2023 |
Not scheduled |
Not scheduled |
Not scheduled |
Java 17 |
|
Amazon Linux 2 |
Not scheduled |
Not scheduled |
Not scheduled |
Java 11 |
|
Amazon Linux 2 |
Not scheduled |
Not scheduled |
Not scheduled |
Java 8 |
|
Amazon Linux 2 |
Not scheduled |
Not scheduled |
Not scheduled |
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. -
AWS SDK for Java 2.0
– The official AWS SDK for the Java programming language.
Important
Don't use private components of the JDK API, such as private fields, methods, or classes. Non-public API components can change or be removed in any update, causing your application to break.
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 21.
-
-
Choose Create function.
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
- Define Lambda function handler in Java
- Deploy Java Lambda functions with .zip or JAR file archives
- Deploy Java Lambda functions with container images
- Working with layers for Java Lambda functions
- Customize serialization for Lambda Java functions
- Customize Java runtime startup behavior for Lambda functions
- Using the Lambda context object to retrieve Java function information
- Log and monitor Java Lambda functions
- Instrumenting Java code in AWS Lambda
- Java sample applications for AWS Lambda