Skip to content

Latest commit

 

History

History
83 lines (54 loc) · 5.43 KB

lambda-java.md

File metadata and controls

83 lines (54 loc) · 5.43 KB

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.

Java runtimes

Name Identifier JDK Operating system Architectures
Java 11 java11 amazon-corretto-11 Amazon Linux 2 x86_64, arm64
Java 8 java8.al2 amazon-corretto-8 Amazon Linux 2 x86_64, arm64
Java 8 java8 amazon-corretto-8 Amazon Linux x86_64

Lambda provides the following libraries for Java functions:

Lambda functions use an execution role to get permission to write logs to Amazon CloudWatch Logs, and to access other services and resources. If you don't already have an execution role for function development, create one.

To create an execution role

  1. Open the roles page in the IAM console.

  2. Choose Create role.

  3. Create a role with the following properties.

    • Trusted entityLambda.
    • PermissionsAWSLambdaBasicExecutionRole.
    • Role namelambda-role.

    The AWSLambdaBasicExecutionRole policy has the permissions that the function needs to write logs to CloudWatch Logs.

You can add permissions to the role later, or swap it out for a different role that's specific to a single function.

To create a Java function

  1. Open the Lambda console.

  2. Choose Create function.

  3. Configure the following settings:

    • Namemy-function.
    • RuntimeJava 11.
    • RoleChoose an existing role.
    • Existing rolelambda-role.
  4. Choose Create function.

  5. To configure a test event, choose Test.

  6. For Event name, enter test.

  7. Choose Save changes.

  8. 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