Hello World on AWS Lambda + Java


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

Source code

File list

Simple configuration with only HelloWorld.java and pom.xml.

├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── example
                    └── 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
  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;

Build configuration file for Maven pom.xml

--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"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">






Create a JAR file

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 

Creating a Lambda function

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": {
    "logGroupName": "/aws/lambda/myJavaHelloWorld",
    "functionName": "myJavaHelloWorld",
    "functionVersion": "$LATEST",
    "invokedFunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:myJavaHelloWorld",
    "logger": {},
    "identity": {
      "identityId": "",
      "identityPoolId": ""
    "remainingTimeInMillis": 14814,
    "memoryLimitInMB": 512

About handlers

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

RequestHandler interface

[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);

Input data

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

Context object

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

About the return value

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.

Reference material

-Create a Lambda function in the console -AWS Lambda -Building Lambda functions with Java -AWS Lambda

Recommended Posts

Hello World on AWS Lambda + Java
Is Java on AWS Lambda slow?
Java, Hello, world!
Java Hello World
Hello Java Lambda
Hello World on Mac VS Code Java
"Hello World" in Java
Java Learning (1)-Hello World
Hello World in Java
Hello World on WebAssembly
java hello world, compile, run
Java beginners read Hello World
Regularly post imaged tweets on Twitter with AWS Lambda + Java
Hello World for ImageJ Java Plugin
Hello World in java in eclipse now
Try Hello World using plain Java on a Docker container
Run C binaries on AWS Lambda
Hello world in Java and Gradle
Java 10 (JDK 10) was released on March 20, 2018, so let's try Hello World.
Try running Word2vec model on AWS Lambda
Hello world with Java template engine Thymeleaf
AWS Lambda with Java starting now Part 1
Build Java 8 development environment on AWS Cloud9
Java development with Codenvy: Hello World! Run
Minimal Java environment construction and Hello World
Read "Hello world"
[Java] Lambda expression
Java lambda expression
How to deploy a container on AWS Lambda
Create a Lambda Container Image based on Java 15
Next.js + Rails (API) + Mysql on Docker's Hello World!
Build Java environment and output hello world [Beginner]
How to use Java framework with AWS Lambda! ??
Run JSP Hello World with Tomcat on Docker
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Wastefully creative "Hello World" output method list (Java)
Hello World, a cross-platform GUI app with Groovy running on the Java platform
Let's touch on Java
Install Java on Mac
How to deploy Java to AWS Lambda with Serverless Framework
AWS Lambda timezone change
java neutral lambda expression 1
Lambda on Terraform Container
Quarkus saves Java Lambda! ??
Run PostgreSQL on Java
Read System.out.println ("hello, world")
Let's write Hello World
Understand Java 8 lambda expressions
Java lambda expression variations
Studying Java-Part 1-Hello World
Java 8 lambda expression Feature
java lambda expression memo
About Java lambda expressions
[Introduction] Display Android Studio Hello World on the emulator
Java lambda expression [memo]
Explain Java 8 lambda expressions
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again