Invoke, list, and delete AWS Lambda functions - AWS SDK for Java
AWS services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with AWS services in China.

At Amazon Web Services (AWS), we’re focused on finding ways to improve our products and provide a better customer experience. To do that, we need your feedback. Please take 5 minutes of your time to share insights regarding your experience with Java Spring and your need for Spring integration with AWS.

Click here to take a quick survey

This survey is hosted by an external company (Qualtrics), so the link above does not lead to our website. Please note that AWS will own the data gathered via this survey, and will not share the information/results collected with survey respondents. AWS handles your information as described in the AWS Privacy Notice.

Invoke, list, and delete AWS Lambda functions

This section provides examples of programming with the Lambda service client by using the AWS SDK for Java 2.0.

Invoke a Lambda function

You can invoke a Lambda function by creating a LambdaClient object and invoking its invoke method. Create an InvokeRequest object to specify additional information such as the function name and the payload to pass to the Lambda function. Function names appear as arn:aws:lambda:us-west-2:555556330391:function:HelloFunction. You can retrieve the value by looking at the function in the AWS Console.

To pass payload data to a function, create a SdkBytes object that contains information. For example, in the following code example, notice the JSON data passed to the Lambda function.

Imports

import software.amazon.awssdk.services.lambda.LambdaClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.lambda.model.InvokeRequest; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.services.lambda.model.InvokeResponse; import software.amazon.awssdk.services.lambda.model.LambdaException;

Code

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

public static void invokeFunction(LambdaClient awsLambda, String functionName) { InvokeResponse res = null ; try { //Need a SdkBytes instance for the payload SdkBytes payload = SdkBytes.fromUtf8String("{\n" + " \"Hello \": \"Paris\",\n" + " \"countryCode\": \"FR\"\n" + "}" ) ; //Setup an InvokeRequest InvokeRequest request = InvokeRequest.builder() .functionName(functionName) .payload(payload) .build(); //Invoke the Lambda function res = awsLambda.invoke(request); String value = res.payload().asUtf8String() ; System.out.println(value); } catch(LambdaException e) { System.err.println(e.getMessage()); System.exit(1); }

See the complete example on GitHub.

List Lambda functions

Build a LambdaClient object and invoke its listFunctions method. This method returns a ListFunctionsResponse object. You can invoke this object’s functions method to return a list of FunctionConfiguration objects. You can iterate through the list to retrieve information about the functions. For example, the following Java code example shows how to get each function name.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.lambda.LambdaClient; import software.amazon.awssdk.services.lambda.model.LambdaException; import software.amazon.awssdk.services.lambda.model.ListFunctionsResponse; import software.amazon.awssdk.services.lambda.model.FunctionConfiguration; import java.util.List;

Code

The following Java code example demonstrates how to retrieve a list of function names.

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); }

See the complete example on GitHub.

Delete a Lambda function

Build a LambdaClient object and invoke its deleteFunction method. Create a DeleteFunctionRequest object and pass it to the deleteFunction method. This object contains information such as the name of the function to delete. Function names appear as arn:aws:lambda:us-west-2:555556330391:function:HelloFunction. You can retrieve the value by looking at the function in the AWS Console.

Imports

import software.amazon.awssdk.services.lambda.LambdaClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.lambda.model.DeleteFunctionRequest; import software.amazon.awssdk.services.lambda.model.LambdaException;

Code

The following Java code demonstrates how to delete a Lambda function.

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); }

See the complete example on GitHub.