Home:ALL Converter>Issue on Calling AWS JAVA LAMBDA From Another Java Lambda

Issue on Calling AWS JAVA LAMBDA From Another Java Lambda

Ask Time:2018-02-21T15:32:02         Author:ShaiNe Ram

Json Formatter

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

Author:ShaiNe Ram,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48900336/issue-on-calling-aws-java-lambda-from-another-java-lambda
Mohan Shanmugam :

When you are sure it takes more than 5 minutes following is my solution to process the CSV File. \n\n1) Goto S3 Bucket Create Notification trigger for lambda. \n\n2) By using the lambda event you can read about s3 object metadata and submit a Job in AWS Batch using batch client.\n\n3) You need to create an IAM role for lambda with proper batch permissions.\n\nAWS Batch is nothing but a dockerized environment to run the application or service.\n\nAWS Batch requires you to create docker file and store image in AWS ECR repository or Docker Cloud. You will add spot instances in batch.",
2018-02-21T11:40:07
Akhil Vijayan :

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. \n\n\n 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.\n\n\nSo 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",
2018-02-21T09:43:30
yy