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, 2019 in AWS by datageek
• 3,090 points
5,655 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, 2019 by Archana
• 5,640 points

Related Questions In AWS

0 votes
1 answer

how to get the list of aws services i am used in aws my account by using the lambda function

Hi@shalk, You can create a session in your ...READ MORE

answered Sep 24, 2020 in AWS by MD
• 95,440 points
2,232 views
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,530 points
1,339 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP