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 LambdaClientinvoke
method. Create an InvokeRequest
To pass payload data to a function, create a SdkBytes
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
List Lambda functions
Build a LambdaClientlistFunctions
method.
This method returns a ListFunctionsResponsefunctions
method to return a list of FunctionConfiguration
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
Delete a Lambda function
Build a LambdaClientdeleteFunction
method.
Create a DeleteFunctionRequestdeleteFunction
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