2

I want to call a aws lambda which is in JAVA from another lambda. I go through the below result "https://stackoverflow.com/questions/36483042/how-to-call-an-aws-java-lambda-function-from-another-aws-java-lambda-function-wh"

I implemented the code like below.

First I create one AWS lambda java project. My code is like below

import com.amazonaws.services.lambda.AWSLambdaAsyncClientBuilder;
import com.amazonaws.services.lambda.invoke.LambdaInvokerFactory;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<Object, String> {

    @Override
    public String handleRequest(Object input, Context context) {
        context.getLogger().log("Input: " + input);

        FineGrainedService fg = LambdaInvokerFactory.builder()
                .lambdaClient(
                        AWSLambdaAsyncClientBuilder.standard()

                        .build()
                )
                .build(FineGrainedService.class);

        context.getLogger().log("Response back from FG" + fg.getClass());

        String fgRespone = fg.callFineGrained("Call from Gateway");
        context.getLogger().log("fgRespone: " + fgRespone);

        // TODO: implement your handler
        return "Hello from Gateway Lambda!";
    }
}

As per the above link created one interface

import com.amazonaws.services.lambda.invoke.LambdaFunction;

public interface FineGrainedService {
     @LambdaFunction(functionName="SimpleFineGrained")
     String callFineGrained(String input);
}

Again created another lambda to call the above lambda

import com.amazonaws.services.lambda.AWSLambdaAsyncClientBuilder;
import com.amazonaws.services.lambda.invoke.LambdaInvokerFactory;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler2 implements RequestHandler<Object, String> {

    @Override
    public String handleRequest(Object input, Context context) {
        context.getLogger().log("Input: " + input);

        FineGrainedService fg = LambdaInvokerFactory.builder()
                .lambdaClient(
                        AWSLambdaAsyncClientBuilder.standard().build()
                )
                .build(FineGrainedService.class);

        context.getLogger().log("Response back from FG" + fg.getClass());

        String fgRespone = fg.callFineGrained("Call from Gateway");
        context.getLogger().log("fgRespone: " + fgRespone);

        // TODO: implement your handler
        return "Hello from Gateway Lambda!";
    }
}

Added maven dependency to pom.xml

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-lambda</artifactId>
 </dependency>

Added lambda invoke permission to my current role. Then I uploaded the code to the AWS. But when I am testing this I am getting an error like below.. Please could you help me to figure out this?

enter image description here

It would be grateful if anyone can help to get this... Or any other way to call a lambda from another lambda using JAVA

6
  • i am not sure about your exact requirement. But AWS Step function is the best way to implement the solution. Other ways are we can send data from lambda to SNS and trigger another lambda. Commented Feb 21, 2018 at 7:56
  • @Mohan Shanmugam. Thanks for your replay.. Could you explain me how to use step function for this lambda. I tried to get this. But I could not implement this using step funnction as I am newbie to aws.. Will u please help to achieve this? Commented Feb 21, 2018 at 8:16
  • My exact requirement is to process a large csv using serverless architecture. I got a link to process large CSV using stepfunction. It was in node.js. I am not able to convert the thing to java. How could I achieve this? click here for process large csv link Please help me Commented Feb 21, 2018 at 8:19
  • Where do you have your large csv file? Are you have in S3? Confirm what is the time taken to complete the processing of CSV file in local? Commented Feb 21, 2018 at 9:04
  • Yeh..I am keeping my csv in s3. I am adding a trigger to my first lambda. Sure I will process in my local java ad let you know the tile taken to complete the processing of CSV. Commented Feb 21, 2018 at 9:07

3 Answers 3

0

We should never directly link lambda together, AWS step function is the right way to go. You can go through the AWS documentation. It is a well-structured documentation provide my AWS so no need to worry.

Note: AWS step function is not good when we need to get the response back as it works asynchronously so we need to poll the function again and again in the code to know the status whether it is in running or in a finished state.

So it is based on the requirement. AWS step function is best when we don't have to wait for the operation to finish. You can refer to example1 and example2

Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Akhil Vijayan. Thanks for your reply. I already go through the examples which you mentioned. But you know I am little confused how to test the same? If i am going to my eclipse and I create a new aws lambda project it will have a handler. How can I use this to my project? If you don't mind please help me to integrate this with my handler.
Where do you need help? Do you need help is testing out a lambda function or an AWS step function?
Using Waiters that is built into the Java V2 API, there are many operations where you do longer need to poll. See docs.aws.amazon.com/sdk-for-java/latest/developer-guide/…
0

When you are sure it takes more than 5 minutes following is my solution to process the CSV File.

1) Goto S3 Bucket Create Notification trigger for lambda.

2) By using the lambda event you can read about s3 object metadata and submit a Job in AWS Batch using batch client.

3) You need to create an IAM role for lambda with proper batch permissions.

AWS Batch is nothing but a dockerized environment to run the application or service.

AWS Batch requires you to create docker file and store image in AWS ECR repository or Docker Cloud. You will add spot instances in batch.

2 Comments

I hope it will become a server application. I want to implement the task as a serverless application
When you are looking for Serverless you need to write your lambda in java and follow the step function link what you referred from LinkedIn.
0

Try using AWS Step Functions to invoke Lambda functions. Each Step function is actually a Lambda function and that lambda function can invoke other AWS Services. Therefore you can build a workflow where each step can invoke different services. For details, see this AWS tutorial that uses Java V2 for each step:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_workflows_stepfunctions

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.