### Install AWS Lambda Runtime Interface Emulator (RIE) Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md Installs the RIE on your local machine for testing container image Lambda functions. Ensure you have curl and chmod available. ```shell mkdir -p ~/.aws-lambda-rie && \ curl -Lo ~/.aws-lambda-rie/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \ chmod +x ~/.aws-lambda-rie/aws-lambda-rie ``` -------------------------------- ### Build and Install AWS Lambda C++ Runtime Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md Commands to clone the repository, build, and install the runtime library on a GNU/Linux system. Ensure CMake version 3.9 or later is installed. ```bash git clone https://github.com/awslabs/aws-lambda-cpp.git cd aws-lambda-cpp mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/lambda-install make && make install ``` -------------------------------- ### Load SNSEvent using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Illustrates loading an SNSEvent from a JSON file using the EventLoader utility. This is beneficial for custom test setups. ```java SNSEvent snsEvent = EventLoader.loadSNSEvent("sns_event.json"); ``` -------------------------------- ### Build Lambda Application Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/s3/README.md Compiles the Lambda function written in C++ and packages it into a zip file ready for deployment. It uses CMake to configure the build and specifies the installation prefix for dependencies. ```bash cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=~/install make make aws-lambda-package-encoder ``` -------------------------------- ### Build AWS C++ SDK Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/api-gateway/README.md Builds the AWS C++ SDK from source, focusing on the 'core' component and disabling shared libraries for a static build. Installs the SDK to a specified prefix. ```bash 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 ``` -------------------------------- ### Build Lambda Application Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/dynamodb/README.md Compiles the Lambda function code located in main.cpp and packages it into a deployable zip file. It uses CMake to configure the build, referencing the previously installed SDK and runtime. ```bash $ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=~/install $ make $ make aws-lambda-package-ddb-demo ``` -------------------------------- ### Build Lambda Application and Package Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/api-gateway/README.md Compiles the C++ Lambda function and packages it for deployment. This involves CMake configuration using the installed SDK and runtime, followed by a make command to create the deployment package. ```bash mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=~/install make make aws-lambda-package-api ``` -------------------------------- ### CMakeLists.txt for AWS Lambda C++ Example Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/api-gateway/CMakeLists.txt This CMakeLists.txt file configures a C++ project for AWS Lambda. It specifies the minimum CMake version, C++ standard, project name, and finds the necessary AWS Lambda runtime and SDK components. It then adds an executable target, links the required libraries, and packages the target for Lambda deployment. ```cmake cmake_minimum_required(VERSION 3.5) set(CMAKE_CXX_STANDARD 11) project(api LANGUAGES CXX) find_package(aws-lambda-runtime REQUIRED) find_package(AWSSDK COMPONENTS core) add_executable(${PROJECT_NAME} "main.cpp") target_link_libraries(${PROJECT_NAME} PUBLIC AWS::aws-lambda-runtime ${AWSSDK_LINK_LIBRARIES}) aws_lambda_package_target(${PROJECT_NAME}) ``` -------------------------------- ### Java Lambda Handler Example Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md A simple Java class demonstrating a Lambda function handler. This class provides a 'sayHello' method that returns a greeting string. ```java package example; public class App { public static String sayHello() { return "Hello λ!"; } } ``` -------------------------------- ### Run Automated Local Tests Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md Executes automated tests for the runtime interface client, including RIE setup, dependency management, and Docker orchestration. Ensure prerequisites like Maven and Docker are met. ```shell make test-rie ``` -------------------------------- ### Build C++ Lambda Runtime Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/dynamodb/README.md Clones and builds the AWS C++ Lambda runtime, configuring it for release and specifying an installation prefix. This runtime is necessary for deploying C++ Lambda functions. ```bash $ git clone https://github.com/awslabs/aws-lambda-cpp-runtime.git $ cd aws-lambda-cpp-runtime $ mkdir build $ cd build $ cmake .. -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -DCMAKE_INSTALL_PREFIX=~/install $ make $ make install ``` -------------------------------- ### Load APIGatewayProxyRequestEvent using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Demonstrates using EventLoader to manually load an APIGatewayProxyRequestEvent from a JSON file. Useful for custom test setups or when annotations are not used. ```java APIGatewayProxyRequestEvent restEvent = EventLoader.loadApiGatewayRestEvent("apigw_rest_event.json"); ``` -------------------------------- ### Build AWS C++ SDK Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/dynamodb/README.md Builds the AWS C++ SDK, specifying only the DynamoDB module and configuring build options for release, shared libraries, unity build, and installation prefix. ```bash $ mkdir ~/install $ git clone https://github.com/aws/aws-sdk-cpp.git $ cd aws-sdk-cpp $ mkdir build $ cd build $ cmake .. -DBUILD_ONLY="dynamodb" \ -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 -j 4 $ make install ``` -------------------------------- ### Run Lambda Container Image with RIE Locally Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md Runs your Lambda function packaged as a container image locally using Docker and the RIE. This command starts an endpoint at http://localhost:9000/2015-03-31/functions/function/invocations. ```shell docker run -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \ --entrypoint /aws-lambda/aws-lambda-rie \ myfunction:latest \ /usr/bin/java -cp './*' com.amazonaws.services.lambda.runtime.api.client.AWSLambda example.App::sayHello ``` -------------------------------- ### Build AWS C++ SDK Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/s3/README.md Builds the AWS C++ SDK, focusing only on the S3 module, with specific configurations for release type, shared libraries, memory management, installation prefix, and unity build. ```bash 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 ``` -------------------------------- ### Integrate AWS Lambda Runtime with CMake Project Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md CMake configuration to find and link the aws-lambda-runtime library to your executable. This setup is for projects that also use CMake. ```cmake cmake_minimum_required(VERSION 3.9) set(CMAKE_CXX_STANDARD 11) project(demo LANGUAGES CXX) find_package(aws-lambda-runtime) add_executable(${PROJECT_NAME} "main.cpp") target_link_libraries(${PROJECT_NAME} PRIVATE AWS::aws-lambda-runtime) target_compile_features(${PROJECT_NAME} PRIVATE "cxx_std_11") target_compile_options(${PROJECT_NAME} PRIVATE "-Wall" "-Wextra") # this line creates a target that packages your binary and zips it up aws_lambda_package_target(${PROJECT_NAME}) ``` -------------------------------- ### Build AWS C++ Lambda Runtime Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/api-gateway/README.md Clones and builds the AWS C++ Lambda runtime. This process involves CMake configuration for a Release build with static libraries and installation to a specified prefix. ```bash git clone https://github.com/awslabs/aws-lambda-cpp-runtime.git cd aws-lambda-cpp-runtime mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -DCMAKE_INSTALL_PREFIX=~/install make make install ``` -------------------------------- ### Log4j2 Configuration for AWS Lambda Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-log4j2/README.md Configure log4j2.xml to define appenders and loggers. This example sets up a Lambda appender that can format logs as TEXT or JSON based on the AWS_LAMBDA_LOG_FORMAT environment variable. ```xml %d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n ``` -------------------------------- ### Implement Java Lambda Request Handler Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md Use this interface for Lambda functions that process simple request/response models. No specific setup is required beyond including the aws-lambda-java-core dependency. ```java public class Handler implements RequestHandler, String>{ @Override public String handleRequest(Map event, Context context) { } } ``` -------------------------------- ### Test API Gateway Endpoint with cURL Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/examples/api-gateway/README.md Tests the deployed Lambda function via an API Gateway endpoint using cURL. This example demonstrates sending a POST request with custom headers and a JSON payload. ```bash curl -v -X POST \ '?name=Bradley&city=Chicago' \ -H 'content-type: application/json' \ -H 'day: Sunday' \ -d '{ "time": "evening" }' ``` -------------------------------- ### Build the Lambda Profiler Extension Layer Source: https://github.com/aws/aws-lambda-java-libs/blob/main/experimental/aws-lambda-java-profiler/README.md Navigate to the extension directory and execute the build script to create the Lambda layer. ```bash cd aws-lambda-java-libs/experimental/aws-lambda-java-profiler/extension ./build_layer.sh ``` -------------------------------- ### Deploy the Profiler Extension with Update Script Source: https://github.com/aws/aws-lambda-java-libs/blob/main/experimental/aws-lambda-java-profiler/README.md Run the provided script to create an S3 bucket, Lambda layer, and configure your function for profiling. Replace YOUR_FUNCTION_NAME with your actual Lambda function name. ```bash cd .. ./update-function.sh YOUR_FUNCTION_NAME ``` -------------------------------- ### Dockerfile for Lambda with Java Runtime Interface Client Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md This Dockerfile demonstrates how to build a Lambda-compatible Java application using Amazon Linux 2 and Corretto 11. It includes steps for dependency management, compilation, and packaging the application with the RIC. ```dockerfile # we'll use Amazon Linux 2 + Corretto 11 as our base FROM public.ecr.aws/amazoncorretto/amazoncorretto:11 as base # configure the build environment FROM base as build RUN yum install -y maven WORKDIR /src # cache and copy dependencies ADD pom.xml . RUN mvn dependency:go-offline dependency:copy-dependencies # compile the function ADD . . RUN mvn package # copy the function artifact and dependencies onto a clean base FROM base WORKDIR /function COPY --from=build /src/target/dependency/*.jar ./ COPY --from=build /src/target/*.jar ./ # configure the runtime startup as main ENTRYPOINT [ "/usr/bin/java", "-cp", "./*", "com.amazonaws.services.lambda.runtime.api.client.AWSLambda" ] # pass the name of the function handler as an argument to the runtime CMD [ "example.App::sayHello" ] ``` -------------------------------- ### Package Lambda Application with CMake Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md Commands to build your Lambda application and create a zip archive using CMake. This includes building the executable and running the packaging target. ```bash mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=~/lambda-install make make aws-lambda-package-demo ``` -------------------------------- ### Specify Native Library Location via System Property Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md Configure the Java entrypoint to specify the location of the native client library using the com.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI system property. This is useful if you extract the .so files during build time. ```bash ENTRYPOINT [ "/usr/bin/java", "-Dcom.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI=/function/libaws-lambda-jni.linux_x86_64.so" "-cp", "./*", "com.amazonaws.services.lambda.runtime.api.client.AWSLambda" ] ``` -------------------------------- ### Run Docker Container for Lambda Development Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md Launch an Amazon Linux 2017.03 Docker container to replicate the Lambda execution environment for local testing and debugging. The `seccomp` option is required for tools like gdb and strace. ```bash $docker run -v /tmp:/tmp -it --security-opt seccomp=unconfined amazonlinux:2017.03 ``` -------------------------------- ### Load SQSEvent using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Demonstrates loading an SQSEvent from a JSON file using EventLoader. This provides an alternative to annotation-based injection for specific testing needs. ```java SQSEvent sqsEvent = EventLoader.loadSQSEvent("sqs_event.json"); ``` -------------------------------- ### Optional Environment Variables for Lambda Profiler Source: https://github.com/aws/aws-lambda-java-libs/blob/main/experimental/aws-lambda-java-profiler/README.md Customize the profiler's behavior using these optional environment variables. Options include controlling the start/stop commands, enabling debug logging, and setting the communication port. ```text AWS_LAMBDA_PROFILER_START_COMMAND | start,event=wall,interval=1us AWS_LAMBDA_PROFILER_STOP_COMMAND | stop,file=%s,include=*AWSLambda.main,include=start_thread AWS_LAMBDA_PROFILER_DEBUG | false AWS_LAMBDA_PROFILER_COMMUNICATION_PORT | 1234 ``` -------------------------------- ### Clone the AWS Lambda Java Libs Repository Source: https://github.com/aws/aws-lambda-java-libs/blob/main/experimental/aws-lambda-java-profiler/README.md Clone the repository containing the Lambda profiler extension and other Java libraries. ```bash git clone https://github.com/aws/aws-lambda-java-libs ``` -------------------------------- ### Upload Lambda Zip to S3 via AWS CLI Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md Use this command to upload your Lambda deployment package to an S3 bucket before creating the function. Ensure the S3 bucket region matches your Lambda function's region. ```bash $ aws s3 cp demo.zip s3://mys3bucket/demo.zip ``` -------------------------------- ### Load KinesisEvent using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Illustrates loading a KinesisEvent from a JSON file using the EventLoader utility. This is helpful for scenarios requiring direct event loading. ```java KinesisEvent kinesisEvent = EventLoader.loadKinesisEvent("kinesis_event.json"); ``` -------------------------------- ### Create Lambda Function with S3 Code Location via AWS CLI Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md After uploading the zip file to S3, use this command to create your Lambda function, specifying the S3 bucket and key for the code. ```bash $ aws lambda create-function --function-name demo \ --role \ --runtime provided --timeout 15 --memory-size 128 \ --handler demo --code "S3Bucket=mys3bucket,S3Key=demo.zip" ``` -------------------------------- ### Load APIGatewayV2HTTPEvent using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Demonstrates using EventLoader to manually load an APIGatewayV2HTTPEvent from a JSON file. This is an alternative to using annotations when direct control is needed. ```java APIGatewayV2HTTPEvent httpEvent = EventLoader.loadApiGatewayHttpEvent("apigw_http_event.json"); ``` -------------------------------- ### Configure CA Bundle for libcurl Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md If using libcurl directly for HTTPS requests, set the CURLOPT_CAINFO option to the correct CA bundle file path to resolve TLS connection issues. ```c curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "/etc/pki/tls/certs/ca-bundle.crt"); ``` -------------------------------- ### Configure JFR Output for AWS Lambda Java Profiler Source: https://github.com/aws/aws-lambda-java-libs/blob/main/experimental/aws-lambda-java-profiler/README.md Set the AWS_LAMBDA_PROFILER_START_COMMAND environment variable to output profiling data in JFR format to a specified file. This overrides the default HTML output. ```bash AWS_LAMBDA_PROFILER_START_COMMAND="start,event=wall,interval=1us,file=/tmp/profile.jfr" AWS_LAMBDA_PROFILER_STOP_COMMAND="stop,file=%s" ``` -------------------------------- ### Create Lambda Function via AWS CLI Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md AWS CLI command to create a Lambda function named 'demo'. It specifies the runtime, handler, role ARN, timeout, memory size, and the deployment package zip file. ```bash aws lambda create-function --function-name demo \ --role \ --runtime provided --timeout 15 --memory-size 128 \ --handler demo --zip-file fileb://demo.zip ``` -------------------------------- ### Configure Maven Surefire Plugin Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Ensure the Maven Surefire plugin is configured in your pom.xml to facilitate running tests. ```xml org.apache.maven.plugins maven-surefire-plugin 2.22.2 ``` -------------------------------- ### Load S3Event using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Shows how to use EventLoader to load an S3Event from a JSON file. This method allows for direct control over event loading in tests. ```java S3Event s3Event = EventLoader.loadS3Event("s3_event.json"); ``` -------------------------------- ### Invoke Lambda Function via AWS CLI Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md AWS CLI command to invoke the 'demo' Lambda function with a sample JSON payload and save the output to 'output.txt'. ```bash aws lambda invoke --function-name demo --payload '{"answer":42}' output.txt ``` -------------------------------- ### Maven Shade Plugin Configuration for Log4j2 Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-log4j2/README.md Configure the Maven Shade Plugin to include the Log4j2Plugins.dat file. This is necessary for the shade plugin to correctly package log4j2 plugins. ```xml ... org.apache.maven.plugins maven-shade-plugin 3.6.1 package shade com.github.edwgiz maven-shade-plugin.log4j2-cachefile-transformer 2.8.1 ... ``` -------------------------------- ### Attach Basic Execution Role Policy Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md AWS CLI command to attach the AWSLambdaBasicExecutionRole policy to the 'lambda-demo' IAM role. This allows the Lambda function to write logs to CloudWatch. ```bash aws iam attach-role-policy --role-name lambda-demo --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole ``` -------------------------------- ### Required Environment Variables for Lambda Profiler Source: https://github.com/aws/aws-lambda-java-libs/blob/main/experimental/aws-lambda-java-profiler/README.md Configure these environment variables for the Lambda profiler extension to function correctly. AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME specifies the S3 bucket for results, and JAVA_TOOL_OPTIONS is used to attach the Java agent. ```text AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME | Your unique bucket name JAVA_TOOL_OPTIONS | -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -javaagent:/opt/profiler-extension.jar ``` -------------------------------- ### Load DynamodbEvent using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Shows how to use EventLoader to load a DynamodbEvent from a JSON file. This method provides flexibility for loading events outside of the parameterized test framework. ```java DynamodbEvent ddbEvent = EventLoader.loadDynamoDbEvent("ddb_event.json"); ``` -------------------------------- ### Load ScheduledEvent using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Demonstrates loading an EventBridge ScheduledEvent from a JSON file using EventLoader. This approach is useful for custom test configurations. ```java ScheduledEvent eventBridgeEvent = EventLoader.loadScheduledEvent("eb_event.json"); ``` -------------------------------- ### Add Runtime Interface Client to Java Lambda Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md Add this Maven dependency to your Java project to utilize the Lambda Runtime Interface Client. This is essential for deploying applications as container images. ```xml com.amazonaws aws-lambda-java-runtime-interface-client 2.10.1 ``` -------------------------------- ### Configure CA Bundle for AWS C++ SDK Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md When making HTTPS calls with the AWS C++ SDK, explicitly set the CA bundle file path to ensure TLS connections succeed across different Linux distributions. ```cpp Aws::Client::ClientConfiguration config; config.caFile = "/etc/pki/tls/certs/ca-bundle.crt"; ``` -------------------------------- ### Gradle Shadow Plugin Configuration for Log4j2 Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-log4j2/README.md Configure the Gradle Shadow plugin to transform Log4j2 plugins. This ensures that log4j2 plugins are correctly included in the shadow JAR. ```groovy dependencies{ ... implementation group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: '1.6.4' implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4jVersion implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion } jar { enabled = false } shadowJar { transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer) } build.dependsOn(shadowJar) ``` -------------------------------- ### Add Maven Dependencies for SDK Transformer Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md Include these dependencies in your pom.xml to use the aws-lambda-java-events-sdk-transformer library. ```xml com.amazonaws aws-lambda-java-events-sdk-transformer 3.1.0 com.amazonaws aws-lambda-java-events 3.11.2 ``` -------------------------------- ### Maven POM for Lambda Java Runtime Interface Client Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md This pom.xml file configures a Maven project to include the aws-lambda-java-runtime-interface-client dependency. It also sets up the maven-dependency-plugin to copy dependencies during the package phase. ```xml 4.0.0 example hello-lambda jar 1.0-SNAPSHOT hello-lambda http://maven.apache.org 1.8 1.8 com.amazonaws aws-lambda-java-runtime-interface-client 2.11.0 org.apache.maven.plugins maven-dependency-plugin 3.1.2 copy-dependencies package copy-dependencies ``` -------------------------------- ### Create IAM Role for Lambda Function Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md AWS CLI command to create an IAM role named 'lambda-demo' using the specified trust policy JSON file. Ensure you have the AWS CLI configured. ```bash aws iam create-role --role-name lambda-demo --assume-role-policy-document file://trust-policy.json ``` -------------------------------- ### Load Generic Event using EventLoader Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Shows the generic method in EventLoader to load any custom event type from a JSON file by specifying the class. This offers maximum flexibility for custom event structures. ```java MyEvent myEvent = EventLoader.loadEvent("my_event.json", MyEvent.class); ``` -------------------------------- ### Inject Multiple SQSEvents with @Events (List) Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Use the @Events annotation with a list of @Event annotations to inject multiple SQS events. The JSON files must be in the classpath. Requires JUnit 5's @ParameterizedTest. ```java @ParameterizedTest @Events( events = { @Event("sqs/sqs_event.json"), @Event("sqs/sqs_event2.json"), }, type = SQSEvent.class ) public void testInjectEvents(SQSEvent event) { // test your handleRequest method with all the JSON events available in the sqs folder } ``` -------------------------------- ### Convert full DynamodbEvent to SDK v1 List Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md This snippet shows how to convert an entire `DynamodbEvent` object into a `List` compatible with AWS SDK v1. This is useful when you need to process multiple records from a DynamoDB stream event using SDK v1 constructs. ```APIDOC ## Convert full DynamodbEvent to SDK v1 List ### Description Converts a `DynamodbEvent` object to a `List` compatible with AWS SDK v1. ### Method Signature `DynamodbEventTransformer.toRecordsV1(DynamodbEvent ddbEvent)` ### Parameters - **ddbEvent** (`DynamodbEvent`) - Required - The input DynamoDB event object. ### Return Value - `List` - A list of records compatible with AWS SDK v1. ``` -------------------------------- ### Java Log4j2 Logging in AWS Lambda Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-log4j2/README.md This snippet shows how to initialize and use Log4j2 for logging within a Java AWS Lambda handler. It contrasts Log4j2's single-event logging with System.out/err's multi-event logging for multi-line output. ```java package example; import com.amazonaws.services.lambda.runtime.Context; import static org.apache.logging.log4j.CloseableThreadContext.put; import org.apache.logging.log4j.CloseableThreadContext.Instance; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Hello { // Initialize the Log4j logger. static final Logger logger = LogManager.getLogger(Hello.class); public String myHandler(String name, Context context) { // System.out: One log statement but with a line break (AWS Lambda writes two events to CloudWatch). System.out.println("log data from stdout \n this is continuation of system.out"); // System.err: One log statement but with a line break (AWS Lambda writes two events to CloudWatch). System.err.println("log data from stderr. \n this is a continuation of system.err"); // Use log4j to log the same thing as above and AWS Lambda will log only one event in CloudWatch. logger.debug("log data from log4j debug \n this is continuation of log4j debug"); logger.error("log data from log4j err. \n this is a continuation of log4j.err"); // When logging in JSON, you can also add custom fields // In java11+ you can use the `try (var ctx = put("name", name)) {}` structure Instance ctx = put("name", name); logger.info("log line with input name"); ctx.close(); // Return will include the log stream name so you can look // up the log later. return String.format("Hello %s. log stream = %s", name, context.getLogStreamName()); } } ``` -------------------------------- ### Sample Lambda Handler Function in C++ Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md A basic C++ handler function for AWS Lambda that processes an invocation request. It demonstrates returning a success or failure response based on payload length. ```cpp #include using namespace aws::lambda_runtime; static invocation_response my_handler(invocation_request const& req) { if (req.payload.length() > 42) { return invocation_response::failure("error message here"/*error_message*/, "error type here" /*error_type*/); } return invocation_response::success("json payload here" /*payload*/, "application/json" /*MIME type*/); } int main() { run_handler(my_handler); return 0; } ``` -------------------------------- ### Inject Multiple SQSEvents with @Events (Folder) Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Use the @Events annotation with the 'folder' attribute to inject all JSON events from a specified folder. The folder must be in the classpath. Requires JUnit 5's @ParameterizedTest. ```java // sqs folder must be in the classpath (most often in src/test/resources) @ParameterizedTest @Events(folder = "sqs", type = SQSEvent.class) public void testInjectEventsFromFolder(SQSEvent event) { // test your handleRequest method with all the JSON events available in the sqs folder } ``` -------------------------------- ### Add Log4J2 Support to Java Lambda Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md Include this Maven dependency to enable Log4J2 logging in your Java Lambda functions. Ensure you are using a compatible version. ```xml com.amazonaws aws-lambda-java-log4j2 1.6.0 ``` -------------------------------- ### Add aws-lambda-java-tests Dependency Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Include this dependency in your project's pom.xml to use the testing utilities. It is a test dependency. ```xml com.amazonaws aws-lambda-java-tests 1.1.1 test ``` -------------------------------- ### Add aws-lambda-java-events-sdk-transformer Dependency Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md Include this Maven dependency to add helper classes for transforming Lambda event model objects into SDK-compatible output model objects, used in conjunction with aws-lambda-java-events. ```xml com.amazonaws aws-lambda-java-events-sdk-transformer 3.1.0 ``` -------------------------------- ### Default AWS Lambda Java Profiler Configuration Source: https://github.com/aws/aws-lambda-java-libs/blob/main/experimental/aws-lambda-java-profiler/README.md These are the default environment variables for the AWS Lambda Java Profiler extension. They specify the profiling event, interval, and include/exclude patterns for the stop command. ```bash AWS_LAMBDA_PROFILER_START_COMMAND="start,event=wall,interval=1us" AWS_LAMBDA_PROFILER_STOP_COMMAND="stop,file=%s,include=*AWSLambda.main,include=start_thread" ``` -------------------------------- ### Create IAM Trust Policy for Lambda Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md JSON content for an IAM trust policy that allows the Lambda service to assume the role. This is a prerequisite for creating an IAM role for your Lambda function. ```json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": ["lambda.amazonaws.com"] }, "Action": "sts:AssumeRole" } ] } ``` -------------------------------- ### Convert DynamodbStreamRecord to SDK v2 Record Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md Use DynamodbRecordTransformer.toRecordV2 to convert a single DynamodbEvent.DynamodbStreamRecord to an SDK v2 compatible Record. ```java import com.amazonaws.services.lambda.runtime.events.transformers.v2.dynamodb.DynamodbRecordTransformer; public class MyClass { public void myMethod(DynamodbEvent.DynamodbStreamRecord record) { // ... Record convertedRecord = DynamodbRecordTransformer.toRecordV2(record); // ... ``` -------------------------------- ### Platform-Specific Linux x86_64 JAR Dependency Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md Use this Maven dependency configuration to include a platform-specific JAR for Linux x86_64, reducing the overall size if the full uber JAR is not needed. ```xml com.amazonaws aws-lambda-java-runtime-interface-client 2.11.0 linux-x86_64 ``` -------------------------------- ### Add Maven Dependency for DynamoDB SDK v1 Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md If transforming events for DynamoDB SDK v1, add this dependency to your pom.xml instead. ```xml com.amazonaws aws-java-sdk-dynamodb 1.11.914 ``` -------------------------------- ### Add aws-lambda-java-events Dependency Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md Include this Maven dependency to utilize Java objects representing various AWS Lambda event sources, such as SQS, S3, and API Gateway. ```xml com.amazonaws aws-lambda-java-events 3.16.0 ``` -------------------------------- ### Test Multiple Events and Responses from Folders with @HandlerParams Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Use @HandlerParams with @Events(folder=...) and @Responses(folder=...) to test handler methods with multiple events and their corresponding responses loaded from directories. Requires JUnit 5's @ParameterizedTest. ```java // Multiple events in folder @ParameterizedTest @HandlerParams( events = @Events(folder = "apigw/events/", type = APIGatewayProxyRequestEvent.class), responses = @Responses(folder = "apigw/responses/", type = APIGatewayProxyResponseEvent.class)) public void testMultipleEventsResponsesInFolder(APIGatewayProxyRequestEvent event, APIGatewayProxyResponseEvent response) { } ``` -------------------------------- ### Convert single DynamodbStreamRecord to SDK v1 Record Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md This snippet demonstrates converting a single `DynamodbEvent.DynamodbStreamRecord` object to an SDK v1 compatible `Record`. This is useful for processing individual records when the full event object is not needed or when working with specific record transformations. ```APIDOC ## Convert single DynamodbStreamRecord to SDK v1 Record ### Description Converts a single `DynamodbEvent.DynamodbStreamRecord` object to an AWS SDK v1 `Record`. ### Method Signature `DynamodbRecordTransformer.toRecordV1(DynamodbEvent.DynamodbStreamRecord record)` ### Parameters - **record** (`DynamodbEvent.DynamodbStreamRecord`) - Required - The input DynamoDB stream record. ### Return Value - `Record` - An SDK v1 compatible `Record` object. ``` -------------------------------- ### Add aws-lambda-java-tests Dependency Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md Include this Maven dependency for utilities that simplify testing Java Lambda functions with JUnit, including event injection. ```xml com.amazonaws aws-lambda-java-tests 1.1.1 test ``` -------------------------------- ### Package Target with NO_LIBC Flag in CMake Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/src/main/jni/deps/aws-lambda-cpp-0.2.7/README.md Use this CMake command to package your project without including the C runtime library. This is an optimization applicable when building on an Amazon Linux version compatible with the Lambda execution environment. ```cmake aws_lambda_package_target(${PROJECT_NAME} NO_LIBC) ``` -------------------------------- ### Convert DynamodbEvent to SDK v2 Records Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md Use DynamodbEventTransformer.toRecordsV2 to convert a full DynamodbEvent to a List of SDK v2 compatible Records. ```java import com.amazonaws.services.lambda.runtime.events.transformers.v2.DynamodbEventTransformer; public class DDBEventProcessor implements RequestHandler { public String handleRequest(DynamodbEvent ddbEvent, Context context) { // Process input event List convertedRecords = DynamodbEventTransformer.toRecordsV2(ddbEvent); // Modify records as needed and write back to DynamoDB using the DynamoDB AWS SDK for Java 2.0 } ``` -------------------------------- ### Add Lambda Java Serialization Support Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md This Maven dependency provides serialization logic used by the `aws-lambda-java-runtime-client`. It does not have standalone usage. ```xml com.amazonaws aws-lambda-java-serialization 1.1.5 ``` -------------------------------- ### Invoke Lambda Function via RIE Endpoint Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md Posts an event to the locally running RIE endpoint to invoke your Lambda function. The event payload is provided as data in the POST request. ```shell curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' ``` -------------------------------- ### Add Maven Dependency for DynamoDB SDK v2 Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md If transforming events for DynamoDB SDK v2, add this dependency to your pom.xml. ```xml software.amazon.awssdk dynamodb 2.15.40 ``` -------------------------------- ### Convert Identity to SDK v1 Identity Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md This snippet shows how to convert an `Identity` object from a `DynamodbEvent` to an SDK v1 compatible `com.amazonaws.services.dynamodbv2.model.Identity`. This is useful for handling identity information within SDK v1 operations. ```APIDOC ## Convert Identity to SDK v1 Identity ### Description Converts an `Identity` object from a `DynamodbEvent` to an AWS SDK v1 `com.amazonaws.services.dynamodbv2.model.Identity`. ### Method Signature `DynamodbIdentityTransformer.toIdentityV1(Identity identity)` ### Parameters - **identity** (`Identity`) - Required - The input identity object. ### Return Value - `com.amazonaws.services.dynamodbv2.model.Identity` - An SDK v1 compatible `Identity` object. ``` -------------------------------- ### Convert StreamRecord to SDK v1 StreamRecord Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md This snippet shows how to convert a `StreamRecord` object, originating from a `DynamodbEvent`, into an SDK v1 compatible `com.amazonaws.services.dynamodbv2.model.StreamRecord`. This is useful for detailed manipulation of stream record data using SDK v1 models. ```APIDOC ## Convert StreamRecord to SDK v1 StreamRecord ### Description Converts a `StreamRecord` object from a `DynamodbEvent` to an AWS SDK v1 `com.amazonaws.services.dynamodbv2.model.StreamRecord`. ### Method Signature `DynamodbStreamRecordTransformer.toStreamRecordV1(StreamRecord streamRecord)` ### Parameters - **streamRecord** (`StreamRecord`) - Required - The input stream record object. ### Return Value - `com.amazonaws.services.dynamodbv2.model.StreamRecord` - An SDK v1 compatible `StreamRecord` object. ``` -------------------------------- ### Convert StreamRecord to SDK v2 StreamRecord Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md Use DynamodbStreamRecordTransformer.toStreamRecordV2 to convert a StreamRecord from a DynamodbEvent to an SDK v2 compatible StreamRecord. ```java import com.amazonaws.services.lambda.runtime.events.transformers.v2.dynamodb.DynamodbStreamRecordTransformer; public class MyClass { public void myMethod(StreamRecord streamRecord) { // ... software.amazon.awssdk.services.dynamodb.model.StreamRecord convertedStreamRecord = DynamodbStreamRecordTransformer.toStreamRecordV2(streamRecord); // ... ``` -------------------------------- ### Set Docker Hub Credentials for CodeBuild Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-runtime-interface-client/README.md Sets Docker Hub username and password as environment variables for CodeBuild to overcome pull rate limits. It is recommended to retrieve these credentials from AWS Secrets Manager. ```shell DOCKERHUB_USERNAME= DOCKERHUB_PASSWORD= ``` -------------------------------- ### Inject Single SQSEvent with @Event Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-tests/README.md Use the @Event annotation to inject a single SQS event from a JSON file into a test. The JSON file must be in the classpath. Requires JUnit 5's @ParameterizedTest. ```java // the json file must be in the classpath (most often in src/test/resources) @ParameterizedTest @Event(value = "sqs/sqs_event.json", type = SQSEvent.class) public void testInjectSQSEvent(SQSEvent event) { // test your handleRequest method with this event as parameter } ``` -------------------------------- ### Convert AttributeValue to SDK v1 AttributeValue Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md This snippet demonstrates converting an `AttributeValue` object from a `DynamodbEvent` to an SDK v1 compatible `com.amazonaws.services.dynamodbv2.model.AttributeValue`. This is essential for working with attribute data in SDK v1 context. ```APIDOC ## Convert AttributeValue to SDK v1 AttributeValue ### Description Converts an `AttributeValue` object from a `DynamodbEvent` to an AWS SDK v1 `com.amazonaws.services.dynamodbv2.model.AttributeValue`. ### Method Signature `DynamodbAttributeValueTransformer.toAttributeValueV1(AttributeValue attributeValue)` ### Parameters - **attributeValue** (`AttributeValue`) - Required - The input attribute value object. ### Return Value - `com.amazonaws.services.dynamodbv2.model.AttributeValue` - An SDK v1 compatible `AttributeValue` object. ``` -------------------------------- ### Implement Java Lambda Stream Handler Source: https://github.com/aws/aws-lambda-java-libs/blob/main/README.md Use this interface for Lambda functions that require direct control over the input and output streams. This is useful for custom serialization or large payloads. Requires the aws-lambda-java-core dependency. ```java public class HandlerStream implements RequestStreamHandler { @Override public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { } } ``` -------------------------------- ### Convert DynamodbStreamRecord to SDK v1 Record Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md Transform a single DynamodbEvent.DynamodbStreamRecord object into an SDK v1 compatible Record. This is useful for granular event processing. ```java import com.amazonaws.services.lambda.runtime.events.transformers.v1.dynamodb.DynamodbRecordTransformer; public class MyClass { public void myMethod(DynamodbEvent.DynamodbStreamRecord record) { // ... Record convertedRecord = DynamodbRecordTransformer.toRecordV1(record); // ... } } ``` -------------------------------- ### Maven Dependencies for AWS Lambda Java Events Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events/README.md Include these dependencies in your Maven project to use the AWS Lambda Java Events library. Ensure you have aws-lambda-java-core as well. ```xml ... com.amazonaws aws-lambda-java-core 1.2.3 com.amazonaws aws-lambda-java-events 3.16.0 ... ``` -------------------------------- ### Convert Identity to SDK v2 Identity Source: https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-events-sdk-transformer/README.md Use DynamodbIdentityTransformer.toIdentityV2 to convert an Identity object from a DynamodbEvent to an SDK v2 compatible Identity. ```java import com.amazonaws.services.lambda.runtime.events.transformers.v2.dynamodb.DynamodbIdentityTransformer; public class MyClass { public void myMethod(Identity identity) { // ... software.amazon.awssdk.services.dynamodb.model.Identity convertedIdentity = DynamodbIdentityTransformer.toIdentityV2(identity); // ... ```