--AWS (Amazon Web Services) Run a simple Hello World with Java on Lambda --Use the predefined interface RequestHandler provided by AWS Lambda Java core library (aws-lambda-java-core) to implement the handler. --Use Maven to generate JAR files
Simple configuration with only HelloWorld.java and pom.xml.
├── pom.xml
└── src
└── main
└── java
└── com
└── example
└── HelloWorld.java
HelloWorld.java
Receive input data as a Map <String, Object> object so that you can process general-purpose JSON input data. The output is also returned as a Map <String, Object> object.
package com.example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.HashMap;
import java.util.Map;
public class HelloWorld implements RequestHandler<Map<String, Object>, Map<String, Object>> {
/**
*AWS Lambda handler method.
*
* @param input input data
* @param context AWS Lambda Context object
* @return output data
*/
@Override
public Map<String, Object> handleRequest(Map<String, Object> input, Context context) {
Map<String, Object> output = new HashMap<>();
output.put("foo", "Hello, world");
output.put("bar", "Goodbye, world");
output.put("input", input); //I want to see the input information, so include it in the output
output.put("context", context); //I want to see the context information, so include it in the output
return output;
}
}
--Match Java version 1.8 supported by AWS Lambda --Introduce the library aws-lambda-java-core for AWS Lambda --Introduce maven-shade-plugin to generate JAR file including library
[Creating a \ .jar deployment package using Maven without IDE \ (Java ) -AWS Lambda](https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/java-create- jar-pkg-maven-no-ide.html)
In the "dependencies" section, groupId (com.amazonaws) is the Amazon AWS group ID for the Maven artifacts in the Maven central repository. artifactId (aws-lambda-java-core) is AWS Lambda's core library that provides RequestHandler, RequestStreamHandler definitions, and the Context AWS Lambda interface for use in Java applications. At build time, Maven resolves these dependencies. In the Plugins section, Apache maven-shade-plugin is a plugin that Maven downloads and uses during the build process. This plugin is used to package the jar to create a standalone .jar (.zip file) that is a deployment package.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>lambda-hello-world</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>lambda-hello-world</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Generate a JAR file with the mvn package command.
$ mvn package
$ ls -l target/*.jar
-rw-r--r--@ 1 foo staff 10047 8 25 10:08 target/lambda-hello-world-1.0.jar
-rw-r--r--@ 1 foo staff 2692 8 25 10:08 target/original-lambda-hello-world-1.0.jar
Deploy to AWS Lambda is the lambda-hello-world-1.0.jar file. You can check the list of files included in the jar command.
$ jar --list --file target/lambda-hello-world-1.0.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/example/
com/example/HelloWorld.class
META-INF/maven/
META-INF/maven/com.example/
META-INF/maven/com.example/lambda-hello-world/
META-INF/maven/com.example/lambda-hello-world/pom.xml
META-INF/maven/com.example/lambda-hello-world/pom.properties
com/amazonaws/
com/amazonaws/services/
com/amazonaws/services/lambda/
com/amazonaws/services/lambda/runtime/
com/amazonaws/services/lambda/runtime/LambdaRuntime$1.class
com/amazonaws/services/lambda/runtime/CognitoIdentity.class
com/amazonaws/services/lambda/runtime/LambdaRuntime.class
com/amazonaws/services/lambda/runtime/ClientContext.class
com/amazonaws/services/lambda/runtime/Client.class
com/amazonaws/services/lambda/runtime/Context.class
com/amazonaws/services/lambda/runtime/LambdaRuntimeInternal.class
com/amazonaws/services/lambda/runtime/LambdaLogger.class
com/amazonaws/services/lambda/runtime/RequestHandler.class
com/amazonaws/services/lambda/runtime/RequestStreamHandler.class
META-INF/maven/com.amazonaws/
META-INF/maven/com.amazonaws/aws-lambda-java-core/
META-INF/maven/com.amazonaws/aws-lambda-java-core/pom.xml
META-INF/maven/com.amazonaws/aws-lambda-java-core/pom.properties
Create a Lambda function from the Lambda Management Console (https://console.aws.amazon.com/lambda/).
Select "Create from scratch".
Enter the "basic information". Function name: any name Runtime: Java 8 Run Role: Create a new role with basic Lambda permissions
Nothing in particular is done in "Designer".
Enter the "function code". Code entry type: Upload .zip or jar file Runtime: Java 8 Handler: com.example.HelloWorld :: handleRequest
Click "Upload" to upload the generated JAR file lambda-hello-world-1.0.jar, and click "Save" in the upper right.
Click Test at the top right of the page to create a new test event. Event template: Hello World Event name: any name Change the JSON that is the input data to any one (or leave the default).
You can run the test by clicking "Test" at the top right of the page. The result is displayed.
An example of the output result.
{
"input": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"bar": "Goodbye, world",
"foo": "Hello, world",
"context": {
"awsRequestId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"logGroupName": "/aws/lambda/myJavaHelloWorld",
"logStreamName": "2019/08/25/[$LATEST]XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"functionName": "myJavaHelloWorld",
"functionVersion": "$LATEST",
"invokedFunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:myJavaHelloWorld",
"logger": {},
"identity": {
"identityId": "",
"identityPoolId": ""
},
"remainingTimeInMillis": 14814,
"memoryLimitInMB": 512
}
}
--There are two main patterns for implementing handlers. --Write your own handler method using any name and parameters --Use a predefined interface (RequestStreamHandler or RequestHandler) --Both have the same input / output interface --Input data and Context object are passed as arguments to the handler
[Using the predefined interface for handler creation \ (Java ) -AWS Lambda](https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/java-handler-using- predefined-interfaces.html)
Instead of writing your own handler method with any name and parameter, use one of the predefined interfaces provided by the AWS Lambda Java core library (aws-lambda-java-core). You can also create a Lambda function handler.
You can implement one of the predefined interfaces (RequestStreamHandler or RequestHandler) and implement the handleRequest method provided for that interface. These interfaces depend on whether you use standard Java types or custom POJO types for handler I / O (AWS Lambda automatically serializes the I / O to match the data type). Implement one of them. Or use the Stream type to customize serialization.
I couldn't find the official documentation by javadoc, so refer to the source code on GitHub.
aws-lambda-java-libs/RequestHandler.java at master · aws/aws-lambda-java-libs · GitHub
/**
*
* Lambda request handlers implement AWS Lambda Function application logic using plain old java objects
* as input and output.
*
* @param <I> The input parameter type
* @param <O> The output parameter type
*/
public interface RequestHandler<I, O> {
/**
* Handles a Lambda Function request
* @param input The Lambda Function input
* @param context The Lambda execution environment context object.
* @return The Lambda Function output
*/
public O handleRequest(I input, Context context);
}
Java AWS Lambda Function Handler -AWS Lambda
inputType – The first parameter of the handler is the input to the handler. This parameter can be event data (issued by the event source) or user-provided custom input (such as a string or custom data object). For AWS Lambda to be able to call this handler successfully, the function must be called with input data that can be serialized to the data type of the input parameter.
Handler I / O type \ (Java ) -AWS Lambda
AWS Lambda supports the following I / O types of handlers: · Simple Java types (AWS Lambda supports string, integer, Boolean, map, and list types) ・ POJO (Plain Old Java Object) type Stream type (if you don't use POJOs, or if Lambda's serialization approach doesn't suit your needs, you can use a byte stream implementation). For more information, see Example: Using Streams for Handler I / O (Java).
Java AWS Lambda Context Object -AWS Lambda
When the function is executed in Lambda, the context object is passed to the handler. This object provides methods and properties that provide information about calls, functions, and execution functions.
aws-lambda-java-libs/Context.java at master · aws/aws-lambda-java-libs · GitHub
The context object allows you to access useful information available within the Lambda execution environment
Java AWS Lambda Function Handler -AWS Lambda
outputType – If you call your Lambda function synchronously (using the RequestResponse call type), you can return the output of the function using any of the supported data types. For example, if you use your Lambda function as a backend for your mobile application, you're calling it synchronously. The output data type is serialized to JSON. For plans that call Lambda functions asynchronously (using the Event call type), outputType must be void. For example, if you use AWS Lambda with event sources such as Amazon S3 or Amazon SNS, these event sources use your Event call type to call your Lambda function.
-Create a Lambda function in the console -AWS Lambda -Building Lambda functions with Java -AWS Lambda
Recommended Posts