Lambda examples using SDK for Java 2.x - AWS SDK for Java

You can now use the Amazon S3 Transfer Manager (Developer Preview) in the AWS SDK for Java 2.x for accelerated file transfers. Give it a try and let us know what you think! By the way, the AWS SDK for Java team is hiring software development engineers!

Lambda examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with Lambda.

Actions are code excerpts that show you how to call individual Lambda functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Lambda functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to create a Lambda function.

SDK for Java 2.x

public static void createLambdaFunction(LambdaClient awsLambda, String functionName, String filePath, String role, String handler) { try { LambdaWaiter waiter = awsLambda.waiter(); InputStream is = new FileInputStream(filePath); SdkBytes fileToUpload = SdkBytes.fromInputStream(is); FunctionCode code = FunctionCode.builder() .zipFile(fileToUpload) .build(); CreateFunctionRequest functionRequest = CreateFunctionRequest.builder() .functionName(functionName) .description("Created by the Lambda Java API") .code(code) .handler(handler) .runtime(Runtime.JAVA8) .role(role) .build(); // Create a Lambda function using a waiter CreateFunctionResponse functionResponse = awsLambda.createFunction(functionRequest); GetFunctionRequest getFunctionRequest = GetFunctionRequest.builder() .functionName(functionName) .build(); WaiterResponse<GetFunctionResponse> waiterResponse = waiter.waitUntilFunctionExists(getFunctionRequest); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println("The function ARN is " + functionResponse.functionArn()); } catch(LambdaException | FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see CreateFunction in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete a Lambda function.

SDK for Java 2.x

public static void deleteLambdaFunction(LambdaClient awsLambda, String functionName ) { try { //Setup an DeleteFunctionRequest DeleteFunctionRequest request = DeleteFunctionRequest.builder() .functionName(functionName) .build(); awsLambda.deleteFunction(request); System.out.println("The "+functionName +" function was deleted"); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see DeleteFunction in AWS SDK for Java 2.x API Reference.

The following code example shows how to invoke a Lambda function.

SDK for Java 2.x

public static void invokeFunction(LambdaClient awsLambda, String functionName) { InvokeResponse res = null ; try { //Need a SdkBytes instance for the payload JSONObject jsonObj = new JSONObject(); jsonObj.put("inputValue", "2000"); String json = jsonObj.toString(); SdkBytes payload = SdkBytes.fromUtf8String(json) ; //Setup an InvokeRequest InvokeRequest request = InvokeRequest.builder() .functionName(functionName) .payload(payload) .build(); res = awsLambda.invoke(request); String value = res.payload().asUtf8String() ; System.out.println(value); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see Invoke in AWS SDK for Java 2.x API Reference.

Scenarios

The following code example shows how to:

  • Create an AWS Identity and Access Management (IAM) role that grants Lambda permission to write to logs.

  • Create a Lambda function and upload handler code.

  • Invoke the function with a single parameter and get results.

  • Update the function code and configure its Lambda environment with an environment variable.

  • Invoke the function with new parameters and get results. Display the execution log that's returned from the invocation.

  • List the functions for your account.

  • Delete the IAM role and the Lambda function.

For more information, see Create a Lambda function with the console.

SDK for Java 2.x

public class LambdaScenario { public static void main(String[] args) throws InterruptedException { final String usage = "\n" + "Usage:\n" + " <functionName> <filePath> <role> <handler> <bucketName> <key> \n\n" + "Where:\n" + " functionName - The name of the Lambda function. \n"+ " filePath - The path to the .zip or .jar where the code is located. \n"+ " role - The AWS Identity and Access Management (IAM) service role that has Lambda permissions. \n"+ " handler - The fully qualified method name (for example, example.Handler::handleRequest). \n"+ " bucketName - The Amazon Simple Storage Service (Amazon S3) bucket name that contains the .zip or .jar used to update the Lambda function's code. \n"+ " key - The Amazon S3 key name that represents the .zip or .jar (for example, LambdaHello-1.0-SNAPSHOT.jar)." ; if (args.length != 6) { System.out.println(usage); System.exit(1); } String functionName = args[0]; String filePath = args[1]; String role = args[2]; String handler = args[3]; String bucketName = args[4]; String key = args[5]; Region region = Region.US_WEST_2; LambdaClient awsLambda = LambdaClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); String funArn = createLambdaFunction(awsLambda, functionName, filePath, role, handler); System.out.println("The AWS Lambda ARN is "+funArn); // Get the Lambda function. System.out.println("Getting the " +functionName +" AWS Lambda function."); getFunction(awsLambda, functionName); // List the Lambda functions. System.out.println("Listing all AWS Lambda functions."); listFunctions(awsLambda); // Invoke the Lambda function. System.out.println("*** Invoke the Lambda function."); invokeFunction(awsLambda, functionName); // Update the AWS Lambda function code. System.out.println("*** Update the AWS Lambda function code. "); updateFunctionCode(awsLambda, functionName, bucketName, key); // Update the AWS Lambda function configuration. System.out.println("Update the run time of the function."); updateFunctionConfiguration(awsLambda, functionName, handler); // Invoke again with updated function code. invokeFunction(awsLambda, functionName); // Delete the AWS Lambda function. System.out.println("Delete the AWS Lambda function."); deleteLambdaFunction(awsLambda, functionName ); awsLambda.close(); } public static String createLambdaFunction(LambdaClient awsLambda, String functionName, String filePath, String role, String handler) { try { LambdaWaiter waiter = awsLambda.waiter(); InputStream is = new FileInputStream(filePath); SdkBytes fileToUpload = SdkBytes.fromInputStream(is); FunctionCode code = FunctionCode.builder() .zipFile(fileToUpload) .build(); CreateFunctionRequest functionRequest = CreateFunctionRequest.builder() .functionName(functionName) .description("Created by the Lambda Java API") .code(code) .handler(handler) .runtime(Runtime.JAVA8) .role(role) .build(); // Create a Lambda function using a waiter CreateFunctionResponse functionResponse = awsLambda.createFunction(functionRequest); GetFunctionRequest getFunctionRequest = GetFunctionRequest.builder() .functionName(functionName) .build(); WaiterResponse<GetFunctionResponse> waiterResponse = waiter.waitUntilFunctionExists(getFunctionRequest); waiterResponse.matched().response().ifPresent(System.out::println); return functionResponse.functionArn(); } catch(LambdaException | FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } public static void getFunction(LambdaClient awsLambda, String functionName) { try { GetFunctionRequest functionRequest = GetFunctionRequest.builder() .functionName(functionName) .build(); GetFunctionResponse response = awsLambda.getFunction(functionRequest); System.out.println("The runtime of this Lambda function is " +response.configuration().runtime()); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void listFunctions(LambdaClient awsLambda) { try { ListFunctionsResponse functionResult = awsLambda.listFunctions(); List<FunctionConfiguration> list = functionResult.functions(); for (FunctionConfiguration config: list) { System.out.println("The function name is "+config.functionName()); } } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void invokeFunction(LambdaClient awsLambda, String functionName) { InvokeResponse res; try { // Need a SdkBytes instance for the payload. JSONObject jsonObj = new JSONObject(); jsonObj.put("inputValue", "2000"); String json = jsonObj.toString(); SdkBytes payload = SdkBytes.fromUtf8String(json) ; InvokeRequest request = InvokeRequest.builder() .functionName(functionName) .payload(payload) .build(); res = awsLambda.invoke(request); String value = res.payload().asUtf8String() ; System.out.println(value); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void updateFunctionCode(LambdaClient awsLambda, String functionName, String bucketName, String key) { try { LambdaWaiter waiter = awsLambda.waiter(); UpdateFunctionCodeRequest functionCodeRequest = UpdateFunctionCodeRequest.builder() .functionName(functionName) .publish(true) .s3Bucket(bucketName) .s3Key(key) .build(); UpdateFunctionCodeResponse response = awsLambda.updateFunctionCode(functionCodeRequest) ; GetFunctionConfigurationRequest getFunctionConfigRequest = GetFunctionConfigurationRequest.builder() .functionName(functionName) .build(); WaiterResponse<GetFunctionConfigurationResponse> waiterResponse = waiter.waitUntilFunctionUpdated(getFunctionConfigRequest); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println("The last modified value is " +response.lastModified()); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void updateFunctionConfiguration(LambdaClient awsLambda, String functionName, String handler ){ try { UpdateFunctionConfigurationRequest configurationRequest = UpdateFunctionConfigurationRequest.builder() .functionName(functionName) .handler(handler) .runtime(Runtime.JAVA11 ) .build(); awsLambda.updateFunctionConfiguration(configurationRequest); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void deleteLambdaFunction(LambdaClient awsLambda, String functionName ) { try { DeleteFunctionRequest request = DeleteFunctionRequest.builder() .functionName(functionName) .build(); awsLambda.deleteFunction(request); System.out.println("The "+functionName +" function was deleted"); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } }