This section provides examples of programming with the Lambda service client by using the AWS SDK for Java. To learn how to create a Lambda function, see How to Create AWS Lambda functions.
You can invoke a Lambda function by creating an :aws-java-class:`AWSLambda <services/lambda/AWSLambda>` object and invoking its :methodname:`invoke` method. Create an :aws-java-class:`InvokeRequest <services/lambda/model/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, invoke the :aws-java-class:`InvokeRequest <services/lambda/model/InvokeRequest>` object's :methodname:`withPayload` method and specify a String in JSON format, as shown in the following code example.
Imports
.. literalinclude:: lambda.java1.invoke.import.txt :language: java
Code
The following code example demonstrates how to invoke a Lambda function.
.. literalinclude:: lambda.java1.invoke.main.txt :language: java
See the complete example on Github.
Build an :aws-java-class:`AWSLambda <services/lambda/AWSLambda>` object and invoke its :methodname:`listFunctions` method. This method returns a :aws-java-class:`ListFunctionsResult <services/lambda/model/ListFunctionsResult>` object. You can invoke this object's :methodname:`getFunctions` method to return a list of :aws-java-class:`FunctionConfiguration <services/lambda/model/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
.. literalinclude:: lambda.java1.list.import.txt :language: java
Code
The following Java code example demonstrates how to retrieve a list of Lambda function names.
.. literalinclude:: lambda.java1.list.main.txt :language: java
See the complete example on Github.
Build an :aws-java-class:`AWSLambda <services/lambda/AWSLambda>` object and invoke its :methodname:`deleteFunction` method. Create a :aws-java-class:`DeleteFunctionRequest <services/lambda/model/DeleteFunctionRequest>` object and pass it to the :methodname:`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
.. literalinclude:: lambda.java1.delete.import.txt :language: java
Code
The following Java code demonstrates how to delete a Lambda function.
.. literalinclude:: lambda.java1.delete.main.txt :language: java
See the complete example on Github.