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.x.
Invoke a Lambda function
You can invoke a Lambda function by creating a LambdaClient
invoke
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 String json = "{\"Hello \":\"Paris\"}"; 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); } }
See the complete example
List Lambda functions
Build a LambdaClient
object and invoke its
listFunctions
method. This method returns a ListFunctionsResponse
functions
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 LambdaClient
deleteFunction
method. Create a DeleteFunctionRequest
deleteFunction
method. This object contains information such as the
name of the function to delete. Function names appear as
arn:aws:lambda:us-east-1:123456789012:function:HelloFunction.
You can retrieve the value by looking at the function in the AWS Management 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 { 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