|
| 1 | +# Blank function (Java) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +The project source includes function code and supporting resources: |
| 6 | + |
| 7 | +- `src/main` - A Java function. |
| 8 | +- `src/test` - A unit test and helper classes. |
| 9 | +- `template.yml` - An AWS CloudFormation template that creates an application. |
| 10 | +- `build.gradle` - A Gradle build file. |
| 11 | +- `pom.xml` - A Maven build file. |
| 12 | +- `1-create-bucket.sh`, `2-build-layer.sh`, etc. - Shell scripts that use the AWS CLI to deploy and manage the application. |
| 13 | + |
| 14 | +Use the following instructions to deploy the sample application. |
| 15 | + |
| 16 | +# Requirements |
| 17 | +- [Java 8 runtime environment (SE JRE)](https://www.oracle.com/java/technologies/javase-downloads.html) |
| 18 | +- [Gradle 5](https://gradle.org/releases/) or [Maven 3](https://maven.apache.org/docs/history.html) |
| 19 | +- The Bash shell. For Linux and macOS, this is included by default. In Windows 10, you can install the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) to get a Windows-integrated version of Ubuntu and Bash. |
| 20 | +- [The AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) v1.17 or newer. |
| 21 | + |
| 22 | +If you use the AWS CLI v2, add the following to your [configuration file](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html) (`~/.aws/config`): |
| 23 | + |
| 24 | +``` |
| 25 | +cli_binary_format=raw-in-base64-out |
| 26 | +``` |
| 27 | + |
| 28 | +This setting enables the AWS CLI v2 to load JSON events from a file, matching the v1 behavior. |
| 29 | + |
| 30 | +# Setup |
| 31 | +Download or clone this repository. |
| 32 | + |
| 33 | + $ git clone https://github.com/awsdocs/aws-lambda-developer-guide.git |
| 34 | + $ cd aws-lambda-developer-guide/sample-apps/blank-java |
| 35 | + |
| 36 | +To create a new bucket for deployment artifacts, run `1-create-bucket.sh`. |
| 37 | + |
| 38 | + blank-java$ ./1-create-bucket.sh |
| 39 | + make_bucket: lambda-artifacts-a5e491dbb5b22e0d |
| 40 | + |
| 41 | +To build a Lambda layer that contains the function's runtime dependencies, run `2-build-layer.sh`. Packaging dependencies in a layer reduces the size of the deployment package that you upload when you modify your code. |
| 42 | + |
| 43 | + blank-java$ ./2-build-layer.sh |
| 44 | + |
| 45 | +# Deploy |
| 46 | + |
| 47 | +To deploy the application, run `3-deploy.sh`. |
| 48 | + |
| 49 | + blank-java$ ./3-deploy.sh |
| 50 | + BUILD SUCCESSFUL in 1s |
| 51 | + Successfully packaged artifacts and wrote output template to file out.yml. |
| 52 | + Waiting for changeset to be created.. |
| 53 | + Successfully created/updated stack - blank-java |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +You can also build the application with Maven. To use maven, add `mvn` to the command. |
| 58 | + |
| 59 | + java-basic$ ./3-deploy.sh mvn |
| 60 | + [INFO] Scanning for projects... |
| 61 | + [INFO] -----------------------< com.example:blank-java >----------------------- |
| 62 | + [INFO] Building blank-java-function 1.0-SNAPSHOT |
| 63 | + [INFO] --------------------------------[ jar ]--------------------------------- |
| 64 | + ... |
| 65 | + |
| 66 | +# Test |
| 67 | +To invoke the function, run `4-invoke.sh`. |
| 68 | + |
| 69 | + blank-java$ ./4-invoke.sh |
| 70 | + { |
| 71 | + "StatusCode": 200, |
| 72 | + "ExecutedVersion": "$LATEST" |
| 73 | + } |
| 74 | + |
| 75 | +Let the script invoke the function a few times and then press `CRTL+C` to exit. |
| 76 | + |
| 77 | +The application uses AWS X-Ray to trace requests. Open the [X-Ray console](https://console.aws.amazon.com/xray/home#/service-map) to view the service map. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +Finally, view the application in the Lambda console. |
| 86 | + |
| 87 | +*To view the application* |
| 88 | +1. Open the [applications page](https://console.aws.amazon.com/lambda/home#/applications) in the Lambda console. |
| 89 | +2. Choose **blank-java**. |
| 90 | + |
| 91 | +  |
| 92 | + |
| 93 | +# Cleanup |
| 94 | +To delete the application, run `5-cleanup.sh`. |
| 95 | + |
| 96 | + blank$ ./5-cleanup.sh |
0 commit comments