The project source includes function code and supporting resources:
src/main- A Java function.src/test- A unit test and helper classes.template.yml- An AWS CloudFormation template that creates an application.build.gradle- A Gradle build file.pom.xml- A Maven build file.1-create-bucket.sh,2-deploy.sh, etc. - Shell scripts that use the AWS CLI to deploy and manage the application.
Use the following instructions to deploy the sample application.
- Java 8 runtime environment (SE JRE)
- Maven 3
- The Bash shell. For Linux and macOS, this is included by default. In Windows 10, you can install the Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.
- The AWS CLI.
Download or clone this repository.
$ git clone git@github.com:awsdocs/aws-lambda-developer-guide.git
$ cd aws-lambda-developer-guide/sample-apps/java-basic
To create a new bucket for deployment artifacts, run 1-create-bucket.sh. Or, if you already have a bucket, create a file named bucket-name.txt that contains the name of your bucket.
java-basic$ ./1-create-bucket.sh
make_bucket: lambda-artifacts-a5e4xmplb5b22e0d
To deploy the application, run 2-deploy.sh.
java-basic$ ./2-deploy.sh
BUILD SUCCESSFUL in 1s
Successfully packaged artifacts and wrote output template to file out.yml.
Waiting for changeset to be created..
Successfully created/updated stack - java-basic
This script uses AWS CloudFormation to deploy the Lambda functions and an IAM role. If the AWS CloudFormation stack that contains the resources already exists, the script updates it with any changes to the template or function code.
You can also build the application with Maven. To use maven, add mvn to the command.
java-basic$ ./2-deploy.sh mvn
[INFO] Scanning for projects...
[INFO] -----------------------< com.example:java-basic >-----------------------
[INFO] Building java-basic-function 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
To invoke the function, run 3-invoke.sh.
java-basic$ ./3-invoke.sh
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
"200 OK"
The application uses AWS X-Ray to trace requests. Open the X-Ray console to view the service map.
Choose a node in the main function graph. Then choose View traces to see a list of traces. Choose any trace to view a timeline that breaks down the work done by the function.
Finally, view the application in the Lambda console.
To view the output
- Open the applications page in the Lambda console.
- Choose java-basic.
By default, the function uses a handler class named Handler that takes a map as input and returns a string. The project also includes handlers that use other input and output types. These are defined in the following files under src/main/java/example:
Handler.java– Takes aMap<String,String>as input.HandlerInteger.java– Takes anIntegeras input.HandlerList.java– Takes aList<Integer>as input.HandlerStream.java– Takes anInputStreamandOutputStreamas input.HandlerString.java– Takes aStringas input.HandlerWeatherData.java– Takes a custom type as input.
To use a different handler, change the value of the Handler setting in the application template (template.yml or template-mvn.yaml). For example, to use the list handler:
Properties:
CodeUri: build/distributions/java-basic.zip
Handler: example.HandlerList
Deploy the change, and then use the invoke script to test the new configuration. For handlers, that don't take a JSON object as input, pass the type (string, int or list) as an argument to the invoke script.
./3-invoke.sh list
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
9979
To delete the application, run 4-cleanup.sh.
java-basic$ ./4-cleanup.sh



