Invoking AWS Lambda from a Java Client

Carvia Tech | August 12, 2019 | 2 min read | 48 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. Creating AWS Lambda in Java using Spring Cloud Function
  3. Creating AWS Lambda in Kotlin using Spring Cloud Function
  4. AWS SDK 2.0 - S3 Object Operations using Spring Boot
  5. Retrofit vs FeignClient on Server Side with Spring Cloud
  6. Self Signed Certificate in Spring 5 WebClient
  7. Synechron Java Interview Questions



Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info