How to invoke the AWS lambda function from Java code

0 votes

I haven't used AWS lambda before. I have created a lambda function with handler 

example.Orders::orderHandler

And this is the custom handler, now I want to invoke this from my Java program. So, how do I do that?

Feb 19 in AWS by datageek
• 3,040 points
1,104 views

1 answer to this question.

0 votes

There are 2 methods in this class which should be able to help you. One is for if you need to pass if there a payload, the other if the payload is null.

You should know that the function name may not be the same as the handler. 

So, if you have a function with the function name 'myFunction' that behind the scenes invokes  your example. Orders::orderHandler handler, then this is what you would pass into the run methods below.

import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaAsyncClient;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;

class LambdaInvoker {

    public void runWithoutPayload(String region, String functionName) {
        runWithPayload(region, functionName, null);
    }

    public void runWithPayload(String region, String functionName, String payload) {
        AWSLambdaAsyncClient client = new AWSLambdaAsyncClient();
        client.withRegion(Regions.fromName(region));

        InvokeRequest request = new InvokeRequest();
        request.withFunctionName(functionName).withPayload(payload);
        InvokeResult invoke = client.invoke(request);
        System.out.println("Result invoking " + functionName + ": " + invoke);
    }
}
answered Feb 19 by Archana
• 5,560 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

How to link AWS Lambda function to Amazon CloudWatch ?

In order to create Log Group and ...READ MORE

answered Jul 20, 2018 in AWS by datageek
• 2,440 points
209 views
0 votes
1 answer
0 votes
1 answer

Error when tried to invoke lambda function by AWS CLI

It looks like you need to provide ...READ MORE

answered Sep 10, 2018 in AWS by Archana
• 4,090 points
535 views
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer
0 votes
1 answer