The AWS Greengrass Core SDK for Java enables Java developers to develop Lambda functions which will run within Greengrass.
This document provides instructions for preparing your Greengrass Core environment to run Lambda functions written in Java. It also includes examples on how to develop a Lambda function in Java as well as packaging and running an example Hello World file in Java for your Greengrass core.
The environment where Greengrass is running on needs to be able to run Java 8 packages.
samples/HelloWorld
folder to your workspace.libs
folder within HelloWorld
folder and copy GreengrassJavaSDK-1.3.jar
file from sdk
folder into the libs
folder.gradle build
HelloWorld.zip
in build/distributions
folder.Java 8
.HelloWorld.zip
file in Lambda function code section.com.amazonaws.greengrass.examples.HelloWorld::handleRequest
. The format of handler for java functions is package.class::method-reference
.You can use any building and packaging tool you like to create this zip. Regardless of the tools you use, the resulting .zip file must have the following structure:
/lib
directory.All the examples and instructions in this manual use Gradle build and deployment tool to create the .zip.
You will need to download Gradle. For instructions, go to the gradle website, https://gradle.org/
For Greengrass, follow the steps below.
libs
folder.GreengrassJavaSDK-1.3.jar
to libs
folder.build.gradle
file for Greengrass function looks like the following. You may add additional dependencies as necessary for your function.
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'com.fasterxml.jackson.core:jackson-core:2.8.8'
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.8'
compile 'org.apache.httpcomponents:httpclient:4.5.3'
compile 'com.amazonaws:aws-lambda-java-core:1.1.0'
compile 'com.amazonaws:aws-java-sdk-core:1.11.178'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
task buildZip(type: Zip) {
from compileJava
from processResources
into('lib') {
from configurations.runtime
}
}
build.dependsOn buildZip
src
folder.gradle build
build/distributions
folder which you can now upload to AWS Lambda to be used by Greengrass.Your System.out.println operation will be logged as INFO. A System.err.println operation will be logged as ERROR. Alternatively, you can also log using context.getLogger().log
operation which will log at INFO level. Currently, our Java SDK only allows you to log at INFO or ERROR level only.
From GGC version 1.5.0, you can send binary data with the SDK. However, in order to make a lambda function be able to handle binary payload. You need to do the following:
* void (InputStream, OutputStream, Context)
* void (InputStream, OutputStream)
* void (OutputStream, Context)
* void (InputStream, Context)
* void (InputStream)
* void (OutputStream)
In addition to the function signatures mentioned above, there are also more supported function signatures being introduced:
Supported "json" function handler signatures:
* Anything (Context)
* Anything (AlmostAnything, Context)
* Anything (AlmostAnything)
* Anything ()
If your Java code contains multiple methods with same name as the handler name, then GGC uses the following rules to pick a method to invoke:
In Greengrass, you can send a context object in a JSON format to be passed to another Lambda that is being invoked. The context format looks like this: { custom: { customData: 'customData', }, }
As new features are added to AWS Greengrass, previous versions of the Greengrass SDK will be incompatible with newer versions of the AWS Greengrass core. The following table lists the compatible SDKs for all GGC releases.
GGC Version | Compatible SDK Versions |
---|---|
1.0.x-1.6.x | 1.0.x-1.2.x |
1.7.x | 1.0.x-1.3.x |