AWS Lambda to invoke other lambda function asynchronously in same region using Java

Create IAM role for Lambda:

Go to AWS Console, type IAM and click the IAM service.
This will open the IAM service page.
Click the Roles from the left menu to display the Roles under Access management.
Click the Create role button on right.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "arn:aws:lambda:*:*:*"
}
]
}

Create a lambda function which we want to invoke:

1. Type lambda in the console to list the lambda service and select it.
2. Click the Create function to generate new lambda function.
3. In Basic information give the function Name,Runtime (Java 8).
4. Default Advanced settings.
5. Click Create function.

import com.amazonaws.services.lambda.runtime.Context;

public class LamdaExample
{
String text="Invoking the Lambda";

public Boolean handler(Context context){
context.getLogger().log("Function name: "+ context.getFunctionName()+"\n" );
context.getLogger().log( "Invoke message: "+text+"\n" );
return true;
}
}

Create a lambda function using InvokeRequest:

1. Type lambda in the console to list the lambda service and select it.
2. Click the Create function to generate new lambda function.
3. In Basic information give the function Name,Runtime (Java 8),Existing execution role (previously created).
4. Default Advanced settings.
5. Click Create function.

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.lambda.AWSLambdaClient;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import java.nio.ByteBuffer;


public class LambdaToLambda implements RequestHandler {
private static final AmazonS3 s3client=AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("<youraccesskey>", "<yoursecretkey>"))))
.withRegion("us-east-1").build();
String functionName = "<your function ARN>";
String inputJSON = "{\"Name\":\"Swapnil\",\"lastName\": \"Watkar\"}";
@Override
public String handleRequest(Object input, Context context) {

AWSLambdaClient lambdaClient = new AWSLambdaClient();
try {
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName(functionName);
invokeRequest.setPayload(inputJSON);
ByteBuffer payload = lambdaClient.invoke(invokeRequest).getPayload();
context.getLogger().log(payload.toString());

} catch (Exception e) {
context.getLogger().log(" --Exception-- "+e.getMessage());
}
return "Invoking other lambda MyLambdaFunction ";
}
}
mvn clean package

Test the scenario:

To test our code go to our Lambda function which has InvokeRequest , then go to Test section.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Swapnil Watkar

Swapnil Watkar

Hi ! I am a Software Engineer, experienced in Mobile and Web applications Also AWS Certified Solutions Architect — Associate