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
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.
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
- Java AWS Lambda using Spring Cloud Function
- AWS Lambda in Kotlin using Spring Cloud Function
- S3 File upload & download with AWS Java SDK v2
- Feign Client Logging and connection timeout
- Slack Webhook Message from Spring Boot
- How does Session handling works in Servlet environment