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'
}
Invoking AWS Lambda from a Java Client
Carvia Tech | August 12, 2019 | 2 min read | 483 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.
Lets define our domain models: LambdaInput
and LambdaOutput
, these classes will be used for marshalling input and output of AWS Lambda.
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.
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.
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());
}
}
lambda response = 54321
That’s all.
Grab the source code
You can download the source code for this article from Github:
Linked Article
Top articles in this category:
- Invoke AWS Lambda from a Kotlin Client
- AWS SDK 2.0 - S3 File upload & download in Java
- Creating AWS Lambda in Java using Spring Cloud Function
- Creating AWS Lambda in Kotlin using Spring Cloud Function
- HTTP logging & connection timeout in Feign Clients with Spring Boot
- SendGrid emails with Spring Boot and Java
- Self Signed Certificate in Spring 5 WebClient
Find more on this topic:

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:
Similar Posts
- Feign common headers using RequestInterceptor
- Sending Gupshup SMS using Java API
- Basic Authentication in RestTemplate
- Disable SSL certificate validation in RestTemplate
- How does Session handling works in Servlet environment
- HTTP logging & connection timeout in Feign Clients with Spring Boot
- SendGrid emails with Spring Boot and Java
- Spring Boot file upload with Kotlin and Junit 5
- Java 8 date time JSON formatting with Jackson
- AWS SDK 2.0 - S3 File upload & download in Java
Enter your email address to subscribe to this blog and receive notifications of new posts by email.