Invoking AWS Lambda from a Java Client

Upasana | August 12, 2019 | 2 min read | 1,153 views | AWS Tutorials


In this tutorial, we will invoke an existing lambda function from a Java Client with aws-java-sdk. This approach can be used to invoke a lambda from another lambda or a java based microservice running as full fledged server app.

Here we will assume that we already have a lambda function deployed in AWS with name "number-reversal-java" that accepts an input number, reverses it and returns it back.

You can follow the previous article if you want to explore Creating a AWS Lambda using Spring Cloud Function

Creating a Gradle based project

Lets create a simple gradle based project for our implementation.

build.gradle
plugins {
    id 'java'
}

group 'com.shunya'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.amazonaws', name: 'aws-java-sdk-lambda', version: '1.11.546'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Lets define our domain models: LambdaInput and LambdaOutput, these classes will be used for marshalling input and output of AWS Lambda.

Domain models
public class LambdaInput {

    private long input;

    public long getInput() {
        return input;
    }

    public void setInput(long input) {
        this.input = input;
    }
}

public class LambdaOutput {

    private long result;

    public long getResult() {
        return result;
    }

    public void setResult(long result) {
        this.result = result;
    }
}

Now let’s define a service interface for our lambda function. This service defines the signature/contract for input and output of lambda function.

NumberService.java
import com.amazonaws.services.lambda.invoke.LambdaFunction;

public interface NumberService {

    @LambdaFunction(functionName = "number-reversal-java") (1)
    LambdaOutput reverse(LambdaInput input);
}
1 number-reversal-java is the name of AWS lambda deployed on AWS.

Finally, we will create a client that invokes the lambda using domain models and interface defined above. We are using AWS java sdk for lambda in this client to access lambda and invoke it programmatically.

LambdaClient.java
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.invoke.LambdaInvokerFactory;

public class LambdaClient {

    public static void main(String[] args) {
        final NumberService numberService = LambdaInvokerFactory.builder()
                .lambdaClient(AWSLambdaClientBuilder.defaultClient())
                .build(NumberService.class);

        LambdaInput request = new LambdaInput();
        request.setInput(12345);
        final LambdaOutput lambdaOutput = numberService.reverse(request);
        System.out.println("lambda response = " + lambdaOutput.getResult());
    }
}
Program output
lambda response = 54321

That’s all.

Grab the source code

You can download the source code for this article from Github:


Top articles in this category:
  1. Invoke AWS Lambda from a Kotlin Client
  2. Java AWS Lambda using Spring Cloud Function
  3. AWS Lambda in Kotlin using Spring Cloud Function
  4. S3 File upload & download with AWS Java SDK v2
  5. Feign Client Logging and connection timeout
  6. Slack Webhook Message from Spring Boot
  7. How does Session handling works in Servlet environment

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.