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
• 2,870 points
158 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,360 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,390 points
79 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
• 3,770 points
261 views
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.