2

I have a code in java that returns an integer value. for example:

public class Hello {

    public int myHandler(Object name, Context context) {

        int count = 5;
        return count;
    }
}

I have uploaded this code on AWS with the handler name as example.Hello::myHandler and function name as AWSLAMBDA under the us-east-2 region.

Now I want to write another code in Java which invokes the output value (count) of the earlier code. Note that count is an integer. Since I am a novice in both java and aws. Kindly help me with this and please provide a simple explanation if possible.

1

2 Answers 2

1
  • This is the code snippet example aws sdk
  • To invoke a function asynchronously, set InvocationType to Event
  • To invoke a function synchronously, set InvocationType to RequestResponse (which is the default value).
  • The calling lambda should have a role with attached policy having lambda:InvokeFunction action.
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;

AWSLambda client = AWSLambdaClientBuilder.standard().build();
InvokeRequest request = new InvokeRequest().withFunctionName("MyFunction").withInvocationType("RequestResponse").withLogType("Tail").withClientContext("MyApp")
        .withPayload(ByteBuffer.wrap("fileb://file-path/input.json".getBytes())).withQualifier("1");
InvokeResult response = client.invoke(request);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for answering. It is throwing an error in java stating that method invoke is undefined for the type AWSLambda. The error is in this line of the code. InvokeResult response = client.invoke(request);
I have updated the answer with few of the imports that you need, check that
Vikram if it has helped you can you please accept the answer. So that it can help others as well.
Make sure your Maven or Gradle pulls in dependency com.amazonaws:aws-java-sdk-lambda.
0

Besides the Java code you also need to make sure that the policy attached to your Lambda function is actually able to invoke the second Lambda function. Otherwise, the Java snippet will fail since the invoking Lambda misses the permission to invoke other functions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.