aws-lambda-java-libs

repository·main·Indexed 20 days ago

https://github.com/aws/aws-lambda-java-libs

A collection of essential Java libraries for running, testing, and optimizing Java applications on AWS Lambda. Includes aws-lambda-java-events for service event POJOs, aws-lambda-java-events-sdk-transformer for converting DynamoDB events to SDK v1 and v2 models, aws-lambda-java-log4j2 for specialized Lambda logging, and the AWS Lambda Java Runtime Interface Client (RIC) for creating custom Docker images compatible with the Lambda Runtime API.

Tokens
17K
Snippets
50
Records
56
Agent score
69%

What's inside aws-lambda-java-libs

  1. What is the AWS Lambda Java Runtime Interface Client (RIC)?

    main

    The AWS Lambda Java Runtime Interface Client (RIC) is a lightweight interface that implements the Lambda Runtime API. It allows you to take a preferred base image (such as Amazon Linux, Alpine, Ubuntu, Debian, or CentOS) and make it compatible with AWS Lambda by enabling the runtime to receive requests from and send requests to the Lambda service.

    To use a base image with the RIC, the image must meet these requirements:

    • Built for x86_64 or ARM64 architectures.
    • Contains Java >= 8.
    • Contains glibc >= 2.17 or musl.
  2. Handle GLIBC compatibility and packaging

    main

    Because Lambda runs on Amazon Linux, your build environment's GLIBC version must be compatible. If you build on a different Linux distribution, you must package the C runtime library with your function.

    Option 1: Package the C runtime (Default)

    If you build on a distribution other than the one used by the target Lambda version, the runtime will automatically include the necessary C libraries in the zip file.

    Option 2: Skip packaging C runtime (NO_LIBC)

    If you are building on the same Amazon Linux version used by Lambda, you can reduce the package size by passing the NO_LIBC flag to aws_lambda_package_target in CMake:

    aws_lambda_package_target(${PROJECT_NAME} NO_LIBC)

    Important Packaging Notes

    • Dynamic Dependencies: Any library loaded via dlopen is NOT automatically packaged. You must add these manually to your zip file.
    • TLS/HTTPS: If making HTTPS calls, ensure you point to the correct CA bundle location for your environment. For example, using libcurl:
      curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "/etc/pki/tls/certs/ca-bundle.crt");
  3. Build and package the API Gateway Lambda application

    main

    Build the Lambda function defined in main.cpp. You must provide the CMAKE_PREFIX_PATH pointing to your installation directory so CMake can find the SDK and Runtime.

    After building, run the aws-lambda-package-api target to generate a deployment zip file named api.zip.

    $ mkdir build
    $ cd build
    $ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=~/install
    $ make
    $ make aws-lambda-package-api
  4. Quick Start with the Lambda Profiler Extension

    main

    To quickly set up the profiler using the provided scripts (MacOS/Linux), follow these steps to build the extension, create an S3 bucket, and deploy a Lambda layer for a specific function.

    Note: This is an alpha release and is currently tested with managed runtimes for Java 17 and Java 21.

    # 1. Clone the repository
    git clone https://github.com/aws/aws-lambda-java-libs
    
    # 2. Build the extension layer
    cd aws-lambda-java-libs/experimental/aws-lambda-java-profiler/extension
    ./build_layer.sh
    
    # 3. Run the update script to create S3 bucket, layer, and configuration
    cd ..
    ./update-function.sh YOUR_FUNCTION_NAME
  5. Build the AWS C++ SDK from source

    main

    To use the AWS C++ SDK for parsing requests and writing responses in your Lambda function, you must build it from source. This example builds only the core component to minimize dependencies.

    Ensure you set the CMAKE_INSTALL_PREFIX to your desired installation directory (e.g., ~/install).

    $ mkdir ~/install
    $ git clone https://github.com/aws/aws-sdk-cpp.git
    $ cd aws-sdk-cpp
    $ mkdir build
    $ cd build
    $ cmake .. -DBUILD_ONLY="core" \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DENABLE_UNITY_BUILD=ON \
      -DCUSTOM_MEMORY_MANAGEMENT=OFF \
      -DCMAKE_INSTALL_PREFIX=~/install \
      -DENABLE_UNITY_BUILD=ON
    $ make
    $ make install
  6. Implement AWS Lambda request handlers with aws-lambda-java-core

    main

    The aws-lambda-java-core library provides the essential interfaces and the Context object required to write Lambda functions in Java. You can implement handlers using two primary patterns:

    1. RequestHandler: Used for standard request/response patterns where you define the input and output types.
    2. RequestStreamHandler: Used when you need direct access to the raw InputStream and OutputStream (e.g., for custom serialization or handling large payloads).

    To use this, add the following dependency to your pom.xml:

    // Example RequestHandler
    public class Handler implements RequestHandler<Map<String, String>, String>{
     @Override
     public String handleRequest(Map<String, String> event, Context context) {
         // implementation
     }
    }
    
    // Example RequestStreamHandler
    public class HandlerStream implements RequestStreamHandler {
      @Override
      public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
          // implementation
      }
    }
    <dependency>
     <groupId>com.amazonaws</groupId>
     <artifactId>aws-lambda-java-core</artifactId>
     <version>1.3.0</version>
    </dependency>
  7. Build the AWS C++ SDK for S3

    main

    To use S3 features within a C++ Lambda function, you must build the AWS C++ SDK from source. This example demonstrates building only the S3 component to minimize build time and size. Ensure you set the CMAKE_INSTALL_PREFIX to a directory of your choice to manage the installation.

    $ mkdir ~/install
    $ git clone https://github.com/aws/aws-sdk-cpp.git
    $ cd aws-sdk-cpp
    $ mkdir build
    $ cd build
    $ cmake .. -DBUILD_ONLY="s3" \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DCUSTOM_MEMORY_MANAGEMENT=OFF \
      -DCMAKE_INSTALL_PREFIX=~/install \
      -DENABLE_UNITY_BUILD=ON
    
    $ make
    $ make install
  8. Configure Log4J2 logging for AWS Lambda

    main

    To use Log4J version 2 for logging within your Lambda functions, use the aws-lambda-java-log4j2 adapter. This ensures that logs are correctly formatted and integrated with the Lambda environment.

    <dependency>
     <groupId>com.amazonaws</groupId>
     <artifactId>aws-lambda-java-log4j2</artifactId>
     <version>1.6.0</version>
    </dependency>
  9. Build and test the Gson custom serialization sample

    main

    To build and test the Gson custom serialization sample project, use the AWS SAM (Serverless Application Model) CLI. This allows you to build the project dependencies and invoke the Lambda function locally using a sample event file.

    Ensure you have the AWS SAM CLI installed and configured before running these commands.

    sam build
    sam local invoke -e events/event.json
  10. Build and test the Jackson-jr custom serialization sample

    main

    To build and test the custom serialization sample using Jackson-jr, use the AWS SAM CLI. This allows you to build the application and invoke the Lambda function locally using a sample event file.

    1. Build the application: Use sam build to compile the project and prepare the deployment artifacts.
    2. Invoke locally: Use sam local invoke with the -e flag to simulate a Lambda invocation using the provided events/event.json file.
    sam build
    sam local invoke -e events/event.json
  11. Install the AWS Lambda Java Events SDK Transformer

    main

    To use this library, you must add the transformer and the aws-lambda-java-events library to your Maven pom.xml. Depending on whether you are using the AWS DynamoDB Java SDK v1 or v2, you must also include the corresponding SDK dependency.

    <!-- Core dependencies -->
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events-sdk-transformer</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>3.11.2</version>
        </dependency>
    </dependencies>
    
    <!-- For DynamoDB SDK v2 support -->
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>dynamodb</artifactId>
        <version>2.15.40</version>
    </dependency>
    
    <!-- OR for DynamoDB SDK v1 support -->
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-dynamodb</artifactId>
        <version>1.11.914</version>
    </dependency>