AWS Lambda as Java POJOs-Deployed with Java CDK

AWS Lambda functions are plain Java classes with at least a single public method:

    public class Greetings{

        public String onEvent(Map input) {

...with primitive datatypes, collections or streams as input parameters. The return value is serialized into a String for primitive types or JSON for classes.

A selected public method becomes the function handler. In the example above it is the method: onEvent

An AWS Lambda can be deployed with AWS CDK in Java:



Function createUserListenerFunction(String functionName,String functionHandler, int memory, int timeout) {
    return Function.Builder.create(this, id(functionName))
...and (blackbox) integration tested with AWS SDK for Java 2.x:

public void initClient() {
    var credentials = DefaultCredentialsProvider

    this.client = LambdaClient.builder()

public void invokeLambdaAsynchronously() {
        String json = "{\"user \":\"duke\"}";
        SdkBytes payload = SdkBytes.fromUtf8String(json);

        InvokeRequest request = InvokeRequest.builder()

        var response = this.client.invoke(request);
        var error = response.functionError();
        var value = response.payload().asUtf8String();
        System.out.println("Function executed. Response: " + value);
See it in action:

The sample code is available as template from:


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments